How can i update RITM Variables using scripted rest API.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 08:47 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 11:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 09:29 PM - edited 12-02-2023 09:41 PM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 06:22 AM - edited 12-03-2023 06:23 AM
Hi @Emp 53 ,
- 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.
- 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.
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 07:17 AM
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