set assignment group based on user location by using workflow

sainath reddy
Tera Contributor

I have to write script to get assigned the assignment group based on user location.if the user is based on beijing location,the beijing assignment group should get auto populated in the assignment group.I'm having so many users from different locations and having different assignment group.how to wirte the script,anyone help me.the field names are location and assignment group from form.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sainath reddy 

create a system property which will hold json string

then in your workflow get the value of that property.

then get user's location

then parse the JSON and get the group for this location and set it

Example:

[
{
"location": "Beijing",
"group": "Group 1 SysId"
},
{
"location": "India",
"group": "Group 2 SysId"
}
]

Workflow run script

var userLocation = ''; // get the user's location based on your logic
var parsedData = JSON.parse(gs.getProperty('propertyName'));
var group;
for(var i=0;i<parsedData.length;i++){
	if(parsedData[i].location == userLocation){
		group = parsedData[i].group;
		break;
	}
}
current.groupField = group;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

11 REPLIES 11

Anil Lande
Kilo Patron

Hi,

Do you have mappings [Location-> Assignment group] stored anywhere in your instance?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Community Alums
Not applicable

@sainath reddy --

 

  1. Create a new table to store the mapping between locations and assignment groups. This table should have two fields: Location and Assignment Group. Populate this table with the relevant mappings.

  2. Populate the Assignment Group Mapping table:

    • Populate the "Assignment Group Mapping" table with the appropriate mappings between locations and assignment groups. For each location, specify the corresponding assignment group.
  3. Configure the workflow stages and activities:

    • Add a new "Activity" to the workflow.
    • Set the "Activity Type" to "Script".
    • In the script, you will need to fetch the user's location and set the assignment group accordingly. Use the "current" variable to access the current record.
    • Here's an example of a script that sets the assignment group based on the user's location (assuming you have a field named "location" on the User table):

// Get the user's location

var userLocation = current.user.location;

// Query the Assignment Group Mapping table

var assignmentGroupMapping = new GlideRecord('u_assignment_group_mapping');

assignmentGroupMapping.addQuery('location', userLocation);

assignmentGroupMapping.query();

// Set the assignment group based on the mapping

if (assignmentGroupMapping.next())

{

current.assignment_group = assignmentGroupMapping.assignment_group;

}