- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 01:40 PM
Hi Everyone,
I have an existing inbound action with the following code:
var grREQ = new GlideRecord('sc_request');
grREQ.requested_for = '165c7f9adbbfa340a9423ebf9d9619a6';
grREQ.short_description = 'Test_8';
grREQ.description = 'Test_8';
//grREQ.parent = current.sys_id;
var reqSysID = grREQ.insert();
var ritm = new GlideRecord('sc_req_item');
ritm.short_description = 'Test_8';
ritm.description = 'Test_8';
ritm.comments = 'Test_8';
ritm.cat_item = '7f8b78b9db19738002b63ebd7c96198b';
ritm.request = reqSysID;
ritm.state = 1;
//Workflow Trigger
var w = new Workflow();
wfSysId = w.getWorkflowFromName("Infosec Approval");
w.startFlow(wfSysId, ritm, ritm.operation());
ritm.insert();
This is working for inbound action, however, we need to change our process and submit or create this catalog item via business rule and when I used this exact script on BR, it's creating REQ, RITM but the workflow is not attaching. Do you have an idea why this happens? Thank you.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2020 08:18 AM
Use below code with cart API to add comments in RITM.
Make sure to pass your catalog item sys_id in cart
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('f8861cbedbed73008d73771c8c961961');
var rc = cart.placeOrder();
var req = new GlideRecord('sc_req_item');
req.addQuery('request',rc.sys_id);
req.query();
while(req.next()){
req.comments = "test comm";
req.update();
}
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2020 08:38 AM
Unfortunately you cannot use Cart API to update/set any fields on RITM or REQ
you will have to query and update it as mentioned by Sachin
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 02:37 PM
Using Catalog Cart API is recommended option as suggested by Sachin.
However if you want to use your code , I have modified a slightly and it should work.
var grREQ = new GlideRecord('sc_request');
grREQ.requested_for = 'cdb1b5c41b67d0902181ddb9ec4bcb37';
grREQ.short_description = 'Test_8';
grREQ.description = 'Test_8';
//grREQ.parent = current.sys_id;
var reqSysID = grREQ.insert();
var ritm = new GlideRecord('sc_req_item');
ritm.short_description = 'Test_8';
ritm.description = 'Test_8';
ritm.comments = 'Test_8';
ritm.cat_item = '7f8b78b9db19738002b63ebd7c96198b';
ritm.request = reqSysID;
ritm.state = 1;
var item = ritm.insert();
var wf = new GlideRecord('sc_req_item');
wf.get(item);
var w = new Workflow();
wfSysId = w.getWorkflowFromName("Infosec Approval");
w.startFlow(wfSysId, wf, 'update');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 11:12 PM
Hi Mihir,
Thanks for your response. Tried this but still not working.
Regards,
Diane

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2020 08:27 AM
Hello ,
var grREQ = new GlideRecord('sc_request');
grREQ.newRecord();
grREQ.requested_for = '165c7f9adbbfa340a9423ebf9d9619a6';
grREQ.short_description = 'Test_8';
grREQ.description = 'Test_8';
//grREQ.parent = current.sys_id;
var reqSysID = grREQ.insert();
var ritm = new GlideRecord('sc_req_item');
ritm.newRecord();
ritm.short_description = 'Test_8';
ritm.description = 'Test_8';
ritm.comments = 'Test_8';
ritm.cat_item = '7f8b78b9db19738002b63ebd7c96198b';
ritm.request = reqSysID;
ritm.state = 1;
ritm.insert();
//Workflow Trigger
var w = new Workflow();
wfSysId = w.setWorkflowFromName("Infosec Approval");
w.startFlow(wfSysId, ritm, ritm.operation());
Try the Above Code.
Regards
Yash Agrawal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2020 08:35 AM
Hello,
Try the code for executing workflow from Business Rule.
function onAsync(current) {
startWorkflow();
function startWorkflow()
{
var u_company_name = "CompanyName";
var w = new global.Workflow();
var wfId = w.getWorkflowFromName("invokeCustomWorkflow");
var context = w.startFlow(wfId,current,null,u_company_name);
if (context != null)
current.context = context.sys_id;
}
}
Regards,
Yash Agrawal