How to prefill variables of a record producer

Ruhi Jibhyenkar
Tera Contributor

Hi All,

The requirement is when a Request is closed and the email is sent, we need to provide an option(button) in the email to the requestor to create an incident from the esc portal via a custom record producer, link the request in the incident(pre-fill details of request in record producer variables ), then requestor can edit/add details and submit the record producer.
Pre filled details on record producer form-
1. Variable - 'Requested for' should be pre-filled with the 'Requested for' of the Request.
2. Variable - 'Summarize your incident' should be pre-filled with 'Request number and RITM short description'
Backend name of the variables- Requested For(name - requested_for) and Summarize your incident(name - issue)

I designed a solution below- 
Created an email script and called that inside the notification.
Now, the issue I am facing is-
The button is redirecting to a record producer form on esc portal but variables are not getting pre-filled.

I have used below email script-

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,

    /* Optional EmailOutbound */

    email, /* Optional GlideRecord */ email_action,

    /* Optional GlideRecord */

    event) {

 

    // Add your code here

    var callerSysId = "";

    var shortDesc = "";

   

    // Get the Requested For (Caller ID) from RITM

    if (current.requested_for) {

        var caller = new GlideRecord('sys_user');

        if (caller.get(current.requested_for)) {

            callerSysId = caller.sys_id;

        }

    }

 

    // Get the Request Number from the Parent Request

    var reqNumber = "";

    if (current.request) {

        var requestRecord = new GlideRecord("sc_request");

        if (requestRecord.get(current.request)) {

            reqNumber = requestRecord.number;

        }

    }

 

    // Get RITM Details

    var ritmShortDesc = current.short_description || "";

 

    // Construct Prefill Values

    shortDesc = reqNumber + " - " + ritmShortDesc;

 

    // Encode Only Text Fields

    callerSysId = encodeURIComponent(callerSysId);

    shortDesc = encodeURIComponent(shortDesc);

 

    // Get the instance name dynamically

    var instanceName = gs.getProperty('instance_name');

    var instanceUrl = 'https://' + instanceName + '.service-now.com/';

 

    // Incident Record Producer sys_id (Replace with your actual Record Producer sys_id)

    var recordProducerSysId = "41157e8bdbe70510491b5fc4e2961951";

 

    // Construct the Record Producer URL with prefilled variables

    /* var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item&sys_id=' + recordProducerSysId +

        '&variables.requested_for=' + requestedForSysId +  // Requested For

        '&sysparm_variables=issue=' + summary;*/

    //'&variables.issue=' + summary;                     // "Summarize Your Incident"

 

   var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item' +

    '&sys_id=' + recordProducerSysId +

    '&sysparm_direct=true' + 

    '&sysparm_query=requested_for=' + callerSysId +

    '^issue=' + shortDesc;

 

    // Generate the Button in the Email

    template.print(new global.OutlookRoundedButton().getDynamicSizeButtonHTML(

        'Create an Incident',

        '16',

        'FFFFFF',

        '059BD2',

        '7',

        '20%',

        recordProducerUrl

    ));

I tried building different url in the email script but no luck-
1. var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item' +
    '&sys_id=' + recordProducerSysId +
    '&sysparm_direct=true' +
    '&sysparm_query=requested_for=' + requestedForSysId +
    '^issue=' + summary;


2. var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item&sys_id=' + recordProducerSysId +

    '&variables.requested_for=' + requestedForSysId +  // Requested For

    '&variables.issue=' + summary;                     // "Summarize Your Incident"

 

3.  var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item' +
'&sys_id=' + recordProducerSysId +
'&sysparm_direct=true' +
'&sysparm_variables.requested_for=' + requestedForSysId +
'&sysparm_variables.issue=' + summary;

When I hover on the button, I can see the link with correct values passed in the URL parameters but they are not getting filled when the record producer form opens on click of the button.

Can someone please help me resolve the issue?
Any help is highly appreciated.
@Ankur Bawiskar @Pradeep Sharma @Mike Patel 

 

Thank you





1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ruhi Jibhyenkar 

Your URL from email script should be like this

var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item' +
    '&sys_id=' + recordProducerSysId +
    '&sysparm_direct=true' +
    '&requestedFor=' + requestedForSysId +
    '^issue=' + summary;

have you written onLoad catalog client script to get the values from URL and then set it?

try this

var url = top.location.href;
var requestedFor = new URLSearchParams(url).get("requestedFor");
var issue = new URLSearchParams(url).get("issue");

// then set the value to your variable using g_form.setValue('variableName', issue);

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Ruhi Jibhyenkar 

Your URL from email script should be like this

var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item' +
    '&sys_id=' + recordProducerSysId +
    '&sysparm_direct=true' +
    '&requestedFor=' + requestedForSysId +
    '^issue=' + summary;

have you written onLoad catalog client script to get the values from URL and then set it?

try this

var url = top.location.href;
var requestedFor = new URLSearchParams(url).get("requestedFor");
var issue = new URLSearchParams(url).get("issue");

// then set the value to your variable using g_form.setValue('variableName', issue);

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

@Ankur Bawiskar Thanks for your response.
When I use the URL you provided without using the onload client script-

var recordProducerUrl = instanceUrl + 'esc?id=sc_cat_item' +
    '&sys_id=' + recordProducerSysId +
    '&sysparm_direct=true' +
    '&requestedFor=' + requestedForSysId +
    '^issue=' + summary;

The button itself is not showing up.
I have 2 queries-
1. Do we need to only use the URL you provided for the email script?
2. Or do we need to also need to use the onload client script along with email script URL?
    If yes, the onload client script will always run.
    I just want the values to be pre filled in the record producer, only when that record producer is opened via custom button in the email.
I don't want the values to be always be pre filled, for eg- if the user creates a new incident through that record producer from the ESC portal.
Please guide on this,

Thanks

@Ankur Bawiskar  Thanks for your response.
The URL you provided for the email script is not working. The button itself is not showing up.
I have 2 questions-
1. Do we need to only use URL without using onload client script?
2. Or do we to also use onload client script with email script ?
If yes, the onload client script will always run.
I want the values to be pre filled in the record producer, only when the user opens the record producer via button click in the email.
I don't want the values of that record producer to be always pre filled.(For eg- if user raise a new incident via the record producer from the portal)
Please guide on this.
Thanks

Hi @Ankur Bawiskar 
I used both the url and the onload client script.
The button is now visible and record producer form is getting opened in esc portal.
But the issue is-
1.In requested for- when my name is populated, it shows a message below the variable as 'Item not available to this user' and when I clear the value and add my name again, the error goes away.
2. The issue variable is still not getting pre filled.