cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Conditional Execution of Automatic Promotion Requests and Assignment of 'Created By' Role"

Emrec@n
6-Contributor

Conditional Execution of Automatic Promotion Requests and Assignment of 'Created By' Role"

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.

  1. 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?

  2. 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.

1 ACCEPTED SOLUTION

Accepted Solutions
HelesicPetr
22-Sapphire I
(To:Emrec@n)

Emrec@n 

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

View solution in original post

5 REPLIES 5
HelesicPetr
22-Sapphire I
(To:Emrec@n)

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

Emrec@n
6-Contributor
(To:HelesicPetr)

@HelesicPetr 

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?🙏🙏🙏

HelesicPetr
22-Sapphire I
(To:Emrec@n)

Hi Emrec@n 

It is OOTB function in the workflow, that does not need any coding.

HelesicPetr_0-1721388981447.png

 

PetrH

HelesicPetr
22-Sapphire I
(To:Emrec@n)

Emrec@n 

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

Emrec@n
6-Contributor
(To:HelesicPetr)

@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());
}

 

Top Tags