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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Checking for document creation before allow task completion

GregOlson
15-Moonstone

Checking for document creation before allow task completion

I need assistance creating a code for a conditional node that will check to make sure the task has been complete.  The task for this particular node is that the user creates a service request within windchill.  How I see this working would be the system would look at the PBO of the workflow and grab the value in the attribute called "Serial #" and add it to a variable in the workflow.  Then the system can search for a service request document and see if there is one out there at also has a serial # that matches.  If yes then it allows the workflow to move on, if no it sends another task back to the user to create the document and will stay in the loop until he or she does.

Below is a sample of code we already have that does this exact thing, but for Managed Collections (matches names and adds PBO to MC automatically).

wt.facade.persistedcollection.ManagedCollection mc = null;

java.lang.String collectionName = ((wt.doc.WTDocument)primaryBusinessObject).getName().toUpperCase();

wt.query.QuerySpec qs = new

wt.query.QuerySpec(wt.facade.persistedcollection.ManagedCollection.class);

qs.appendWhere(new wt.query.SearchCondition(wt.facade.persistedcollection.ManagedCollection.class, wt.facade.persistedcollection.ManagedCollection.NAME, wt.query.SearchCondition.EQUAL, collectionName), new int[] { 0 });

wt.fc.QueryResult qr = wt.fc.PersistenceHelper.manager.find((wt.pds.StatementSpec) qs);

if (qr.hasMoreElements()) {

mc = (wt.facade.persistedcollection.ManagedCollection) qr.nextElement();

System.out.println("\n\n\t\t Found Managed Collection!");

} else {

System.out.println("Unable to find ManagedCollection with name \"" + collectionName + "\"");

//save the MC name in a global variable so that we can use it in the activity that we will be sending to the admin to ask for creating the MC

MCName=collectionName;

result="MCNOTFound";

}

if (mc != null)

{

mc = (wt.facade.persistedcollection.ManagedCollection) wt.facade.persistedcollection.PersistedCollectionHelper.service.checkout(mc);

wt.facade.persistedcollection.PersistedCollectionHelper.service.addSeed(mc, (wt.doc.WTDocument)primaryBusinessObject);

//wt.facade.persistedcollection.PersistedCollectionHelper.service.addMember(mc, (wt.doc.WTDocument)primaryBusinessObject);

wt.facade.persistedcollection.PersistedCollectionRecipe recipe = wt.facade.persistedcollection.PersistedCollectionHelper.service.getRecipe(mc);

         if (recipe == null) {

             recipe = wt.facade.persistedcollection.PersistedCollectionRecipe.newPersistedCollectionRecipe();

             wt.facade.persistedcollection.PersistedCollectionHelper.service.setRecipe(mc, recipe);

         }

         mc = (wt.facade.persistedcollection.ManagedCollection) wt.facade.persistedcollection.PersistedCollectionHelper.service.refreshMembers(mc, "AddToManagedCollection");

         mc = (wt.facade.persistedcollection.ManagedCollection) wt.facade.persistedcollection.PersistedCollectionHelper.service.checkin(mc);

       result="AddedToMC";

}

I don't need to add the PBO to anything, just need to make sure users are actually completing their task before they "complete task" in the UI.

Any assistance or ideas would be greatly appreciated....also, I am not a code guy at all.


Greg


1 ACCEPTED SOLUTION

Accepted Solutions
GregOlson
15-Moonstone
(To:GregOlson)

With PTCs help I was able to get code working in a conditional node...see below:

Object pbo = primaryBusinessObject;

boolean createSRQ = false;

wt.doc.WTDocument myDoc = null;

String QSNValue = null;

Object QSN = null;

String SRQSNValue = null;

Object SRQSN = null;

if(pbo.getClass().isAssignableFrom(wt.doc.WTDocument.class)){

    System.out.println("\n\t******** PBO is a WTDocument!");

    myDoc = (wt.doc.WTDocument) primaryBusinessObject;

    System.out.println("\n\t******** PBO number " + myDoc.getNumber());

    com.ptc.core.lwc.server.PersistableAdapter obj = new com.ptc.core.lwc.server.PersistableAdapter(myDoc,null,java.util.Locale.ENGLISH,null);

    obj.load("Serial.Number");

    QSN = obj.get("Serial.Number");

    if(QSN != null){

        QSNValue = QSN.toString();

        System.out.println("\n\tSerial Number IBA on Sales Questionnaire is: " + QSNValue);

    }else{

        System.out.println("\n\t********Serial Number is Null. Please check the Attribute Internal Name!");

    }

    // Below is second part, this is querying only "Service Request" WTDocuments and checking matching IBA value

    com.ptc.core.meta.common.IdentifierFactory IDENTIFIER_FACTORY = (com.ptc.core.meta.common.IdentifierFactory) wt.services.ServiceProviderHelper.getService(com.ptc.core.meta.common.IdentifierFactory.class, "logical");

    com.ptc.core.meta.common.TypeIdentifier tid = null;

    try {

        tid = (com.ptc.core.meta.common.TypeIdentifier) IDENTIFIER_FACTORY.get("com.M_BCO.CORP.Service_Request");

    } catch (com.ptc.core.meta.common.IllegalFormatException e) {

        e.printStackTrace();

    }

    Class qc = wt.doc.WTDocument.class;

    wt.query.QuerySpec qs = null;

    try {

        qs = new wt.query.QuerySpec();

    } catch (wt.query.QueryException e) {

        e.printStackTrace();

    }

    int idx = 0;

    try {

        idx = qs.addClassList(qc, true);

    } catch (wt.query.QueryException e) {

        e.printStackTrace();

    }

    wt.query.SearchCondition sc = null;

    try {

        sc = wt.type.TypedUtilityServiceHelper.service.getSearchCondition(tid, true);

    } catch (wt.util.WTException e1) {

        e1.printStackTrace();

    }

    try {

        qs.appendWhere(sc, new int[] { idx });

    } catch (wt.query.QueryException e1) {

        e1.printStackTrace();

    }

    // System.out.println("*** Printing QS: " + qs);

    wt.fc.QueryResult qr = null;

    try {

        qr = wt.fc.PersistenceHelper.manager.find((wt.pds.StatementSpec)qs);

    } catch (wt.util.WTException e) {

        e.printStackTrace();

    }

    // Iterate through all "Service Request" WTDocuments & check if it has matching QSNValue == SRQSNValue value

    while (qr.hasMoreElements()) {

        wt.fc.Persistable[] ar = (wt.fc.Persistable[]) qr.nextElement();

        wt.doc.WTDocument doc = (wt.doc.WTDocument) ar[idx];

        com.ptc.core.lwc.server.PersistableAdapter obj2 = new com.ptc.core.lwc.server.PersistableAdapter(doc,null,java.util.Locale.ENGLISH,null);

        obj2.load("Serial_Number");

        SRQSN = obj2.get("Serial_Number");

        if(SRQSN != null){

            SRQSNValue = SRQSN.toString();

            System.out.println("\n\tSerial Number IBA on Service Request is: " + QSNValue);

            if (QSNValue.contains(SRQSNValue)) {

                System.out.println("\n\t Hurreyyyyy... We found matching Serial Number!!!");

                createSRQ = true;

            }

            else {

                System.out.println("\n\t Bad luck, no matching Serial Number found. Create one to proceed further...");

                createSRQ = false;

            }

        }

    }

}else{

    System.out.println("\n\n\t\t********SRQSN is Null.Please check the Attribute Internal Name!");

}

if(createSRQ){

    result="go";

}else{

    result="reroute";

}

View solution in original post

4 REPLIES 4
GregOlson
15-Moonstone
(To:GregOlson)

This code might be closer to what I need....this one looks at an attribute of the PBO and compares it to any change notices that are out in the system...then makes a routing decision based on the return.

Object pbo=primaryBusinessObject;

    boolean shootEmail=false;

    wt.doc.WTDocument myDoc=null;

    Object SONumber = null;

      String SONumberValue=null;

    Object ECNSONumber =null;

    String ECNSONumberValue =null;

   

    if(pbo.getClass().isAssignableFrom(wt.doc.WTDocument.class)){

            //System.out.println("\n\n\t\t********PBO is a WTDocument!");

            myDoc=(wt.doc.WTDocument)primaryBusinessObject;

            //System.out.println("\n\n\t\t********PBO is a WTDocument with numaber "+myDoc.getNumber());

   

            com.ptc.core.lwc.server.PersistableAdapter obj = new com.ptc.core.lwc.server.PersistableAdapter(myDoc,null,java.util.Locale.ENGLISH,null);

            obj.load("Sales_Order_Number");

            SONumber = obj.get("Sales_Order_Number");

            if(SONumber!=null){

                  SONumberValue=SONumber.toString();

            }else{

                  //System.out.println("\n\n\t\t********SONumber is Null.Please check the Attribute Internal Name!");

            }

    //we have the SONumber for WTDocument now. We will now search for CNs

    wt.query.QuerySpec qs= new wt.query.QuerySpec(wt.change2.WTChangeOrder2.class);

    wt.fc.QueryResult qr= wt.fc.PersistenceHelper.manager.find(qs);

    while (qr.hasMoreElements()) {

        wt.change2.WTChangeOrder2 CN = (wt.change2.WTChangeOrder2) qr.nextElement();

        com.ptc.core.lwc.server.PersistableAdapter obj2 = new com.ptc.core.lwc.server.PersistableAdapter(CN,null,java.util.Locale.ENGLISH,null);

        obj2.load("Sales_Order_Number");

        ECNSONumber = obj2.get("Sales_Order_Number");

            if(ECNSONumber!=null){

                  ECNSONumberValue=ECNSONumber.toString();

                  //if we find a CN with matching SON

                  if(ECNSONumberValue.contains(SONumberValue)){

            String ECNState=CN.getState().toString();

                        if(!ECNState.equalsIgnoreCase("RESOLVED")){

                              shootEmail=true;

                        }

                  } 

            }else{

                  //System.out.println("\n\n\t\t********ECNSONumber is Null.Please check the Attribute Internal Name!");

            } 

    }

    }

    if(shootEmail){

//System.out.println("\n\n\t\t********Result is Shoot email!");

result="shootEmail";

    }else{

        result="go";

    }

kpritchard
4-Participant
(To:GregOlson)

From what I can see, you would need to create an InfoEngine Task to query based on an IBA (custom attribute)

LoriSood
22-Sapphire II
(To:GregOlson)

Hi Greg, any luck using Keir's suggestion here?

GregOlson
15-Moonstone
(To:GregOlson)

With PTCs help I was able to get code working in a conditional node...see below:

Object pbo = primaryBusinessObject;

boolean createSRQ = false;

wt.doc.WTDocument myDoc = null;

String QSNValue = null;

Object QSN = null;

String SRQSNValue = null;

Object SRQSN = null;

if(pbo.getClass().isAssignableFrom(wt.doc.WTDocument.class)){

    System.out.println("\n\t******** PBO is a WTDocument!");

    myDoc = (wt.doc.WTDocument) primaryBusinessObject;

    System.out.println("\n\t******** PBO number " + myDoc.getNumber());

    com.ptc.core.lwc.server.PersistableAdapter obj = new com.ptc.core.lwc.server.PersistableAdapter(myDoc,null,java.util.Locale.ENGLISH,null);

    obj.load("Serial.Number");

    QSN = obj.get("Serial.Number");

    if(QSN != null){

        QSNValue = QSN.toString();

        System.out.println("\n\tSerial Number IBA on Sales Questionnaire is: " + QSNValue);

    }else{

        System.out.println("\n\t********Serial Number is Null. Please check the Attribute Internal Name!");

    }

    // Below is second part, this is querying only "Service Request" WTDocuments and checking matching IBA value

    com.ptc.core.meta.common.IdentifierFactory IDENTIFIER_FACTORY = (com.ptc.core.meta.common.IdentifierFactory) wt.services.ServiceProviderHelper.getService(com.ptc.core.meta.common.IdentifierFactory.class, "logical");

    com.ptc.core.meta.common.TypeIdentifier tid = null;

    try {

        tid = (com.ptc.core.meta.common.TypeIdentifier) IDENTIFIER_FACTORY.get("com.M_BCO.CORP.Service_Request");

    } catch (com.ptc.core.meta.common.IllegalFormatException e) {

        e.printStackTrace();

    }

    Class qc = wt.doc.WTDocument.class;

    wt.query.QuerySpec qs = null;

    try {

        qs = new wt.query.QuerySpec();

    } catch (wt.query.QueryException e) {

        e.printStackTrace();

    }

    int idx = 0;

    try {

        idx = qs.addClassList(qc, true);

    } catch (wt.query.QueryException e) {

        e.printStackTrace();

    }

    wt.query.SearchCondition sc = null;

    try {

        sc = wt.type.TypedUtilityServiceHelper.service.getSearchCondition(tid, true);

    } catch (wt.util.WTException e1) {

        e1.printStackTrace();

    }

    try {

        qs.appendWhere(sc, new int[] { idx });

    } catch (wt.query.QueryException e1) {

        e1.printStackTrace();

    }

    // System.out.println("*** Printing QS: " + qs);

    wt.fc.QueryResult qr = null;

    try {

        qr = wt.fc.PersistenceHelper.manager.find((wt.pds.StatementSpec)qs);

    } catch (wt.util.WTException e) {

        e.printStackTrace();

    }

    // Iterate through all "Service Request" WTDocuments & check if it has matching QSNValue == SRQSNValue value

    while (qr.hasMoreElements()) {

        wt.fc.Persistable[] ar = (wt.fc.Persistable[]) qr.nextElement();

        wt.doc.WTDocument doc = (wt.doc.WTDocument) ar[idx];

        com.ptc.core.lwc.server.PersistableAdapter obj2 = new com.ptc.core.lwc.server.PersistableAdapter(doc,null,java.util.Locale.ENGLISH,null);

        obj2.load("Serial_Number");

        SRQSN = obj2.get("Serial_Number");

        if(SRQSN != null){

            SRQSNValue = SRQSN.toString();

            System.out.println("\n\tSerial Number IBA on Service Request is: " + QSNValue);

            if (QSNValue.contains(SRQSNValue)) {

                System.out.println("\n\t Hurreyyyyy... We found matching Serial Number!!!");

                createSRQ = true;

            }

            else {

                System.out.println("\n\t Bad luck, no matching Serial Number found. Create one to proceed further...");

                createSRQ = false;

            }

        }

    }

}else{

    System.out.println("\n\n\t\t********SRQSN is Null.Please check the Attribute Internal Name!");

}

if(createSRQ){

    result="go";

}else{

    result="reroute";

}

Top Tags