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.

Need help with Flow variable script

dvelloriy
Kilo Sage

Hello All,

 

I have a requirement to set assignment group and State of HR case based on the Effective Date variable on the record producer. However, this is not working. Can someone please advise?

I am trying to do this via flow (snapshot below). When i execute the flow it just runs and completes. No error. However the assignment group is blank and state is "draft" which is wrong..

 

Here is my script:

/*
**Access Flow/Action data using the fd_data object. Script must return a value.
**Order number is offset by +1 in Error Handling Section.
**Available options display upon pressing "." after fd_data
**example: var shortDesc = fd_data.trigger.current.short_description;
**return shortDesc;
*/
var triggerRecordSysId = fd_data.trigger.current.sys_id;
var tableName = fd_data.trigger.current.sys_class_name;

var rec = new GlideRecord(tableName);
if(rec.get(triggerRecordSysId)){
var assignment_group = '';
var state = '';
var currentDate = new GlideDateTime();
var effectiveDate = new GlideDateTime(rec.variables.effective_date); // give your variable name here
var groupA = '37906b7e1bd04e10e89734cf034bcb08'; // HRSS group
var groupB = '2c74d0031bb98ed0e89734cf034bcb99'; // Recruitment Team

// Calculate the difference in days between the current date and the effective date
var daysDifference = GlideDateTime.subtract(effectiveDate, currentDate).getNumericValue() / (1000 * 60 * 60 * 24);

if (daysDifference <= 30) {
    // Assign to Group A
    assignment_group = groupA;
    state = 10; //Ready
} else {
    // Assign to Group A
    assignment_group = groupA;
    state = 24; //suspended
// Schedule a job to reassign to Group B when it reaches the 30-day mark
    var scheduledJob = new GlideRecord('sys_trigger');
    scheduledJob.initialize();
    scheduledJob.name = 'assign to Group B';
    scheduledJob.script = 'var gr = new GlideRecord("' + tableName + '"); if (gr.get("' + triggerRecordSysId + '")) { gr.assignment_group = "' + groupB + '";gr.state=10; gr.update(); }';
    scheduledJob.next_action = effectiveDate.addDays(-30);
    scheduledJob.insert();
    return assignment_group;
}
}
 
Snapshot of flow:
dvelloriy_0-1740170462300.png

 

12 REPLIES 12

Voona Rohila
Mega Patron
Mega Patron

Hi @dvelloriy 

From set Variables Action you can set value for one variable at a time for multiple variablesIf you want to return both the values then

  • maybe you can change the logic to return the date difference and add if/else conditions for remaining logic.
  • Or you can decide state value based on the group variable value that is returned(As there are only 2 groups here)

 

Issue in your code - move the return statement to outside the else block.

if (daysDifference <= 30) {
        // Assign to Group A
        assignment_group = groupA;
        state = 10; //Ready
    } else {
        // Assign to Group A
        assignment_group = groupA;
        state = 24; //suspended
        // Schedule a job to reassign to Group B when it reaches the 30-day mark
        var scheduledJob = new GlideRecord('sys_trigger');
        scheduledJob.initialize();
        scheduledJob.name = 'assign to Group B';
        scheduledJob.script = 'var gr = new GlideRecord("' + tableName + '"); if (gr.get("' + triggerRecordSysId + '")) { gr.assignment_group = "' + groupB + '";gr.state=10; gr.update(); }';
        scheduledJob.next_action = effectiveDate.addDays(-30);
        scheduledJob.insert();
       
    }
return assignment_group;
}

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Hi Voona,

 

Thanks for your advise. I created a second "state" flow variable and used the same code however returned state in the last line as shown below. Its not working.

I can see assignment group working now but not state value..

 

Also i dont see the sys_trigger record created. Can you please advise here, i might be missing something here.

 

----------

dvelloriy_0-1740173957624.png

 

It should work, share the updated code.

 

I just noticed your code, You are gliding the same table as trigger which is not required, you can change logic to this

var state = '';
var currentDate = new GlideDateTime();
var effectiveDate = new GlideDateTime(fd_data.trigger.current.effective_date); // give your variable name here
// Calculate the difference in days between the current date and the effective date
var daysDifference = GlideDateTime.subtract(effectiveDate, currentDate).getNumericValue() / (1000 * 60 * 60 * 24);
if (daysDifference <= 30) {
    state = 10; //Ready
} else {
    state = 24; //suspended
    //remove the sys_trigger logic, it will create duplicate jobs.
}
return state;

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thanks Voona, this is working now, state is getting updated too.

However i need the sys_trigger logic as well. So when the datediff >30 days, we are setting state as suspended and assignment group HRSS.

However when we reach the 30 day mark, i want to set the state to ready and assignment group as recruitment.. (which was defined in the sys trigger logic).