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

Muralidharan BS
Mega Sage
Mega Sage

Hi Adarsh,

This can be done easily in the flow designers using decision tables. There are oob actions already to create catalog task. You have to just create a decision with branches each for one location and that's the catalog task will be assigned to a specific assignment group. 

https://docs.servicenow.com/bundle/sandiego-application-development/page/administer/flow-designer/concept/flow-logic-make-decision.html

this should be very easy than scripting,

Thanks

Hi Murali,

I have almost completed this task in the workflow editor and the only pending task is to populate the assignment group which is mentioned. It would be great if you can help me with the scripting or something which can be done inside my existing workflow.

Attaching my workflow.

can you try this, 

sct.assignment_group input is the country of the user. I have used as options.country, make changes to the right field. 

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.getDisplayValue('country')); 
sct.request_item = current.sys_id;
sct.insert();
}

function getAssignmentGroup(country){
   switch(true){
   case country == 'UK':
    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; 
}
}

 

Also, the country name which I have added is generic, so modify to right name and add the sysid of the group. 

Thanks

Hi Murali,

Thanks for the update. 

I have a few queries in your code:

The output for the value data1 is sys_id of the users which we have selected in the requested-for variable.

 

Do we need to Glidrecord the sys_user table separately to get the location of the selected user or what is expected to be added here in the place of 'country' 

sct.assignment_group = getAssignmentGroup(options.getDisplayValue('country')); 

Can you please be specific here? the backend name of the location field in sys_user table is 'location' 

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.getDisplayValue('country')); 
sct.request_item = current.sys_id;
sct.insert();
}

function getAssignmentGroup(country){
   switch(true){
   case country == 'UK':
    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; 
}
}