workflow scratchpad in if condition

RudhraKAM
Tera Guru

I have a workflow in which i have 2 tasks(A,B) on sc_task table , if the task A is closed complete( state=3) then we need to store that value in workflow scratch pad 

In task A in advance section I added "workflow.scratchpad.state1 = task.state;"

and after task b is completed i need to check if the Task A is close complete then i need to create another task if not send to end for this i added If activity to check , I am having issue with the code , it always goes to No even if the state of task A is closed complete.

answer =  ifScript();

function ifScript(){
workflow.scratchpad.state1;
var gr = new GlideRecord('sc_task');
gr.addQuery('parent',current.sys_id);   //Copied this code from some other thread, is this condition valid ?
gr.query();
if(gr.next()){
if (workflow.scratchpad.state1 == 3) {    //3 is closed complete
return 'yes';
}
else
return 'no';
}
}
1 ACCEPTED SOLUTION

Hello,

As stated above, you can glide the sc_task table and get the state using the sys_id scratchpad...

So in that same mindset...you can assign that state value to a scratchpad and use it elsewhere...or...just glide and get it...

This thread is going all over the place and I'm confused if you're reading what I wrote. All you did here is just copy and paste the same script from your original post. I don't need to see it again...

For your script...you need to gliderecord query the sc_task table and use the sys_id from the scratchpad...

answer =  ifScript();

function ifScript(){
var gr = new GlideRecord('sc_task');
gr.addQuery('sys_id', workflow.scratchpad.taskID);
gr.query();
if(gr.next()){
if (gr.state == 3) {
return 'yes';
} else {
return 'no';
  }
 }
}

 

Please mark reply as Helpful/Correct and any other post as Helpful, if it was.

Thanks

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

12 REPLIES 12

Ok, yea, please give all information that you can so that we can assist you. Like if it's scoped app, etc. in the future.

If you can't set the taskID scratchpad using setNewGuid(), then you can do this instead in your if activity script (so remove the scratchpad from Task A script section) and just use this in if activity:

answer = ifScript();

function ifScript() {
var gr = new GlideRecord('sc_task');
gr.addQuery('parent',current.sys_id);
gr.orderBy('number');
gr.query();
if(gr.next()) {
workflow.scratchpad.taskID = gr.sys_id;
workflow.scratchpad.taskState = gr.state;
if (workflow.scratchpad.taskState == 3) {
return 'yes';
} else {
return 'no';
  }
 }
}

I did it somewhat the long way and set the sys_id and state to a scratchpad variable for use elsewhere as I don't know fully what your requirements are. 

I have no idea what table this workflow is running on...so you'd want to ensure you're using the right "parent" type field, whether that literally the word "parent" or in the case of a request item...then it's usually "request_item". So make sure you're using the right parent field in your query.

I'm going to install the security incident plugin and see what I can find, but the above should work.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thanks for the reply Allen I really appreciate it,

 

If we remove the scratch pad in the Task A how do we capture the activity in task A as we have almost 6 Tasks in that workflow.

The workflow runs on sn_si_incident , 

tasks created are on sc_task , , when a task is created there is no parent relation on the task , this makes this an issue 

find_real_file.png

kam...I'm not going to be able to keep explaining after this. I'm sorry.

You're not reading what I'm saying.

I said above, in my last response that if you can't use setNewGuid(), as you've said, then you would use the script I provided above. I also said in that very same response that in that same if script...it is capturing the task id AND the state...and placing them as scratchpad variables IF you need to use them...

I don't know Security Incident, within ServiceNow...so I have no clue as to why Security Incident would create catalog tasks...unless that's what YOU'RE doing...if YOU'RE selecting catalog task as the activity in YOUR workflow...then I would say you're wrong.

You need to use the create task activity, not create catalog task.

Once you do that...that should have the parent field or a parent type field to tap in to.

Catalog tasks are for catalog related requests...which have a parent system all mapped out already

Furthmore, after looking at security incident, it seems you should be using "response tasks" anyways...

https://docs.servicenow.com/bundle/orlando-security-management/page/product/security-incident-respon...

We don't even know what you're doing. We don't know why you're doing this workflow...we don't know what the end goal is...we don't know why you've designed it the way you have.

Unfortunately, it's just going down a rabbit hole where we don't have any information from you...fully...and then information I am providing...you aren't reading.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!