How to get the workflow of a change item programmatically?

Hi, I want to run the following client code from an Express ECO button event:
var innovator = new Innovator(); 

var thisItem = document.thisItem;
var id = thisItem.getID(); // id of Express ECO

// here should be the code that fetches the Workflow Process id.

var qryItem = innovator.newItem(); 
qryItem.loadAML( 
"<Item type='Workflow Process' id='/*----missing ID----*/' initial_action='get' action='do_l10n' select='state, name,related_id(name)' >" +
              "<Relationships>" +
                "<Item type='Workflow Process Activity' action='get' select='related_id'>"+
                  "<related_id>" +
                  "<Item type='Activity' action='get' select='name,related_id(name)'>" +
                    "<state condition='ne'>Pending</state>" +
                    "<Relationships>" +
                      "<Item type='Activity Assignment' action='get' select='related_id' >" +
                         "<related_id type='Identity' select='keyed_name' />" +
                      "</Item>" +
                    "</Relationships>" +
                  "</Item>" +
                  "</related_id>" +
                "</Item>" +
              "</Relationships>" +
            "</Item>"
); 
var resultItem = qryItem.apply(); 
Unfortunately I wan´t able to find the missing link between the Express ECO and the corresponding Workflow Process yet. Can you give me a hint how the two are connected? Thanks again! Angela
  • I think the missing key is
    <Item type='Workflow' action='get'> <source_id>{itemID}</source_id> <related_id> <Item type='Workflow Process' action='get'> . . .
    Look at this post, maybe it helps you. community.aras.com/.../  
  • Hi Mahmoud, thanks for your example! I was able to read the ID with your code instantly. I've always searched inside the WorkflowProcess ItemType for the missing link, but I didn´t notice the Workflow ItemType. Here it is even described with an "you cannot overlook it" image - which of course I have overlooked: community.aras.com/.../ But maybe someone can help me here: I want to avoid the use of multible queries for getting all needed information. So integrated the Workflow query into my regular code. Querying the Workflow Process id now works, but I cannot read my other values anymore:
    var innovator = new Innovator(); 
    
    var thisItem = document.thisItem;
    var id = thisItem.getID(); // id of CM item
    var type = thisItem.GetType(); // Type of CM item, e.g. Express ECO
    
    var workflow = innovator.newItem("Workflow","get"); 
    workflow.setProperty("source_id", id);
    // workflow.setProperty("source_type", "CBA93BEFFB4F499CAF122CB79E204983"); // Express ECO
    workflow.setAttribute("select", "state, name, related_id(name)"); 
    
    var workflowProcess = workflow.createRelatedItem("Workflow Process","get"); 
    workflowProcess.setAttribute("select", "name, id"); 
    
    var workflowProcessActivity = workflowProcess.createRelationship("Workflow Process Activity","get"); 
    workflowProcessActivity.setAttribute("select", "related_id");
    
    var activity = workflowProcessActivity.createRelatedItem("Activity", "get"); 
    activity.setPropertyCondition("state", "ne");
    activity.setProperty("state", "Pending");
    activity.setAttribute("select", "name,related_id(name)");
    
    var activityAssignment = activity.createRelationship("Activity Assignment", "get");
    activityAssignment.setAttribute("select", "id, related_id");
    
    var identity = activityAssignment.createRelatedItem("Identity", "get");
    identity.setAttribute("select", "keyed_name"); 
    
    workflow = workflow.apply(); 
    var wfpID = workflow.getItemByIndex(0).getProperty("related_id"); // Workflow Process ID - works!
    var act = workflowProcessActivity.getItemByIndex(0).getProperty("related_id"); // don´t work, returns undefined
    
    alert(wfpID + " " + act);
    What could be missing? (I converted the "staircase" AML query into a flat hierarchy. For me this structure is easier to use, but that's a personal habit.)
  • Is it possible that caused because of (name)?   workflow.setAttribute("select", "state, name, related_id(name)"); ca you try it without (name) I do not know what related_id(name)  really means.        
  • Hi Angela, I believe Mahmoud is correct. The related_id(name) in your select statement is asking for only the name property of the related item to be returned. You canreturn multiple properties from the related item by separating them with commas like this related_id(name, related_id). Chris ______________________________________ Christopher Gillis Aras Labs Software Engineer
  • I agree with you, my query was not yet optimal. The main problem, however, was how I handled the return value. I was reading the individual query parts directly, e.g. var act = workflowProcessActivity.getItemByIndex(0).getProperty("related_id"); That did not work. But with the help of XPath I am able to read the desired values:
    var act = workflow.getItemsByXPath("//Item[@type='Activity']");
    var actID = act.getItemByIndex(0).getID();
    Thank you both for your help!