Workflow Script to set requested_for from a variable

Mike Cumbee
Giga Expert

I need to change/set the requested_for field in a Requested Item so the Tasks emails contain the proper information.

I've made a script and placed it in both a Run Script as well as the Advanced tab for an SC_Task in a workflow.

I used the following script which appears to work (the log shows the right information), but the form still has the wrong user name:

current.requested_for = current.variables.Request_for_User;

current.requested_by = current.variables.Request_for_User;

current.update();

gs.log("FIRST RUN SCRIPT: Current Number/request_for/requested_by:" +current.number+'/'+current.requested_for.getDisplayValue()+'/'+current.requested_by.getDisplayValue());

Any suggestions?

Mike

1 ACCEPTED SOLUTION

As it's on the request table you can try



var req = new GlideRecord('sc_request');


req.get(current.request);


req.requested_for = current.variables.requested_for_user;     // or whatever the variable is called


req.update();


View solution in original post

16 REPLIES 16

Last question (I hope).   I have an email notification that is going out - but it is going off of caller_id from the sc_task.



What is the best way to update that in the workflow?



I have this, but it doesn't do it:



var gt = new GlideRecord('sc_task');


gt.addQuery('parent', current.sys_id);


gt.query();



while(gt.next())


      {


      gt.opened_by = current.variables.Request_for_User;


      current.caller_id = current.variables.Request_for_User;


      gt.update();


      current.update();


}


because there is no Caller_ID for Request/Requested Item/ SC Task



It's only in Incident...


Where is the workflow with that script running?   On the requested item?   Like you mentioned, requested items don't have a caller_id, so I'm not sure why you're setting current.caller_id.



Also, you really should never use current.update() in a workflow script.   You don't need it (the workflow will automatically handle updating the record for you whenever it hits an activity that causes a pause in execution), and it has the potential to cause some unexpected negative results.   See the official explanation provided by ServiceNow here: ValidateScriptForCurrentDotUpdate


Yes, the workflow is in for a Request, RITM.



Correct, called_id is not in this table (which is sc_req_item), or the parent.   caller_id is in Incident.


I think I was a bit confused because I wasn't fully understanding why you were asking about the caller_id field.   But I think I understand.   You are trying to use the caller_id field in the notification email, but it doesn't exist, so you were trying to add it by setting it manually.   ServiceNow won't store it in the database since the caller_id field doesn't exist for Tasks.   So when you try to use it later, it doesn't exist.



Since you are ultimately trying to set it the caller_id to the same value as requested_for on the request, just use requested_for in your notification instead of caller_id:



Caller: ${parent.request.requested_for}


Caller email: ${parent.request.requested_for.email}


Caller phone: ${parent.request.requested_for.phone}



Or am I still misunderstanding what you're trying to do?