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

@Ruhi Jibhyenkar 

what type of variable is present on the form?

Does it have any reference qualifier applied?

what does the URL parameter contain for issue? does it have value?

share both the complete scripts and screenshots.

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 
The variables I want to pre- fill are-
1. Requested for--> type- Reference, name- requested_for, Reference qualifier- active is true and employee number is not empty, Default Value - javascript:gs.getUserID()
2. Summarize your incident --> type - String, name - issue, No reference qualifier and default value.

I have attached the URL parameter screenshot, email script screenshot and record producer screenshot(after clicking on the button)
Client script -

function onLoad() {
var url = top.location.href;
var req = new URLSearchParams(url).get("requested_for");
var summary = new URLSearchParams(url).get("issue");
g_form.setValue('requested_for', req);
g_form.setValue('issue', summary);
}
Please have a look and help to resolve this.
Thanks.



@Ruhi Jibhyenkar 

so what came in alert for issue and requested for?

The script I shared should give the values from URL.

The requested for which you are populating doesn't satisfy the user criteria for that item.

Are you using the normal reference type variable for Requested for?

Why not use Requested for variable type for that?

AnkurBawiskar_0-1743476682698.png

 

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 
I added two alerts in the client script as- 

alert('requested for is:' +req);
alert('summary is:' +summary);

I have added the screenshots of the already which got printed on the portal.
Requested for is having some value, but summary is null.

The requested for I am populating does satisfy the user criteria because initially when the form loads by my name, it shows the error 'Item not available' but when I clear the value and add my name again, the error goes away. So the issue is not related with user criteria.

Also my bad, we are already using type- 'Requested For' the variable 'Requested for.'
Please check the attached screenshots.

@Ankur Bawiskar 
I just made a small change in your provided URL.

URL-

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

 

Change was- 
Instead of '^issue=' + summary; 

I used '&issue=' + summary;
And this worked.

Thank you.
Really appreciate your help.