How can i update RITM Variables using scripted rest API.

Rama26
Tera Contributor

HI All,

I have a catalog item in that i using 10 variables to create the RITM.  using scripted rest api i am trying to update variables in RITM but not working

below is the example data i am passing in json

{

"short_description":"test",

"variables":{

          "u_environment": "123",

        "account": "456",

        "provider": "aws"

}

} for updating data  used below script 

var reqItem = new GlideRecord('sc_item_option_mtom');
reqItem.addQuery('request_item.number', 'RITM1100973');
reqItem.query();

while (reqItem.next()) {
    var details = reqItem.sc_item_option.item_option_new.getDisplayValue();
    gs.info('Details: ' + details);
}

4 REPLIES 4

Community Alums
Not applicable

Hey @Rama26 ,

 

Firstly, verify whether your RITM update code is working or not. You can refer following script:

var reqItem = new GlideRecord('sc_item_option_mtom');
reqItem.addQuery('request_item.number', 'RITM1100973');
reqItem.query();

while (reqItem.next()) {
  reqItem.setValue('variables.u_environment', '123');
    reqItem.setValue('variables.account', '456');
    reqItem.setValue('variables.provider', 'aws');

    // Update the RITM record
    ritm.update();

  reqItem.update();
}

 Then, you can pass the API response data as variable values.

 

If you found this helpful, a 'like' is the secret handshake of appreciation!

Prasad

i tried with above solution but not working as expected.

below is the code i used in scripted rest api. but updating data in variables business rule restricting

 

var ritm = request.pathParams.number;
var request_body = request.body.nextEntry();

var variables = request_body.variables;
 var ritmGR = new GlideRecord("sc_req_item");
 ritmGR.get("number", ritm);

 ritmGR.description = request_body.description;
 for (var key in variables) {
 if (variables.hasOwnProperty(key) && typeof ritmGR.variables[key] !== 'undefined') {
                ritmGR.variables[key] = variables[key];
            }
        }
        ritmGR.update();

Community Alums
Not applicable

Hi @Emp 53 ,

 

  1. Business Rule Check: Review the Business Rule associated with the sc_req_item table that might be preventing the update of variables. Look for any conditions or scripts that could be blocking the update.
  2. Check Field-Level Security: Ensure that the fields you're trying to update via the REST API are not restricted through Field-Level Security. Verify the permissions for these fields.
  3. Business Rule Modification: If you're certain the Business Rule is the bottleneck, modify it to allow the update under the necessary conditions. If it's a constraint for security or other reasons, consider involving system administrators or stakeholders to reassess the restriction.


Don't forget to hit the like button!

 

regards,

Prasad

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Rama26 ,
I trust you are doing great.
Below is a revised version of your script tailored for updating variables:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // Parse the incoming JSON payload
    var requestBody = request.body.dataString;
    var jsonData = JSON.parse(requestBody);
    var variables = jsonData.variables;

    // Define the RITM number to update
    var ritmNumber = 'RITM1100973';

    // Query the sc_item_option_mtom table for the given RITM
    var reqItem = new GlideRecord('sc_item_option_mtom');
    reqItem.addQuery('request_item.number', ritmNumber);
    reqItem.query();

    while (reqItem.next()) {
        // Get the variable name and its current value
        var varName = reqItem.sc_item_option.item_option_new.name;
        var currentValue = reqItem.sc_item_option.item_option_new.getDisplayValue();

        // Check if the variable is in the incoming JSON and needs an update
        if (variables.hasOwnProperty(varName)) {
            var newValue = variables[varName];

            // Update the variable value if it's different
            if (newValue !== currentValue) {
                reqItem.sc_item_option.item_option_new.setDisplayValue(newValue);
                reqItem.update();
                gs.info('Updated variable ' + varName + ' to ' + newValue);
            }
        }
    }

})(request, response);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi