Need help with Flow variable script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 12:42 PM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 01:15 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 01:39 PM
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.
----------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 01:50 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 01:57 PM
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).