Update RITM Variable value from field on table

leport
Tera Contributor

I have the need to update the contents of a variable when the field on TableXYZ changes. 

 

I have created a BR that runs when Field A on TableXYZ changes and I am trying to get it to copy the value from Field A to a variable on the RITM linked to the record in TableXYZ, but it does not appear to be working:

 

I have configured the following BR on Table XYZ and I added a log statement to troubleshoot, but my glide record keeps coming back as undefined

 

When to Run:  When is After, Update is True, Filter Conditions are when field A changes

 

Script:

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var gr = new GlideRecord('sc_item_option_mtom');
    gr.addQuery('request_item', current.u_requested_item);
    gr.addQuery('sc_item_option', 'package_description');
    gr.query();
    gs.log ('I found a record ' + gr.number, "cloud");
    if (gr.next()){
        current.package_description = gr.package_description;
    }
    current.update();

})(current, previous);
 

 

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@leport 

you want to update variable with Field A value.

But in your script you are setting package_description field on Table XYZ

Assumptions

-> if your variable name is package_description and field u_requested_item holds RITM sysId then use this script

-> if your variable is string type and package_description on Table XYZ is also string type

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var gr = new GlideRecord('sc_item_option_mtom');
    gr.addQuery('request_item', current.u_requested_item);
    gr.addQuery('sc_item_option.item_option_new.name', 'package_description');
    gr.query();
    if (gr.next()) {
        var itemOptionRec = gr.sc_item_option.getRefRecord();
        itemOptionRec.value = current.package_description;
        itemOptionRec.update();
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

PrashantT
Giga Guru

Hi @leport  ,

 

I have modified your code. If you follow my new code and replace the sys_id of your variable & field name then you can achieve this requirement.

Note: Please check my code before using this code

 

Here the code:

 

 var gr = new GlideRecord('sc_item_option_mtom');
 gr.addQuery('request_item', current.u_requested_item);
 gr.query();
 while (gr.next()) {
     var storeDepdentdentSysId = gr.sc_item_option.toString();

     var itemOption = new GlideRecord('sc_item_option');
     itemOption.addQuery('sys_id', storeDepdentdentSysId);
     itemOption.addQuery('item_option_new''sys_id'); // Replace the sys_id value to your package_description variable sys_id, To get this sys_id go to "item_option_new" table and find your variable for the catalog and copy the sys_id and Replace that
     itemOption.query();
     while (itemOption.next()) {
         current.field_name = itemOption.value; // Replace the field_name with your table define field name
     }

    storeDepdentdentSysId = '';
 }
   
--------------------------------------------------------Code End ------------------------------
You will definitely thank me after using my code.
 
Please accept my solution and give it a thumbs up if it's helpful for you.
Thanks & Regards,
Prashant

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@leport 

you want to update variable with Field A value.

But in your script you are setting package_description field on Table XYZ

Assumptions

-> if your variable name is package_description and field u_requested_item holds RITM sysId then use this script

-> if your variable is string type and package_description on Table XYZ is also string type

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var gr = new GlideRecord('sc_item_option_mtom');
    gr.addQuery('request_item', current.u_requested_item);
    gr.addQuery('sc_item_option.item_option_new.name', 'package_description');
    gr.query();
    if (gr.next()) {
        var itemOptionRec = gr.sc_item_option.getRefRecord();
        itemOptionRec.value = current.package_description;
        itemOptionRec.update();
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

PrashantT
Giga Guru

Hi @leport  ,

 

I have modified your code. If you follow my new code and replace the sys_id of your variable & field name then you can achieve this requirement.

Note: Please check my code before using this code

 

Here the code:

 

 var gr = new GlideRecord('sc_item_option_mtom');
 gr.addQuery('request_item', current.u_requested_item);
 gr.query();
 while (gr.next()) {
     var storeDepdentdentSysId = gr.sc_item_option.toString();

     var itemOption = new GlideRecord('sc_item_option');
     itemOption.addQuery('sys_id', storeDepdentdentSysId);
     itemOption.addQuery('item_option_new''sys_id'); // Replace the sys_id value to your package_description variable sys_id, To get this sys_id go to "item_option_new" table and find your variable for the catalog and copy the sys_id and Replace that
     itemOption.query();
     while (itemOption.next()) {
         current.field_name = itemOption.value; // Replace the field_name with your table define field name
     }

    storeDepdentdentSysId = '';
 }
   
--------------------------------------------------------Code End ------------------------------
You will definitely thank me after using my code.
 
Please accept my solution and give it a thumbs up if it's helpful for you.
Thanks & Regards,
Prashant

leport
Tera Contributor

Thank you!  I ended up adding the following script to a BR on the source table:

 

(function executeRule(current, previous /*null when async*/) {

    // Assuming 'u_source_table_field' is the field in the current table that triggers the update
    // And 'target_variable_name' is the variable in the RITM to be updated
    if (current.u_package_description.changes() || current.u_requested_item.changes()) {
        var grRITM = new GlideRecord('sc_req_item');
           grRITM.addQuery('sys_id', current.u_requested_item);
        grRITM.query();
        if (grRITM.next()) {
            grRITM.variables.package_description = current.u_package_description
            grRITM.update();
        }
    }

})(current, previous);