How to get the updated field value from incident table and paste it in a workflow built on requested item table ?

Florin Dascalu
Tera Contributor

Hi Guys, 

1. I created this run script (inserted into a workflow on requested item table). It has the role to create an incident and it works fine

var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = current.value_of_field1;
inc.description = current.value_of_field2;
inc.insert();

 

workflow.scratchpad.incidentSysId = gr.sys_id;       //get the current incident which has state open

current.update();

-------------------------------------------------------------------------------------------------------------------------------

2.Then, I created a Wait for condition in the same workflow to check if the status of incident is closed in order to continue. 

var incidentSysIdHere = workflow.scratchpad.incidentSysId;    //get the current incident sys_id 

var gr = new GlideRecord("incident");
gr.addQuery('sys_id', incidentSysIdHere);

gr.query();
gr.next();

if (gr.state == '7') {

    answer = true;

} else {
    
    answer = false;
}

-----------------------------------------------------------------------------------------------------------------------

3. Then I updated the status of incident to Closed, but in the workflow the value of status field remained Open and didn't update to Closed (see gs.info() below..)

var incidentSysIdHere = workflow.scratchpad.incidentSysId;//get the current incident sys_id

var gr = new GlideRecord("incident");
gr.addQuery('sys_id', incidentSysIdHere);

gr.query();
gr.next();

gs.info(gr.state);         // get only 1 (open) value, even I changed the incident state to 7

//check for incident status closed 
if (gr.state == '7') {

    answer = true;

} else {
    
    answer = false;
}

gs.info(answer);         //false

--------------------------------------------------------------------------------------------------------------------------

The question is:
- how to get in the workflow the updated value of incident state ? That means 7 (Closed) instead of 1 (Open) ?

- I tried to implement a business rule in incident table with the following conditions but doesn't work...

When: After / Update, with condition State changes to Closed

---------------------------------------------------------------------------

(function executeRule(current, previous /*null when async*/ ) {

    runWorkflow_task();

    function runWorkflow_task() {
        var parentGr = new GlideRecord('sc_req_item');
        parentGr.addQuery("state", current.sys_id);
        parentGr.query();
        if (parentGr.next()) {
            new Workflow().broadcastEventToCurrentsContexts(parentGr, 'update', null);
        }
    }

})(current, previous);


If you have any ideea how to get the updated value of state field from the incident and set it into workflow on sc_req_item let me know 🙂
Thanks!

 

 

 

 

 

 

8 REPLIES 8

Hello,

You can write a  after update BR on incident table giving condition as state changes to closed complete

But make sure that in script of BR you need to glide record to RITM table and find the corresponding RITM which created this incident through run script and close that RITM through script. If you have this link of RITM to incident you can proceed with BR  otherwise timer activity is the only way .

But only thing if you use thr BR  is you will have to end the workflow at setting the value which is at the 4 th activity in above screenshot . And in BR close thr RITM through script by updating the state value to close complete

Please mark my answer correct if it helps you

Hello, Just a follow up . If you find it helpful mark the answer as correct and helpful so that I will be helpful for future readers too

Hi Mohith Devatte,
Thank you for the recommendations and I really apreciate your effort!

The solution that I implemented and worked well, was to replace the activities "Run script" (that creates incident) and "Wait for condition"  with only one new activity in child workflow:  "Create task".
Even the workflow has been built on request table, I created a "Create task" activity that has been applied on Incident table and I checked with true the checkbox Wait for completion. In advanced section in "Create task" activity, I only added the code that populates fields from request to incident.
Then, I called with the task object the insert() method 

Example:

task.short_description = current.short_description;
task.insert();

//task is incident table object and current is object in request table)

The "Create task" activity had the purpose to create the incident and when the incident has been closed, the "Create task" has been completed and the workflow went forward to the next Run script.
And finally, the request has been closed as well.
Thank you!

Hey florin,

Glad it worked well . You made it.

But if you don't mind mark the answer correct and close the thread so that it will be helpful for future readers too ! Thanks again