mak1A4
Tera Guru

I wasn't really happy with the Solutions I have found on the community for this issue.
With the old way of doing this (GlideappCalculationHelper.addItemToExistingRequest) you have to populate the variables manually after the requested item has been inserted. So here is a solution to add a catalog item to an existing requested item by using the CartJS API.

Script Include

var addCatalogItemToRequest = function (requestSysId, catItemMap, startFlow) {
	var tempCartName = "temp_" + gs.generateGUID();
	var tempCart = new sn_sc.CartJS(tempCartName);
	var cartItem = tempCart.addToCart(catItemMap).items[0];

	var cartItemHint = "<hints><entry key='sysparm_processing_hint' value='" +
		"setfield:request=" + requestSysId + "'/></hints>";

	var grCartItem = new GlideRecord("sc_cart_item");
	if (grCartItem.get(cartItem.cart_item_id)) {
		grCartItem.setValue("hints", cartItemHint);
		grCartItem.update();
	}
	//a business rule on sc_request will abort insert with these special_instructions
	tempCart.submitOrder({
		'special_instructions': '[__ABORT_INSERT_ACTION__]'
	});

	var getVars = function (grReqItem) {
		var vars = {};
		for (var n in grReqItem.variables) {
			vars[n] = grReqItem.variables[n];
		}
		return vars;
	};

	var grReqItem = new GlideRecord("sc_req_item");
	grReqItem.addEncodedQuery("request=" + requestSysId);
	grReqItem.orderByDesc("sys_created_on");
	grReqItem.setLimit(1);
	grReqItem.query();
	if (grReqItem.next()) {
		if (startFlow) {
			//as long as the OOTB business rules "Start Workflow" and "Start FlowDesigner Flow"
			//aren't modified setting the stage to "request_approved" should start the (work)flow
			grReqItem.stage = "request_approved";
			grReqItem.update();
		}
		return grReqItem;
	}
};

Sample Call of Script Include

var requestId = 'd1eab3138741cd141820ed309bbb35b6'; //SysID of existing request
addCatalogItemToRequest(requestId, {
    'sysparm_id': 'd6ea58701b1c41105ff0c036464bcb73', //SysID of catalog item
    'sysparm_quantity': '1',
    'variables': { //variables of catalog item
        'type': 'Systemcopy',
        'selectdatetime_immediately': 'true',
        'additional_comments': 'testscript'
    }
});

Business Rule

Table: sc_request
When: Before insert
Order: 0
Condition: current.special_instructions == "[__ABORT_INSERT_ACTION__]"

(function executeRule(current, previous /*null when async*/) {

	//Business Rule will prevent creation of a request
	//if the special_instructions field is set to [__ABORT_INSERT_ACTION__]
	//This is used to add RITMs to an already existing Request while still
	//using the OOTB CartJS API.
	current.setAbortAction(true);

})(current, previous);
Comments
NFGDom
Mega Sage

Thank you so much for this! I have this setup with the flow designer as an action.

I noticed that the new catalog item's flow does not seem to be executing. Any thoughts?

call to script include (as action):

find_real_file.png

Flow where action is called:

find_real_file.png

New RITM created successfully:

find_real_file.png

But no flow context:

find_real_file.png

Any thoughts or help would be appreciated. Let me know if I can provide more information!

mak1A4
Tera Guru

Hello, you're welcome : )
If you want to start the item flow you have to pass an additional parameter to the script include.

var requestId = 'd1eab3138741cd141820ed309bbb35b6'; //SysID of existing request
addCatalogItemToRequest(requestId, {
    'sysparm_id': 'd6ea58701b1c41105ff0c036464bcb73', //SysID of catalog item
    'sysparm_quantity': '1',
    'variables': { //variables of catalog item
        'type': 'Systemcopy',
        'selectdatetime_immediately': 'true',
        'additional_comments': 'testscript'
    }
}, true); //passing true as the third parameter should start the flow

 

NFGDom
Mega Sage

Thank you!

When I add the third parameter I get the following error:

find_real_file.png

find_real_file.png

I did verify that the flow for the catalog item I'm calling is published & the catalog item has that flow selected under process engine.

find_real_file.pngfind_real_file.png

mak1A4
Tera Guru

Sorry there was a bug in the script include, you have to change line 26 to the following:

var flow = grReqItem.cat_item.flow_designer_flow;

I have updated the article accordingly.

NFGDom
Mega Sage

Thanks so much! Everything is working as one would expect now. This will help us tremendously building out some of our flows.

All the best,

Dom

NFGDom
Mega Sage

Hey there!

Curious, would this work for a catalog item that uses the workflow and not Flow Designer Flow? The one I have setup I'm getting the following:

find_real_file.png

I think this makes sense because the catalog item I'm submitting does not have a flow... but a workflow.

find_real_file.png

Any chance this could work with both (meaning one or the other)?

Thanks in advance!
Dom

mak1A4
Tera Guru

Hello Dom, I updated the Script in the Article it should now work for both.
I have not tested it, more or less just copied the content of the "Start Workflow" Business Rule. Would be great if you could let me know if it worked, thanks.

NFGDom
Mega Sage

Thanks Martin!

I receive the following:

find_real_file.png

Looks like the id variable on line 41 isn't defined.

Edit: looks like it needs to be the sysid of the workflow that is supposed to be submitted.

Edit2:

I added this and my quick test seems that it is working as expected. I'll report back if I run into more issues.

var id = grReqItem.cat_item.workflow.toString();

It looks like this now:

else if (!grReqItem.cat_item.workflow.nil() && grReqItem.context.nil()) {
     var w = new Workflow();
     var id = grReqItem.cat_item.workflow.toString();
     var context = w.startFlow(id, grReqItem, "update", getVars(grReqItem));
     if (context != null) grReqItem.context = context.sys_id;
     grReqItem.update();
}
mak1A4
Tera Guru

Hello Dom, so I ran into an issue myself. I ordered a catalog item which as first flow action immediately uses the requested item record to query the variables. This always gave me an "invalid record" exception. I guess it's a timing issue, but I don't like to put in a "wait for X seconds" action somewhere. So the new solution I came up with to start the flow (or workflow) is to just update the requested item stage to "request_approved", as long as the OOTB business rules for triggering flow/workflow are unmodified this seems to be the best solution. I have updated the article accordingly.

NFGDom
Mega Sage

Just did a quick test - it is setting everything up correctly at least with the variables and setting the stage. But I don't see the workflow "running" with a context record. What I had before was working for me, I didn't seem to get an invalid record... Interesting...

I should say that our BRs are OOB too

NFGDom
Mega Sage

Hey there,

Just wanted to come back and confirm; I moved this over to my PDI and tested a catalog item that calls another with a Flow Designer Flow and Workflow. Everything is created correctly and passed over correctly (as far as variables and parent REQ is concerned). For the new item with Flow Designer Flow it starts correctly under the original REQ and there is a flow context. For the workflow item it does not seem to start the workflow and there is no flow context. This is with the OOB BRs mentioned. I'm not sure why its not starting the workflow for me.

I then reverted back to the script include before the latest change (where you pulled the start workflow BR). Doing the same test everything works as I would expect it too. I didn't encounter an "Invalid Record" exception. All of my Service Catalog Flows have a trigger of "Service Catalog" then my first action is a "Get Catalog Variables". Followed by any logic I want from there.

find_real_file.png

Let me know if I can provide more details for you.

Jacob Nan
Tera Guru

The autonumber of requests is skipping in-between requests. Is this expected or have I setup the business rule incorrectly? Thanks for the awesome script!
find_real_file.png

Babu Nukala
Tera Explorer

Hi @mak1A4  Thank you for the post. I have a use case where a user receives a notification upon a custom request (Ex: ITHRW1234) creation via trigger (Flow). The notification contains a link to a catalog item. When user submits the catalog item, the newly created RITM should be assigned to the request created earlier(in our case it is ITHRW1234).

 

How to proceed forward with your solution for my use case. 

Version history
Last update:
‎01-20-2022 04:28 AM
Updated by: