Autopopulate variable value using Ui Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 02:05 AM
I am working on a requirement. Using UI Action creating a request from an Incident. Once we click on the UI Action a request will be created and incident will be cancelled. This portion is working.
There are 3 variables on the catalog item for example abc, def and xyz. Catlog item is in global application scope and ui action is in different application scope.
abc should be auto populated as per the incident short description, def should be auto populated as per the incident description and xyz should be auto populated as per the incident urgency. This portion is not working. Variables are not even populating.
Below is the script I have written:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 02:20 AM
It seems like you're trying to set the values of variables (abc, def, xyz) on a requested item (sc_req_item) based on fields from an incident (current), but there are a couple of issues in your script.
- You're not querying for the requested item correctly.
- You need to use the correct variable names to set values on the requested item.
Here's the corrected script:
(function executeRule(current, previous /*null when async*/) {
try {
var requestGr = new GlideRecord('sc_request');
requestGr.initialize();
requestGr.requested_for = current.caller_id;
var requestID = requestGr.insert();
var itemGr = new GlideRecord('sc_req_item');
itemGr.initialize();
itemGr.request = requestID;
itemGr.cat_item = 'sysid of the catalog item'; // Replace 'sysid of the catalog item' with the actual sys_id of your catalog item
itemGr.requested_for = current.caller_id;
var itemID = itemGr.insert();
// Update the RITM with the request summary, description, and urgency
itemGr.variables.abc = current.short_description; // Assuming 'abc' is the correct variable name on your catalog item
itemGr.variables.def = current.description; // Assuming 'def' is the correct variable name on your catalog item
itemGr.variables.xyz = current.urgency.getDisplayValue(); // Assuming 'xyz' is the correct variable name on your catalog item. Use getDisplayValue() for choice variables.
itemGr.update();
} catch (ex) {
gs.error('Error occurred: ' + ex);
}
})(current, previous);
Make sure to replace 'sysid of the catalog item' with the actual sys_id of your catalog item and ensure that the variable names (abc, def, xyz) are correct according to your catalog item definition. Also, note the usage of current.urgency.getDisplayValue() to get the display value of the urgency field.
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 02:58 AM
Thanks for your reply. I have tried this but not working.
Regards,
Abhisek Chattaraj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 02:32 AM
the way in which you are creating REQ and RITM is wrong.
you should use Cart API to generate REQ and RITM
try this
try {
var cart = new sn_sc.CartJS();
var item = {
'sysparm_id': 'sysid of the catalog item',
'sysparm_quantity': '1',
'variables': {
'abc': current.short_description,
'def': current.description,
'xyz': current.urgency,
}
};
var cartDetails = cart.addToCart(item);
var checkoutInfo = cart.checkoutCart();
var parsedData = JSON.parse(checkoutInfo);
var reqSysId = parsedData.request_id;
var gr = new GlideRecord("sc_request");
gr.addQuery("sys_id", reqSysId);
gr.query();
if (gr.next()) {
gr.requested_for = current.caller_id;
gr.update();
}
var gr1 = new GlideRecord("sc_req_item");
gr1.addQuery("request", reqSysId);
gr1.query();
if (gr1.next()) {
gr1.requested_for = current.caller_id;
gr1.update();
}
} catch (ex) {
gs.info('Exception' + ex);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 02:46 AM - edited 06-14-2024 02:53 AM
Thanks for your reply.
I have tried with this script, but UI Action is not working while clicking on that.
In the script it is showing 'cartDetails' is declared but its value is never read.
The catalog item is in global application scope and the UI Action is in different application scope.
Is this the reason?
Regards,
Abhisek Chattaraj.