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

Faizeal Mohamed
Tera Guru

Hi,

 

Did you get any error? Have you tried the same code in background script?

Michael Galang
Tera Contributor

Hi Mohamed, 

 

Thank you for your response. There is  no error message. It just not copying the RITM variables value to the target table fields. 

I ran the workflow but it is only inserting new record number on the table but it did not copy the current field values of the current RITM to requested for, requested by, end of subscription are still empty and the active value is still false. Please see Picture 5. 

 

 

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.

  

Thank you! This helped me as I was using "current.variable.field_name", singular not plural, so "current.variableS.field_name" worked like a charm. :))

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */