Unable to populate Requested for.location in SCTASK Location field when Requested for in SCTASK changes

Gian1
Tera Contributor

We have a requirement that when the SCTASK requested for field changes the job location field gets populated base on the the requested for location. This does not work. Any ideas or any help will be much appreciated.

I have created a business rule on the sc_request table;

update is true

Filter conditions

Requested for changes

Script;

 

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

    var ritm = new GlideRecord('sc_req_item');
    ritm.get(current.getValue('table_sys_id'));

    //if return something
    if (ritm == true) {

        var sctask = new GlideRecord('sc_task');
        sctask.addQuery('request_item', current.getValue('table_sys_id'));
        sctask.query();
        while (sc_task.next()) {
            current.location = current.request.requested_for.location;
 sctask.update();
        }
    }

})(current, previous);

1 ACCEPTED SOLUTION

So the code needs slight adjustment if the requested for field on your sc_task is from sc_req table. This business rule will need to be created on the sc_req table, as the requested for field will be changing there when changed on the sc_task dot walked field.

(function executeRule(current, previous /*null when async*/) {
	
	var userChange = current.requested_for.location;
	var requestSysid = current.getUniqueValue();

	var scTaskRecord = new GlideRecord('sc_task');
	scTaskRecord.addQuery('request', requestSysid);
	scTaskRecord.query();
	while(scTaskRecord.next()){
		scTaskRecord.location = userChange;
		scTaskRecord.update();
	}

})(current, previous);
If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

View solution in original post

5 REPLIES 5

Zach Koch
Giga Sage
Giga Sage

So there are a few things wrong with your code. First off, you have if (ritm == true), ritm will never equal true, it will either be undefined or a GlideRecord, but never the value true, so your block of code will not run. Second, in your while loop, you are using current.location, if you are trying to update the sc_task GlideRecord each loop, you need to change that to sctask.location.  With these changes in mind, your script would look like.

(function executeRule(current, previous /*null when async*/) {
	
	var userChange = current.requested_for.location;
	var requestItem = current.getUniqueValue();

	var scTaskRecord = new GlideRecord('sc_task');
	scTaskRecord.addQuery('request_item', requestItem);
	scTaskRecord.query();
	while(scTaskRecord.next()){
		scTaskRecord.location = userChange;
		scTaskRecord.update();
	}

})(current, previous);

If this resolved your issue, please mark this correct and helpful, thank you!

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Gian1
Tera Contributor

Hi Zach,

I updated my code and its still not working for some reason.

Do I need to query the user table to get the location or that's not necessary

attached are my settings to when to run the rule;

 

I updated the code in the previous response. You need to set the BR to trigger on the sc_req_item table, as the Requested For field on the sc_task table is a dot walked field from the sc_req_item table, so by changing it on the sc task, it will change the Requested For on the RITM. I wasn't sure from your requirements, but the script will set the sc_task location to the Requested For user's location, but it doesn't update the RITM or REQ location, were you needing those set as well? If so let me know and I can adjust the script.

 

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Gian1
Tera Contributor

Thanks for the modified code. 

It's still not working, but I have a feeling it's the way SCTASK requested for is set up.

Below is our sctask requested for field.

It's dot walking from sc_request

the SCTASK job location field does not depend on anything it just references the cmn_location table 

So pretty much this is what our requirements

When we change the sctask requested for field, when we save the case it should populate the sc task job location field.