Auto creation of Catalog tasks

Adarsh T
Tera Contributor

Hi,

I have a requirement to create catalog tasks based on a list collector variable i.e Requested for. I was successfully able to auto-create the catalog task after submitting the request. I followed the below reference-https://community.servicenow.com/community?id=community_question&sys_id=8d970a7adb68ef04f7fca851ca96...

I have added the below code in my workflow run script activity:


var options = current.variables.requested_for;
var data = options.toString();
var data1 = data.split(",");
for (i=0; i < data1.length; i++) {
var sct = new GlideRecord('sc_task');
sct.initialize();
sct.short_description = 'iManage installation';
sct.description = 'Check if relevant software was installed to users device otherwise assist with physical implementation';
//sct.assignment_group = ??
sct.request_item = current.sys_id;
sct.insert();
}

There is a challenge in updating the assignment group as it should be updated based on requested for location.

The values can be hardcoded as an Eg: If the user location is London then the group should be XYZ, if the location is India then the assignment group should be ABC. There are around 5 static locations and their respective group is also allocated. I tried creating a custom field called location in group table and mapped the respective location for those groups. But I am not sure how the logic would fit in the above script.

Can you help me with the solution?

 

1 ACCEPTED SOLUTION

Hi,

you should have 1 more curly bracket to close the IF

var abc= new GlideRecord('sc_req_item');
abc.addQuery("sys_id", '73dfa9a1979301106952f8b3f153af92');
abc.query();
if(abc.next()){
    var options = abc.variables.requested_for;
    var data = options.toString();
    var data1 = data.split(",");
    for (i=0; i < data1.length; i++) {

        var bbb = getAssignmentGroup(data1);
        gs.print(bbb);

    }

}
function getAssignmentGroup(user){
    var groupSysId;
    var gr = new GlideRecord("sys_user");
    gr.addQuery("sys_id", user);
    gr.query();
    if (gr.next()) {
        var country = gr.country;
        switch(country.toString()){
            case 'Brasil':
                groupSysId = '15bcdbfc975301106952f8b3f153af58';
                break;
            case country == 'France':
                groupSysId = 'fb57b129979301106952f8b3f153affd';
                break;
            default:
                groupSysId = '679434f053231300e321ddeeff7b12d8'; // if user has no country info then use this group;
        }
    }
    return groupSysId;
}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Hi Adarsh

is the requested for a reference variable or list collector? if it is a reference variable as per the oob then the below code will do the trick.

var userRec = current.variables.requested_for.country;
var sct = new GlideRecord('sc_task');
sct.initialize();
sct.short_description = 'iManage installation';
sct.description = 'Check if relevant software was installed to users device otherwise assist with physical implementation';
sct.assignment_group = getAssignmentGroup(userRec); 
sct.request_item = current.sys_id;
sct.insert();

function getAssignmentGroup(country){
   switch(true){
   case country == 'GB':
    return 'sysid of the group';
   case country == 'India':
    return 'sysid of the group';
   default:
    return 'sysid of the group'; // if user has no country info then use this group; 
}
}

 

You don't have to get the sysid of the user because the requested for is already a glide object so it contains all the information about the user. So you can just dot walk to the country field as I did above. 

You can then pass the country to the function - getAssignmentGroup

The thing to take care of is the country display value, you have to amend the country names in the switch statement as per your requirement. 

 

Thanks

 

Hi Murali,

The requested for variable is a list collector and that's where this problem arised.

As I mentioned earlier "I have a requirement to create catalog tasks based on a list collector variable i.e Requested for. I was successfully able to auto-create the catalog task after submitting the request. I followed the below reference-https://community.servicenow.com/community?id=community_question&sys_id=8d970a7adb68ef04f7fca851ca96..."

 

There is a challenge in updating the assignment group as it should be updated based on requested for location.

The values can be hardcoded as an Eg: If the user location is London then the group should be XYZ, if the location is India then the assignment group should be ABC. There are around 5 static locations and their respective group is also allocated. I tried creating a custom field called location in group table and mapped the respective location for those groups. But I am not sure how the logic would fit in the above script.

Can you help me with the solution?

Ankur Bawiskar
Tera Patron
Tera Patron

@Adarsh T 

then query for sys_user; get the country for that user

var options = current.variables.requested_for;
var data = options.toString();
var data1 = data.split(",");
for (i=0; i < data1.length; i++) {
	var sct = new GlideRecord('sc_task');
	sct.initialize();
	sct.short_description = 'iManage installation';
	sct.description = 'Check if relevant software was installed to users device otherwise assist with physical implementation';
	sct.assignment_group = getAssignmentGroup(options[i]); 
	sct.request_item = current.sys_id;
	sct.insert();
}

function getAssignmentGroup(user){
	var groupSysId;
	var gr = new GlideRecord("sys_user");
	gr.addQuery("sys_id", user);
	gr.query();
	if (gr.next()) {
		var country = gr.country;
		switch(country.toString()){
			case 'UK':
				groupSysId = 'sysid of the group';
				break;
			case country == 'India':
				groupSysId = 'sysid of the group';
				break;
			default:
				groupSysId = 'sysid of the group'; // if user has no country info then use this group; 
		}
	}
	return groupSysId;
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar 

Before I added this code to my workflow, I tried running a background script to verify it:

I just added the necessary sys_id's for the group and executed the code. But it is throwing me an error stating - 

Evaluator: com.glide.script.RhinoEcmaError: getAssignmentGroup is not a function.
   script : Line(10) column(0)
      7: var data1 = data.split(",");
      8: for (i=0; i < data1.length; i++) {
      9: 	
==>  10: var bbb = getAssignmentGroup(data1[i]); 
     11: gs.print(bbb);
     12: 
     13: }


Below is m code:
var abc= new GlideRecord('sc_req_item');
abc.addQuery("sys_id", '73dfa9a1979301106952f8b3f153af92');
abc.query();
if(abc.next()){
var options = abc.variables.requested_for;
var data = options.toString();
var data1 = data.split(",");
for (i=0; i < data1.length; i++) {

var bbb = getAssignmentGroup(options[i]);
gs.print(bbb);

}

function getAssignmentGroup(user){
var groupSysId;
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user);
gr.query();
if (gr.next()) {
var country = gr.country;
switch(country.toString()){
case 'Brasil':
groupSysId = '15bcdbfc975301106952f8b3f153af58';
break;
case country == 'France':
groupSysId = 'fb57b129979301106952f8b3f153affd';
break;
default:
groupSysId = '679434f053231300e321ddeeff7b12d8'; // if user has no country info then use this group;
}
}
return groupSysId;
}
}

Inspite of options[i] value, I change it to data1[i] as well but doesn't seems to work.
==>  10: var bbb = getAssignmentGroup(options[i]);
==>  10: var bbb = getAssignmentGroup(data1[i]);

Please do let me know whats the issue do highlight if I am making any mistake.

 

Regards,

Adarsh

Hi,

you need to send data1[i] as data1 is an array

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader