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.

How to pass a variable value from first task and populate in the second task variable?

User205031
Tera Contributor

Hi All,

 

I have a requirement. In the first task there is a variable 'ticket_number' and in the second task the variable is 'u_ticket_number'.

Once the first task is closed with 'ticket_number' , I want to populate the same value in 'u_ticket_number' variable.

 

I have written a BR with before/update and below script:

 

Conditions: Item is catalog item name & Short description is second task' short description

 

Code:

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    var ritmSysId = current.request_item;
    gs.log("Line8" + ritmSysId);

    var grfirstTask = new GlideRecord('sc_task');
    grfirstTask .addQuery('request_item', 'ritmSysId');
    grfirstTask .addQuery('sys_id', '!=', current.sys_id);
    grfirstTask .orderBy('sys_created_on');
    grfirstTask .setLimit(1);
    grfirstTask .query();


    if (grfirstTask .next()) {
        var sourceValue = grfirstTask.variables.ticket_number;
       
        if (sourceValue) {
            current.variables.u_ticket_number = sourceValue;
            gs.info('Copied variable value from first task to second task: ' + sourceValue);
        }

    } else {
        gs.info('Source variable is empty.');

    }
})(current, previous);
 
However it is not populating.
 
Thanks!
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@User205031 

What's the requirement to replicate same information in other variable?

Why not show this variable on 2nd task as well for agent to work.

If you still want to proceed then see below points

I assume agent working on 1st task will fill that variable and close/update the sc_task

If yes then you should have after update on sc_task

Condition: current.request_item.item.name == 'Your Item Name'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var ritm = current.request_item.getRefRecord();
    ritm.variables.u_ticket_number = current.variables.ticket_number;
    ritm.update();

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

YaswanthKurre
Tera Guru

Hi @User205031 ,

 

You’re using 'ritmSysId' as a string instead of the variable ritmSysId.

your Business Rule should run on the first task when it’s closed — not on the second task.

 

Trigger:

  • Table: sc_task

  • When: after update

  • Condition: task is closed or whatever your “closed” state is.

 

Try the below script:

(function executeRule(current, previous /*null when async*/) {

    var ritmSysId = current.request_item;
    if (!ritmSysId) {
        gs.info('No RITM linked to this task.');
        return;
    }

    var ticketNum = current.variables.ticket_number;
    if (!ticketNum) {
        gs.info('No ticket_number value found on the closed task.');
        return;
    }

    // Find the "second" task under same RITM
    var grSecondTask = new GlideRecord('sc_task');
    grSecondTask.addQuery('request_item', ritmSysId);
    grSecondTask.addQuery('sys_id', '!=', current.sys_id);
    grSecondTask.orderBy('sys_created_on');
    grSecondTask.query();

    if (grSecondTask.next()) {
        // Assume this is the second task
        grSecondTask.variables.u_ticket_number = ticketNum;
        grSecondTask.update();
        gs.info('Updated second task (' + grSecondTask.number + ') with u_ticket_number: ' + ticketNum);
    } else {
        gs.info('No other task found for this RITM.');
    }

})(current, previous);

 

Mark this as helpful and correct, if this helps you.

 

Thanks,

Yaswanth

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@User205031 

What's the requirement to replicate same information in other variable?

Why not show this variable on 2nd task as well for agent to work.

If you still want to proceed then see below points

I assume agent working on 1st task will fill that variable and close/update the sc_task

If yes then you should have after update on sc_task

Condition: current.request_item.item.name == 'Your Item Name'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var ritm = current.request_item.getRefRecord();
    ritm.variables.u_ticket_number = current.variables.ticket_number;
    ritm.update();

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

 I added the first variable in the second task to populate the variable. It is displaying the value.

 

Thanks!

Deepak Shaerma
Kilo Sage
Kilo Sage

hi @User205031 

The Modern Method is using the Flow Designer,  as it requires no scripting and anyone can look at the flow and see exactly what's happening. It's the standard, supported way to manage variable handoffs.

Note: 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