Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
Version: Windchill 12.0
Hello,
In Windchill, I am creating an automatic promotion request at the end of the change review process with the following code. However, I have two questions that I couldn't resolve.
Question: I have defined a boolean variable "Should automatic promotion request be initiated?" within the activity at the approval stage for the automatic promotion request code. I want the code below to execute if this variable is true; otherwise, it should not execute. How can I achieve this?
Question: I want the person in the submitter role who initiated the change review process to be assigned as the "created by" for the promotion request. These questions might be simple, but I haven't been able to find a solution.
"Code to Automatically Create Promotion Requests for Documents in Change Review"
wt.pom.Transaction trx = new wt.pom.Transaction();
wt.change2.WTChangeReview changeReview = (wt.change2.WTChangeReview) primaryBusinessObject;
// ChangeReview nesnesinden kapsayıcıyı alıyoruz
wt.inf.container.WTContainer container = changeReview.getContainer();
wt.inf.container.WTContainerRef containerRef = wt.inf.container.WTContainerRef.newWTContainerRef(container);
// ChangeReview nesnesinden etkilenen dokümanları alıyoruz
wt.fc.QueryResult queryResult = wt.change2.ChangeHelper2.service.getChangeables(changeReview);
wt.fc.collections.WTSet set = new wt.fc.collections.WTHashSet();
while (queryResult.hasMoreElements()) {
wt.doc.WTDocument document = (wt.doc.WTDocument) queryResult.nextElement();
set.add(document); // WTDocument nesnelerini koleksiyona ekliyoruz
}
try {
trx.start();
wt.maturity.MaturityBaseline maturityBaseline = wt.maturity.MaturityBaseline.newMaturityBaseline();
maturityBaseline.setContainerReference(containerRef);
maturityBaseline = (wt.maturity.MaturityBaseline) wt.fc.PersistenceHelper.manager.save(maturityBaseline);
wt.maturity.PromotionNotice pn = wt.maturity.PromotionNotice.newPromotionNotice("Custom_Promotion_Request");
pn.setTypeDefinitionReference(wt.type.TypedUtilityServiceHelper.service.getTypeDefinitionReference("tr.com.aselsan.hbtwinctest02.HBT.PromotionNoticeTest"));
pn.setPromotionDate(new java.sql.Timestamp(System.currentTimeMillis()));
pn.setContainer(container);
pn.setContainerReference(containerRef);
pn.setMaturityState(wt.lifecycle.State.toState("RELEASED"));
pn.setDescription("theDescription");
pn.setConfiguration(maturityBaseline);
// Assign iteration creator with submitterRef before saving Promotion Notice
// wt.vc.VersionControlHelper.assignIterationCreator((wt.vc.Versioned) pn, submitterRef);
wt.vc.baseline.BaselineHelper.service.addToBaseline(set, pn.getConfiguration());
pn = wt.maturity.MaturityHelper.service.savePromotionNotice(pn);
wt.maturity.MaturityHelper.service.savePromotionTargets(pn, set);
trx.commit();
trx = null;
} catch (Exception e) {
if (trx != null) {
trx.rollback();
}
e.printStackTrace(); // Hata durumunu işleyin veya kaydedin
}
"The assignment code I used when creating a different object before, but this code did not work (perhaps I used it incorrectly)."
// Assign iteration creator with submitterRef before saving Promotion Notice
// wt.vc.VersionControlHelper.assignIterationCreator((wt.vc.Versioned) pn, submitterRef);
@HelesicPetr, I believe you can help with this. I would really appreciate your assistance. Have a great day.
Solved! Go to Solution.
For the creator, I would try to use setPrincipal with the name of user that should be used
usually you get session principal store it as a original user variable
then change the session to user who should save the object
save the object
set the session user back to original user.
// get the session user
wt.session.SessionHelper.manager.getPrincipal();
// set the session user
wt.session.SessionHelper.manager.setPrincipal("userName");
PetrH
Hi Emrec@n
1. You can use the variable in the condition and based on that value go to a right way in the workflow path
usually copy the variable from user task to global variable in the workflow. it is ootb function.
2. I guess that the method assignIterationCreator can not be used to the object that is not versioned. Promotion Request does not have revisions and iterations..
PetrH
Thank you very much for your guidance. How can I copy the selected value within the Activity to a global variable? Do you have a ready method for this? Finally, what method can I use to assign the user to the promotion notice? Do you have any suggestions?🙏🙏🙏
For the creator, I would try to use setPrincipal with the name of user that should be used
usually you get session principal store it as a original user variable
then change the session to user who should save the object
save the object
set the session user back to original user.
// get the session user
wt.session.SessionHelper.manager.getPrincipal();
// set the session user
wt.session.SessionHelper.manager.setPrincipal("userName");
PetrH
@HelesicPetr Great, both of my issues have been resolved. Thank you very much for your guidance. I was very close to the solution, but your direction helped me reach the result. You are the best Windchill expert I have encountered.
I am sharing the complete code below for those who need it.
// get the session user
wt.org.WTPrincipal originalUser = wt.session.SessionHelper.manager.getPrincipal();
wt.pom.Transaction trx = new wt.pom.Transaction();
wt.change2.WTChangeReview changeReview = (wt.change2.WTChangeReview)
wt.team.Team team = wt.team.TeamHelper.service.getTeam(changeReview);
java.util.HashMap map = wt.team.TeamHelper.service.findAllParticipantsByRole(team);
primaryBusinessObject;
wt.inf.container.WTContainer container = changeReview.getContainer();
wt.inf.container.WTContainerRef containerRef = wt.inf.container.WTContainerRef.newWTContainerRef(container);
wt.fc.QueryResult queryResult = wt.change2.ChangeHelper2.service.getChangeables(changeReview);
wt.fc.collections.WTSet set = new wt.fc.collections.WTHashSet();
while (queryResult.hasMoreElements()) {
wt.doc.WTDocument document = (wt.doc.WTDocument) queryResult.nextElement();
set.add(document);
}
try {
trx.start();
// Belirli bir kullanıcıyı "SUBMITTER" rolü olarak ayarlama
String submitterUserName = null;
for (Object key : map.keySet()) {
wt.project.Role role = (wt.project.Role) key;
if (role.toString().equals("SUBMITTER")) {
java.util.List participants = (java.util.List) map.get(key);
if (!participants.isEmpty()) {
wt.org.WTPrincipalReference participantRef = (wt.org.WTPrincipalReference) participants.get(0);
wt.org.WTPrincipal participant = participantRef.getPrincipal();
submitterUserName = participant.getName();
break;
}
}
}
if (submitterUserName != null) {
// Set the session user to the submitter
wt.session.SessionHelper.manager.setPrincipal(submitterUserName);
wt.maturity.MaturityBaseline maturityBaseline = wt.maturity.MaturityBaseline.newMaturityBaseline();
maturityBaseline.setContainerReference(containerRef);
maturityBaseline = (wt.maturity.MaturityBaseline) wt.fc.PersistenceHelper.manager.save(maturityBaseline);
wt.maturity.PromotionNotice pn = wt.maturity.PromotionNotice.newPromotionNotice("Custom_Promotion_Request");
pn.setTypeDefinitionReference(wt.type.TypedUtilityServiceHelper.service.getTypeDefinitionReference("tr.com.aselsan.hbtwinctest02.HBT.PromotionNoticeTest"));
pn.setPromotionDate(new java.sql.Timestamp(System.currentTimeMillis()));
pn.setContainer(container);
pn.setContainerReference(containerRef);
pn.setMaturityState(wt.lifecycle.State.toState("RELEASED"));
pn.setDescription("theDescription");
pn.setConfiguration(maturityBaseline);
wt.vc.baseline.BaselineHelper.service.addToBaseline(set, pn.getConfiguration());
pn = wt.maturity.MaturityHelper.service.savePromotionNotice(pn);
wt.maturity.MaturityHelper.service.savePromotionTargets(pn, set);
} else {
throw new Exception("Submitter role not found.");
}
trx.commit();
trx = null;
} catch (Exception e) {
if (trx != null) {
trx.rollback();
}
e.printStackTrace();
} finally {
// Restore the original user
wt.session.SessionHelper.manager.setPrincipal(originalUser.getName());
}