Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to copy variables field values from RITM and insert that it to new record target table?

Michael Galang
Tera Contributor

I'm working on a catalog item that should automates the Newsletter Subscription. 

Every new subscription request after it approves by the user's manager should automatically run a script in the workflow that will copy the highlighted field values as shown in RITM reference(Picture 1) and insert all of these to a new record in the target table(u_newsletter_subscription) form(Picture 2),  then automatically submit the new record. The new record should be reflected in the Table list(Picture 3). Please see below  script code that I put in the Run Script in the Workflow and it seems there are missing in my below script codes that it not working based on the expectation. 

I would appreciate for any help. Thanks in advance. 

//GlideRecord Query
var getTicket = new GlideRecord('sc_req_item');
getTicket.addQuery('number', variables.current.number);
getTicket.addQuery('requested_for', variables.current.requested_for);
getTicket.addQuery('requested_by', variables.current.requested_by);
getTicket.addQuery('end_date', variables.current.end_date);
getTicket.query();
if(getTicket.next())
{
var recinsert = new GlideRecord('u_newsletter_subscription');
recinsert.initialize();
recinsert.setValue('number', number);
recinsert.setValue('u_requested_for', getTicket.variables.requested_for);
recinsert.setValue('u_requested_by', getTicket.variables.requested_by);
recinsert.setValue('u_end_of_subscription', getTicket.variables.end_date);
recinsert.insert();
}
var createSub = new GlideRecord('u_newsletter_subscription');
createSub.initialize();
//fill out fields
createSub.u_requested_for = getTicket.current.variables.requested_for;
createSub.u_requested_by = getTicket.current.variables.requested_by;
createSub.u_end_ofsubscription = getTicket.current.variables.end_date;
createSub.insert();

 

 

1 ACCEPTED SOLUTION

Hi,

 

I found some errors errors in your script:

You are glide query RITM table, if you are using run script in workflow no need to glide query ritm, you can use current method to get variables like current.variables.requested_for.

Even in the addQuery method, you are adding parameter as 

getTicket.addQuery('number', variables.current.number); its wrong it should be

getTicket.addQuery('number',current.variables.number); thats the reason you are not getting value while insert

 

Write a workflow run script like below to insert record in newsletter subscription table and it should work:

 

var createSub = new GlideRecord('u_newsletter_subscription');
createSub.initialize();
//fill out fields
createSub.u_requested_for =current.variables.requested_for;
createSub.u_requested_by =current.variables.requested_by;
createSub.u_end_ofsubscription = current.variables.end_date;
createSub.insert();

 

Please try and let me know.

 

Thanks,

Faizeal.

  

View solution in original post

5 REPLIES 5

Michael Galang
Tera Contributor

Hi Mohamed, 

 

It works now! Thanks a lot. I appreciate your help.