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

Yes but add that logic in only 1 set-variable code

Yes added that in group flow .. however ita not working.  I dont see any record with name “assign to group B” created in sys_trigger table 

Please check in the flow property, run as should be set as system user.

 

https://developer.servicenow.com/dev.do#!/learn/courses/xanadu/app_store_learnv2_flowdesigner_xanadu...

Tried that, still not working. Not sure why..

It's working for me from background script.

Flow is in which scope?