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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Ability to extract issue information inside a JavaScript api call

jsummers
11-Garnet

Ability to extract issue information inside a JavaScript api call

Would like to know if any one has tried creating a JavaSript routine to extract information from an issue to update with addition information;
Like retrieve the Rich text information from an Incident Ticket and the old and new add additional details together then update the field with new merged information.
 
i.e.
 
EXTRACT OLD DATA
var command = new Packages.com.mks.api.Command("im", "issues --fields='Detailed Description'");
        command.addSelection(UpdateItemId);
        var response = apiBean.executeCmd(command);
 
UPDATE WITH OLD AND NEW MERGED TOGETHER
var issuesCommand = new Packages.com.mks.api.Command("im", "editissue");
issuesCommand.addOption(new Packages.com.mks.api.Option("UpdateIssue", "Detail Description" + "=" + AdditionalDetailDescription));
            issuesCommand.addSelection(UpdateItemId);
        var response = apiBean.executeCmd(issuesCommand);
 
ACCEPTED SOLUTION

Accepted Solutions

OK, I understand the use case, thanks also for sharing the script.

This is a very big one, so I don't have time to try to fix one part to explain how to do it.

What I can say is this: you do not need and should not be using the API to process Integrity items in this script.

Everything you want to do can be achieved with the java beans provided for triggers. Have a look at an example script provided on the server like "postLinkedIssue.js". I picked that one because it creates and updates items.

In it, you find code like: newIssue.setFieldValue(description, newDescription)

All the java beans you need are documented in "Event Trigger Java Documentation" on your server's home page. They can be used create items, edit rich text fields, manipulate attachments, etc.

The main one is "ScriptIssueDeltaBean", have a look at all the methods available to you there. I can see you've defined a "delta" variable, now you have to use it. Please do not use the API.

View solution in original post

9 REPLIES 9

Is the JavaScript in question an Integrity server-side trigger script?

If that's the case, why use an API call? Such calls are not recommended to update items unless you really must, and know what you're doing.

You can use the usual java beans and do exactly what you describe below with the "issue delta bean".

If it's not a server-side trigger script, then I don't think your code example will work (no java packages to import). You would have to use the "real" API or web services. And then rich text fields can be tricky to manipulate that way. Can you explain your use case in more details?

 

Attaching javascript file as text file and yes this a scheduled trigger on the server. Search for function "UpdateIncidentTicket" and "isValidItemId"

 

The process is currently extracting email data from outlook exchange inbox folder and creating an Incident Ticket.

 

We are wanting to enhance the process for updating the existing Incident Tickets by having the user or system email the same exchange inbox folder with additional information or when someone reply's to the notifications that we update the existing Incident ticket with new information instead of creating a new Incident Ticket.   

 

So the process would be;

  • Check to see if it is an Incident Ticket by testing for specific information in the subject line to verify that it is an existing ticket. (That part is working.)

 

  • I'm able to add the update and/or reply email as an attachment but does not work will with rich text information.

 

  • So my goal is to update the current "Detail Description" with the additional information by
    •    Extracting the current value and adding the new information from the body of the email 
    •    Update the issue's "Detail Description" with old and new information merged together. 

Currently getting error message

    [API-issues --fields=Detailed Description ]: im: Invalid command: issues --fields=Detailed Description 

OK, I understand the use case, thanks also for sharing the script.

This is a very big one, so I don't have time to try to fix one part to explain how to do it.

What I can say is this: you do not need and should not be using the API to process Integrity items in this script.

Everything you want to do can be achieved with the java beans provided for triggers. Have a look at an example script provided on the server like "postLinkedIssue.js". I picked that one because it creates and updates items.

In it, you find code like: newIssue.setFieldValue(description, newDescription)

All the java beans you need are documented in "Event Trigger Java Documentation" on your server's home page. They can be used create items, edit rich text fields, manipulate attachments, etc.

The main one is "ScriptIssueDeltaBean", have a look at all the methods available to you there. I can see you've defined a "delta" variable, now you have to use it. Please do not use the API.

My code below fails with the following error
Command parameters 939 : relatedItemLinkField =Caused By relatedItemId =118471
addRelatedItem Failed 945: Cannot convert null to an object
function addRelatedItem(newItemId, relatedItemId) {
try {
// var command = new Packages.com.mks.api.Command("im", "editissue");
// print('Related Item Link Field and new item id 936 : releatedItemLinkField = ' + relatedItemLinkField + " newItemId = " + newItemId + " relatedItemId = " + relatedItemId);
print("command parameters 939 : " + "relatedItemLinkField =" + relatedItemLinkField + " relatedItemId =" + relatedItemId);
// command.addOption(new Packages.com.mks.api.Option("--addRelationships=" + relatedItemLinkField + "=" + relatedItemId));
// command.addSelection(newItemId);
// var response = apiBean.executeCmd(command);
// var result = response.getResult();
delta.addRelatedIssue(relatedItemLinkField, relatedItemId);
} catch (ex) {print('addRelatedItem Failed 945: ' + ex.message);}
}



Do not use the API !

Please read what I tried to explain before, the API is not the right way to write a PRE trigger.

Note the API code is commented out only the DELTA command is running

function addRelatedItem(newItemId, relatedItemId) {
try {
print("command parameters 939 : " + "relatedItemLinkField =" + relatedItemLinkField + " relatedItemId =" + relatedItemId);
delta.setRelationshipFieldValue(relatedItemLinkField, relatedItemId);
} catch (ex) {print('addRelatedItem Failed 945: ' + ex.message);}
}

So here is the next try at this(this process is a scheduled trigger that runs every 15 minutes to update information from outside of Integrity) the getIsssueBean from the manual javadocs triggers gave me the impression that this what you use to access a specific Issue

 

var locale = bsf.lookupBean("ScriptServerBean");
 //  Need to get the old item to update based upon the value in UpdateItemId which has a numeric value of the issue //   to be updated 
 
locale.getIssueBean(UpdateItemId); --- Fails for the following error -- "Cannot convert null to an object."
// next step is to add the attachment file that has been created in another function successfully. 
addAttachment(attachname);

Not sure about your use case and sequence, but this is what I can offer.
You're not using the right beans. For a scheduled trigger that updates items, the sequence would be something like this:

// Initialize the environment with necessary beans
var sb = bsf.lookupBean( "imServerBean" );
//
// Get IDs of all items returned by scheduled trigger's query
var stab = bsf.lookupBean( "imScheduleTriggerArgumentsBean" );
var itemsList= stab.getIssues();
//
// Process all found items
for ( var i = 0; i < itemsList.length; i ++ ) {
  //
  // Get a delta bean for the item so it can be updated
  var ib = sb.getIssueDeltaBean( itemsList[ i ]);
  //
  // Process this item, for example:
  ib.setFieldValue( FieldName , FieldValue );
  ib.addRelatedIssue( RelFieldName , RelFieldValue );
}
// Done

 

I hope this helps.

Announcements


Top Tags