How to get the updated field value from incident table and paste it in a workflow built on requested item table ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2022 11:55 AM
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!
- Labels:
-
Multiple Versions
-
Request Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2022 01:06 PM
Hello,
Just a quick doubt after seeing your script !
Through run script the incident tis created and then you are trying to pass the created incident sys_id in scratch pad variable to wait for condition activity right to check if the incident is closed or not ?
if yes i think you are trying to pass gr.sys_id where there is not gr object .I think you need to pass inc.sys_id as you are using inc variable to insert the incident .
Try to replace it with workflow.scratchpad.incidentSysId = inc.sys_id;
and in the wait for condition replace below code
var incidentSysIdHere = workflow.scratchpad.incidentSysId;//get the current incident sys_id
var gr = new GlideRecord("incident");
gr.addQuery('sys_id', incidentSysIdHere);
gr.query();
if(gr.next()) // added if(gr.next()) instead of gr.next;
{
gs.info(gr.state);
//check for incident status closed
if (gr.state == '7' || gr.state==7) { // tried to compare with both boolean and string value
answer = true;
} else {
answer = false;
}
}
Please accept the solution if it helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2022 01:44 PM
Hi Mohith Devatte,
It was my mistake when I initially displayed the code here. The real code is the following and still doesn't work:
in run script:
var gr= new GlideRecord('incident');
gr.initialize();
gr.short_description = current.value_of_field1;
gr.description = current.value_of_field2;
gr.insert();
workflow.scratchpad.incidentSysId = gr.sys_id; //get the current incident which has state open
current.update();
------------------------------------------------------------------------------------------------
in wait for condition:
var incidentSysIdHere = workflow.scratchpad.incidentSysId; //get the current incident sys_id
var gr = new GlideRecord("incident");
gr.addQuery('sys_id', incidentSysIdHere);
gr.query();
if (gr.next()) // added if(gr.next()) instead of gr.next;
{
gs.info("gr.state after updated incident to closed is: " + gr.state); // get only 1 (open) value, even I changed the incident state to 7
//check for incident status closed
if (gr.state == '7' || gr.state == 7) { // tried to compare with both boolean and string value
answer = true;
} else {
answer = false;
}
}
gs.info("answer after updated incident to closed is: " + answer); //false
---------------------------------------------------------------------------------
And this is the workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2022 02:08 PM
Hello ,
I think the wait for condition script is getting executed before the closure of incident.
Let's try one thing , please remove this activity and instead of this use timer activity and paste the script in timer activity and try
Please accept the solution if it helped you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2022 03:18 PM
Hi Mohith,
You are right 🙂 The incident state value (Open) is passed into Wait for condition, before to have time to change the incident value to Closed.
So, I inserted a Timmer (3 minutes) before to Wait for condition, then I changed the incident state value to Closed, and after 3 minutes the Wait for condition received that new value: Closed. So, the workflow has been closed and from technical point of view the problem was fixed.
But the business problem remains because I can not set a real time for a caller to close the incident. In my opinion, the Wait for condition needs to check all the time until the condition become true (incident state = Closed).
So, the problem is: it is checked only once at the beginning, even if after a while the value of the incident status will be updated. That's why returns Open value to incident state.
Maybe a BR can fix the problem ?
Thanks!