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

Hi Voona, its in Human Resources: core scope.

That is the reason it is not inserting the job record, Other applications can only read sys_trigger table data.

create cross-scope privilege record

https://www.servicenow.com/docs/bundle/vancouver-application-development/page/build/applications/ref... 

 

 


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

so what should be the fix?

I just enabled the create operation as well. still not working.

 

dvelloriy_0-1740415549441.png