How to change Target URL in Change Interceptor based on logged in user's mail id dynamically

Akila Ravichand
Tera Contributor

Hello Everyone,

 

Is there an option to change the target URL dynamically in Change Interceptor (sys_wizard_answer table), based on logged in user mail id.

For example:

I have created two templates -> testuser_template, exampleuser_template

If the user mail id contains @test.com, then the URL should map to testuser_template in sysparam_template -> query string

If the user mail id contains @Example.com, then the URL should map to exampleuser_template in sysparam_template ->query string

 

The target URL in the below should get changed dynamically based on the above conditions.

AkilaRavichand_0-1750179536555.png

 

Regards,

Akila

 

2 REPLIES 2

Abbas_5
Tera Sage
Tera Sage

Hello @Akila Ravichand,

 

To dynamically change the Target URL in a ServiceNow Change Interceptor based on the logged-in user's email, you can utilize a script include and reference it within the Target URL field of the interceptor's answerThe script include will retrieve the user's email, determine the appropriate URL based on that email, and return the dynamic URL. 
 
Here's a breakdown of the process: 
  1. Create a Script Include:
    • Navigate to System Definition > Script Includes.
    • Create a new script include (e.g., ChangeInterceptorHelper).
    • Set Accessible from to "All" or a specific scope if needed.
    • Set Client callable to "false" (unless you need client-side access).
    • Add the following script (replace with your specific logic for determining the URL based on email):
JavaScript
     var ChangeInterceptorHelper = {         getDynamicTargetUrl: function() {             var userEmail = gs.getUser().getEmail();             var targetUrl = '';             // Example logic:             if (userEmail.endsWith('@example.com')) {                 targetUrl = 'https://instance.service-now.com/nav_to.do?uri=%2F$m2m_change_request_form.do%3Fsysparm_stack=no%26sysparm_view=ess';             } else if (userEmail.endsWith('@example.net')) {                 targetUrl = 'https://instance.service-now.com/nav_to.do?uri=%2F$m2m_change_request_form.do%3Fsysparm_stack=no%26sysparm_view=itil';             } else {                 targetUrl = 'https://instance.service-now.com/nav_to.do?uri=%2F$m2m_change_request_form.do%3Fsysparm_stack=no'; // Default URL             }             return targetUrl;         }     };
  1. Modify the Interceptor:
     
    • Navigate to System Definition > Interceptor. 
       
    • Open the Change Request interceptor you're using. 
       
    • Go to the "Answers" related list. 
       
    • Open the relevant answer record.
       
    • In the "Target URL" field, replace the existing URL with:
       
Code
     javascript:new ChangeInterceptorHelper().getDynamicTargetUrl()
Replace ChangeInterceptorHelper with the name of your script include. 
 
  1. Test the Solution:
    • Log in with different user accounts (with different email domains, if applicable).
    • Trigger the interceptor (e.g., by clicking a "Create Change" button).
    • Verify that the Target URL is correctly set based on the user's email.
      If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in future it will be helpful to them.
      Thanks & Regards,
      Abbas Shaik

Hello @Abbas_5 ,

 

Thanks for your response. I have tried it and unfortunately, it is not working as expected.

The script includes returns the URL based on user email. But when the same is used in the Target URL, it is not redirecting to any page and stays in the interceptor page itself.

 

I also trying using the script include in Script field and removed the details from Target URL. Still it was same.(in this scenario, it redirects to a blank page).

AkilaRavichand_0-1750244959759.png

 

AkilaRavichand_1-1750245006822.png

(I tried using your sample code too, for the different URLs, and the result was still the same).

 

Regards,

Akila