Update 2 or more different records in 1 action

WaseemM
Giga Expert

Hi Guys,

 

I need help updating multiple records in a single step in Flow Designer. For example, if my flow fails, I must update both the Request and Change Request simultaneously. However, I must use two actions, making the flow unnecessarily long.

 

Please let me know if there is any OOBE feature or custom action, which would be great.

1 ACCEPTED SOLUTION

Hello @Ankur Bawiskar ,

 

Thanks for your response. I have generated the JSON within JavaScript, and as suggested, it provides users with the option to select and update only the required fields. Additionally, I have hardcoded the table name since the Change Request State fields differ and do not align with the INC and RITM fields.

 

Test_Input_01.pngStep_Input_01.pngAction_Input_01.png

 

(function execute(inputs, outputs) {
    var userinput = inputs.userinput;
    var ritmnumber = inputs.ritmnumber;
    var state = inputs.state;
    var work_notes = inputs.work_notes;
    var assigned_to = inputs.assigned_to;
    var assignment_group = inputs.assignment_group;
    var short_description = inputs.short_description;
    var description = inputs.description;

    var fields = [
        { field: "work_notes", value: work_notes },
        { field: "assigned_to", value: assigned_to },
        { field: "assignment_group", value: assignment_group },
        { field: "short_description", value: short_description },
        { field: "description", value: description },
        { field: "state", value: state }
    ];
    
    // Filter out fields where the value is null or an empty string
    var fieldsToUpdate = fields.filter(function(item) {
        return item.value !== null && item.value !== "";
    });

    var gr = new GlideRecord('sc_req_item');

    // Check if userinput is true
    if (userinput == true) {
        // Check if the RITM number is found
        if (gr.get('number', ritmnumber)) {
            // Iterate through the JSON array and update the fields
            for (var i = 0; i < fieldsToUpdate.length; i++) {
                var field = fieldsToUpdate[i].field;
                var value = fieldsToUpdate[i].value;
                if (field === "work_notes") {
                    gr.work_notes = value; // Use the work_notes property directly
                } else {
                    gr.setValue(field, value);
                }
            }

            gr.update();
            gs.info('RITM ' + ritmnumber + ' has been updated successfully.');
        } else {
            gs.info('RITM ' + ritmnumber + ' not found.');
        }
    } else {
        gs.info('User input was false, RITM update skipped.');
    }

})(inputs, outputs);

 

 

View solution in original post

10 REPLIES 10

WaseemM
Giga Expert

@Dr Atul G- LNG  / @Ankur Bawiskar  / @Rohit Singh ,

 

Thanks for your response! I plan to create an action with three steps, but I'm unfamiliar with JavaScript and GlideRecords. I've defined inputs for the following fields: each record will have a "Yes" or "No" selection. The user will choose "Yes" for the required fields, and the selected values will be used to update the records. This approach will allow us to use this action globally across the ServiceNow instance.

 

Please up vote if you feel this would be a good approach.

 

RITM – Assignment Group, Assigned To, Status, and Worknotes.
Change Request – Status and Worknotes.
Tasks – Assignment Group, Assigned To, Status, Worknotes, and Due Time.

 

(function execute(inputs, outputs) {
    var userinput = inputs.userinput;
    var ritmnumber = inputs.ritmnumber;
    var state = inputs.state;
    var work_notes = inputs.work_notes;
    var assigned_to = inputs.assigned_to;
    var assignment_group = inputs.assignment_group;

    var gr = new GlideRecord('sc_req_item');

    //if userinput equal to true
    if (userinput == true) {
        //if ritmnumber is found
        if (gr.get('number', ritmnumber)) {
            gr.state = state;
            gr.work_notes = work_notes;
            gr.assigned_to = assigned_to;
            gr.assignment_group = assignment_group;
            gr.update();
            gs.info('RITM ' + ritmnumber + ' has been updated successfully.');
        } else {
            gs.info('RITM ' + ritmnumber + ' not found.');
        }
    } else {
        gs.info('User input was false, RITM update skipped.');
    }

})(inputs, outputs);

 

@WaseemM 

to make it even more reusable please pass the table name as input and the fields to update and it's value

It will be somewhat complex but it will be re-usable

1) 1 input for table name

2) 1 input of type JSON string and pass something like this, parse and then set it

[
{
"field": "work_notes",
"value": "myTesting"
},
{
"field": "assigned_to",
"value": "abelTuterSysId"
}
]

3) then update the script to parse this and update the record

Something like this but please enhance

(function execute(inputs, outputs) {
    var userinput = inputs.userinput;
    var ritmnumber = inputs.ritmnumber;
    var fieldsToUpdate = inputs.fieldsToUpdate; // JSON array of fields and values
	var table = inputs.tablename;
    var gr = new GlideRecord(table);

    // Check if userinput is true
    if (userinput == true) {
        // Check if the RITM number is found
        if (gr.get('number', ritmnumber)) {
            // Iterate through the JSON array and update the fields
            for (var i = 0; i < fieldsToUpdate.length; i++) {
                var field = fieldsToUpdate[i].field;
                var value = fieldsToUpdate[i].value;
                gr.setValue(field, value);
            }
            
            gr.update();
            gs.info('RITM ' + ritmnumber + ' has been updated successfully.');
        } else {
            gs.info('RITM ' + ritmnumber + ' not found.');
        }
    } else {
        gs.info('User input was false, RITM update skipped.');
    }
})(inputs, outputs);

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

@WaseemM 

Thank you for marking my response as helpful.

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

Hello @Ankur Bawiskar ,

 

Thanks for your response. I have generated the JSON within JavaScript, and as suggested, it provides users with the option to select and update only the required fields. Additionally, I have hardcoded the table name since the Change Request State fields differ and do not align with the INC and RITM fields.

 

Test_Input_01.pngStep_Input_01.pngAction_Input_01.png

 

(function execute(inputs, outputs) {
    var userinput = inputs.userinput;
    var ritmnumber = inputs.ritmnumber;
    var state = inputs.state;
    var work_notes = inputs.work_notes;
    var assigned_to = inputs.assigned_to;
    var assignment_group = inputs.assignment_group;
    var short_description = inputs.short_description;
    var description = inputs.description;

    var fields = [
        { field: "work_notes", value: work_notes },
        { field: "assigned_to", value: assigned_to },
        { field: "assignment_group", value: assignment_group },
        { field: "short_description", value: short_description },
        { field: "description", value: description },
        { field: "state", value: state }
    ];
    
    // Filter out fields where the value is null or an empty string
    var fieldsToUpdate = fields.filter(function(item) {
        return item.value !== null && item.value !== "";
    });

    var gr = new GlideRecord('sc_req_item');

    // Check if userinput is true
    if (userinput == true) {
        // Check if the RITM number is found
        if (gr.get('number', ritmnumber)) {
            // Iterate through the JSON array and update the fields
            for (var i = 0; i < fieldsToUpdate.length; i++) {
                var field = fieldsToUpdate[i].field;
                var value = fieldsToUpdate[i].value;
                if (field === "work_notes") {
                    gr.work_notes = value; // Use the work_notes property directly
                } else {
                    gr.setValue(field, value);
                }
            }

            gr.update();
            gs.info('RITM ' + ritmnumber + ' has been updated successfully.');
        } else {
            gs.info('RITM ' + ritmnumber + ' not found.');
        }
    } else {
        gs.info('User input was false, RITM update skipped.');
    }

})(inputs, outputs);

 

 

@WaseemM 

Glad to know.

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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