Migrate schedule job script to scheduled flow

User712413
Tera Contributor

How can we convert below schedule job script logic into scheduled flow.
Scheduled JOB Which run Daily:

var EVENT_NAME = "pending.change.task.email";
var taskGroups = {};
var now = new GlideDateTime();
var gr = new GlideRecord('change_task');
gr.addQuery('assigned_toISNOTEMPTY');
gr.addActiveQuery();
gr.query();
while (gr.next()) {
    var assignedTo = gr.assigned_to.email + '';
    var createdDate = new GlideDateTime(gr.getValue('sys_created_on'));
    var name = gr.assigned_to.name + '' || 'Unassigned';
    var email = gr.assigned_to.email + ',' + gr.assigned_to.manager.email;
    var ageMillis = GlideDateTime.subtract(createdDate, now).getNumericValue();
    var ageDays = Math.floor(ageMillis / (1000 * 60 * 60 * 24));
    var category = '';
    if (ageDays >= 0 && ageDays <= 3) {
        category = 'recent';
    } else if (ageDays >= 4 && ageDays <= 8) {
        category = 'moderate';
    } else if (ageDays > 8) {
        category = 'old';
    } else {
        continue; // Skip any future-dated records
    }
    // Initialize if user not present
    if (!taskGroups[assignedTo]) {
        taskGroups[assignedTo] = {
            'name': name,
            'email': email,
            'recent': [],
            'moderate': [],
            'old': []
        };
    }
    // Add the task to the relevant category
    taskGroups[assignedTo][category].push({
        number: gr.getValue('number'),
        short_description: gr.getValue('short_description'),
        link: gs.getProperty('glide.servlet.uri') + gr.getLink(),
        created: gr.getDisplayValue('sys_created_on'),
    });
}
var resultArray = [];
for (var user in taskGroups) {
    gs.eventQueue(EVENT_NAME, null, JSON.stringify(taskGroups[user]), user);
    resultArray.push({
        assigned_to: user,
        tasks_by_age: taskGroups[user]
    });
}
var output = JSON.stringify(resultArray, null, 2);
@Ankur Bawiskar @Dr Atul G- LNG 
6 REPLIES 6

SANDEEP DUTTA
Tera Patron
Tera Patron

Hi @User712413 ,

What have you tried so far for your Flow?

 

Thanks,
Sandeep Dutta

Please mark the answer correct & Helpful, if i could help you.

User712413_0-1751865340254.png

I am just trying to break down into flow logic.

PavanBV
Giga Guru

Hi @User712413 , please try these steps and let me know how it goes. 

-->Try to have a flow built on change_task table that runs as per your defined frequency(daily/monthly/periodically/once)  and conditions as Active= true, AssignedTo is nor empty

-->Create a custom action that calculates the age of the task, supply the task created date as input and the action should perform the duration calculation and return if its recent/moderate/Old/Skip. 

--> In the same action, add a script that groups the tasks based on user and return that in the action output.
Sample script

 
Spoiler
var created = new GlideDateTime(current.sys_created_on);
var now = new GlideDateTime();
var ageMillis = GlideDateTime.subtract(now, created).getNumericValue();
var ageDays = Math.floor(ageMillis / (1000 * 60 * 60 * 24));
var category = '';

 

if (ageDays >= 0 && ageDays <= 3) {
  category = 'recent';
} else if (ageDays >= 4 && ageDays <= 8) {
  category = 'moderate';
} else if (ageDays > 8) {
  category = 'old';
}

 

outputs.category = category;
 

 

-->Use a scriptable Flow Action/Subflow to group tasks by user and category and Build a JSON object like

{
  "user_email": "user@example.com",
  "name": "User Name",
  "recent": [...],
  "moderate": [...],
  "old": [...]
}

--> Use OOB Send Email action

  • Loop through each user in your grouped data
  • Send the JSON summary as part of the email or event payload

Thanks,

Pavan BV

Ankur Bawiskar
Tera Patron
Tera Patron

@User712413 

if you are using complex script then you will have to use flow action for this

where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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