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.

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

You mean we need to create custom action? i am not able to break down the schedule script logic to flow.

Could you please provide the steps to execute in flow?

@User712413 

seems the script is trying to send common email to those users with list of tasks they are assigned with

unless you understand that script you won't be able to handle.

Also since there is complex script involved you will have to use flow action as flow cannot handle the aggregate part.

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