How to copy the variable field value in a Catalog Form to the Requested Item table?

Happy S
Tera Expert

I am trying to copy the field value for a variable - Requested For under a Catalog Form to the Requested Item - Requested For field.

 

My understanding is that the variable field - Requested For is in the Table sc_item_option_mtom but when I clicked on the Dependent Item for the field, it opened up into a new table - sc_item_option and it shows something like below.

 

HappyS_1-1696655404911.png  

 

HappyS_3-1696655564597.png

 

 

The value refers to the user's sys_ID which I wanted to copy over to the field Requested For on the sc_req_item table.

 

HappyS_2-1696655498613.png

 

 

 

Tried with the code below in Business Rules but its not working..

 

 

(function executeRule(current, previous) {
    // Get the sc_req_item sys_id
    var reqItemId = current.sys_id;

    // Get the sc_item_option_mtom table for related records
    var itemOptionMtom = new GlideRecord('sc_item_option_mtom');
    itemOptionMtom.addQuery('request_item', reqItemId);
    itemOptionMtom.addQuery('variable.name', 'common_ReqFor');
    itemOptionMtom.query();

    // Get related records
    if (itemOptionMtom.next()) {
        // Get the value of 'common_ReqFor' from the related record
        var commonReqForValue = itemOptionMtom.value;

        // Update requested_for field in the sc_req_item record with 'common_ReqFor' value
        current.requested_for = commonReqForValue;

        // Update sc_req_item record
        current.update();
    }
})(current, previous);

 

 

Appreciate if someone can help me on this.. 

2 REPLIES 2

Sohithanjan G
Kilo Sage

Hi @Happy S 

Creation BR is not the appropriate solution to copy variable value to RITM field name. Instead you can try using run script and set the value accordingly in the following way in the workflow.

current.field_name= current.variables.variable_name;


Please mark as Accepted Solution & HIT helpful.


BR, Sohith

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Vishal Birajdar
Giga Sage

Hi @Happy S 

 

As my understanding, you want to set "Requested For" field on RITM with "Requested For" coming from Variable 

 

Below things you need to consider

1.There are two Requested For fields on RITM

  1. Requested For - Local to RITM table (which OOTB in not present on RITM form)
  2. Requested For - Present on Request table & displayed on RITM form request.requested_for

 

So you can modify your script like below: 

 

(function executeRule(current, previous) {

    /*Get the Request sys id */
    var request = current.request;
   
     /*Glide record on Reuest table */
    var grReq = new GlideRecord('sc_request');
    grReq.addQuery('sys_id',request);
   grReq.query();
   if(grReq.next()){
        grReq.requested_for = current.variables.<variable_name>;
        grReq.update();
    }
    /*Update the requested for from ritm form */
    current.requested_for = current.variables.<variable_name>;

})(current, previous);

 

You can do this using workflow also.

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates