Populating ServiceNow variable through the Workflow

MuhammadAqS
Tera Contributor

I have a Workflow script, that is assigning values to different variables. Below is that script:

var grReqItem = new GlideRecord('sc_req_item');
    grReqItem.addQuery('request', current.request);
    grReqItem.addQuery('cat_item', sysid);
    grReqItem.orderByDesc('sys_created_on');
    grReqItem.query();

    if (grReqItem.next()) {
        // Reset the parent and summary fields
        grReqItem.parent = ''; // Don't need parent as they are linked via request
        grReqItem.short_description = 'IT New Starter Application Permissions for ' + current.variables.u_first_name + ' ' + current.variables.u_last_name + ' - ' + current.variables.u_start_date.toString();
       
        // Set the non conditional fields first
        grReqItem.variables.u_first_name = current.variables.u_first_name   ;
        grReqItem.variables.u_last_name = current.variables.u_last_name;
        grReqItem.variables.u_job_title = current.variables.u_title;
        grReqItem.variables.u_start_date = current.variables.u_start_date;
        grReqItem.variables.u_additional_it_requirements = current.variables.u_additional_requirements;
        grReqItem.variables.u_location = current.variables.u_location;
        grReqItem.variables.u_manager = current.variables.u_manager;
        grReqItem.variables.u_user_name = current.variables.u_user_name;
        grReqItem.variables.u_email = current.variables.u_email;


I have created a new variable Approval to Hire ID, and I want to assign this value also. I've written the below line but that's not working:

grReqItem.variables.u_approval_to_hire_id = current.variables.u_approval_to_hire_id;


19 REPLIES 19

yes, it is not picking the value of the record producer's variable.

MuhammadAqS_0-1738844871653.png

 

@MuhammadAqS 

is there any read role given in that Permissions tab?

Did you try running script to print that variable value using background script as admin?

Seems your workflow is running in some user's session and it's not having access to this variable

This you can confirm if you run script using admin using background script

Try this and share result

var rec = new GlideRecord('target table of record producer'); // give target table of record producer
if(rec.get('sysId')){ // give record sysId
gs.info(rec.variables.u_approval_to_hire_id);
}

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

Yes, I've tried that in the Background script. But it also not populating. Below is the output of your script, which is working fine:

*** Script: 345


Let me send you the background script that I used for debugging:

var requestSysId = '8769631583ef1610d0d15e60ceaad35f'; // Replace with the actual Request ID
var catItemSysId = '1b11ef4213124050d9dcb6d96144b0a9';

// Retrieve the original request item to get the Approval to Hire ID
var grProducer = new GlideRecord('sc_req_item');
grProducer.addQuery('request', requestSysId);
grProducer.orderByDesc('sys_created_on'); // Get the latest RITM from the request
grProducer.query();

if (grProducer.next()) {
    var approvalToHireId = grProducer.variables.u_approval_to_hire_id;
    gs.info("Approval to Hire ID from Producer: " + approvalToHireId);

    // Find the latest created RITM for the specific catalog item
    var grReqItem = new GlideRecord('sc_req_item');
    grReqItem.addQuery('request', requestSysId);
    grReqItem.addQuery('cat_item', catItemSysId);
    grReqItem.orderByDesc('sys_created_on'); // Get the latest one
    grReqItem.query();

    if (grReqItem.next()) {
        gs.info("Found RITM: " + grReqItem.number);

        // Update the variable
        grReqItem.variables.u_approval_to_hire_id = approvalToHireId;
        grReqItem.update();

        gs.info(" Approval to Hire ID updated successfully for RITM: " + grReqItem.number);
    } else {
        gs.warn("⚠️ No RITM found for Catalog Item: " + catItemSysId);
    }
} else {
    gs.warn("⚠️ No RITM found for Request: " + requestSysId);
}

@MuhammadAqS 

you are fetching variable value from RITM record which is not submitted via record producer

what value came here?

gs.info("Approval to Hire ID from Producer: " + approvalToHireId);

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 it's empty