Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

If condition based on User location

Keerthi Lakshmi
Tera Contributor

I have a request where I have to give if condition which checks the location of the user and if it is UK or Ireland, or Poland, or Spain or Portugal then it should create a new task and assign to appropriate group based on user's location.  There is already a workflow associated with this item and i have to insert this condition here. Can anyone help me to achieve this. I have tried to give the condition in the filter 

 

like item.location.country=United Kingdom but it does not work. Do i have to give the script? Can anyone help me ?

2 REPLIES 2

Maddysunil
Kilo Sage

@Keerthi Lakshmi 

I think you use below approach

  1. Identify User's Location: You need to determine how you're capturing the user's location. If it's stored in a field on the item (request), make sure you're referencing the correct field.

  2. Create a Condition in the Workflow: In the workflow associated with the item, find the appropriate step where you want to insert the condition. Typically, this would be the "Create Task" step.

  3. Define the Condition: Instead of using the Filter Condition builder, you might need to write a script condition to accurately check the user's location

 

var userLocation = current.item.location.country; // Assuming 'location' is the field storing the user's location

// Check if the user's location is one of the specified countries
if (userLocation == 'United Kingdom' || userLocation == 'Ireland' || userLocation == 'Poland' || userLocation == 'Spain' || userLocation == 'Portugal') {
    return true; // Create a new task
} else {
    return false; // Do not create a task
}

 

Replace current.item.location.country with the correct field reference to the user's location in your instance.

  1. Assign the Task to the Appropriate Group: After the condition is met, you can configure the workflow to create a task and assign it to the appropriate group based on the user's location.

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @Keerthi Lakshmi 

In the part of your workflow where you need to check the user’s location, you might add a Scripted Condition or a Run Script action,

 

(function executeScript(current, previous /null when async/) {

    var userGr = new GlideRecord('sys_user'); 
    userGr.get(current.assigned_to);
    
    // Check the user’s location
    if (userGr.location.getDisplayValue() == "United Kingdom" ||
        userGr.location.getDisplayValue() == "Ireland" ||
        userGr.location.getDisplayValue() == "Poland" ||
        userGr.location.getDisplayValue() == "Spain" ||
        userGr.location.getDisplayValue() == "Portugal") {
        
        // Logic to create and assign a new task based on location
        var taskGr = new GlideRecord('task'); // Or a more specific table if applicable
        taskGr.initialize();
        taskGr.short_description = "Task for " + userGr.location.getDisplayValue();
        // Set other fields as necessary, like ‘assigned_to’
        // This could include a switch or if-else statements to set the group based on location
        taskGr.insert();
        
        return true; // Indicates the condition was met, if needed
    } else {
        return false; // Condition not met
    }
})();

 

Inside your script where the comment indicates “// Logic to create and assign a new task based on location,” you can implement further logic to decide which group to assign the task to based on the user’s location. This might involve a series of if or switch statements comparing userGr.location.getDisplayValue() to each country and setting the assigned_to field accordingly, possibly by looking up the correct group in a GlideRecord query.
Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma