I want to prevent the form from saving if the child records are not created in the parent initiative

Shubhi Garg
Tera Contributor

I want to prevent the form from saving if the child records are not created in the parent initiative based on the conditions specified in the script given below, but the abort action isn't working and the record is still getting submitted. 

(function executeRule(current, previous /*null when async*/ ) {

    /*
    Check parent record list of groups are in child record group field.
    Display the message for the groups not in the child records.
    */

    var parentGroups = current.groups.split(',');

    var childGroups = [];
    var groupNotPresent = [];
    var childRecrd = new GlideRecord('x_mab_acc_payable_initiative_tracker');
    childRecrd.addQuery('parent_initiative', current.sys_id);
    childRecrd.addQuery('business_groups', '!=', '');
    childRecrd.query();
    while (childRecrd.next()) {
        childGroups.push(childRecrd.business_groups.toString());
    }

    for (var i = 0; i < parentGroups.length; i++) {
        if (childGroups.indexOf(parentGroups[i]) == -1) {
            groupNotPresent.push(parentGroups[i].toString());
        }
    }
    if (groupNotPresent.length > 0) {
        gs.addErrorMessage("Make sure Split Savings are created for the groups mentioned in the Initiative - " + groupNotPresent);
        //current.setAbortAction(true);
    }
    if(childGroups.length==0){
        current.setAbortAction(true);
    }



})(current, previous);
1 REPLY 1

sreeram_nair
Tera Guru

I think the issue here lies in how the setAbortAction(true) method is used and the execution context of the Business Rule. If the rule is an after or async Business Rule, the record is already saved by the time the rule runs, so aborting will not prevent the record from being saved. To ensure the form doesn't save when the conditions aren't met, you need to use a before Business Rule.

Set the Business Rule to run before insert or before update depending on your requirement. This ensures the setAbortAction(true) method is effective.

Updated Code: (Not tested)

 

(function executeRule(current, previous /*null when async*/) {
    // Split the parent groups
    var parentGroups = current.groups.split(',');

    var childGroups = [];
    var groupNotPresent = [];
    
    // Query child records linked to the parent initiative
    var childRecrd = new GlideRecord('x_mab_acc_payable_initiative_tracker');
    childRecrd.addQuery('parent_initiative', current.sys_id);
    childRecrd.addQuery('business_groups', '!=', '');
    childRecrd.query();

    // Collect all child groups
    while (childRecrd.next()) {
        childGroups.push(childRecrd.business_groups.toString());
    }

    // Check if all parent groups are present in child groups
    for (var i = 0; i < parentGroups.length; i++) {
        if (childGroups.indexOf(parentGroups[i]) === -1) {
            groupNotPresent.push(parentGroups[i].toString());
        }
    }

    // If any group is missing, abort the action and display an error message
    if (groupNotPresent.length > 0) {
        gs.addErrorMessage(
            "Make sure Split Savings are created for the groups mentioned in the Initiative - " + groupNotPresent.join(', ')
        );
        current.setAbortAction(true); // Prevent the record from being saved
    }

    // If no child records exist, also abort the action
    if (childGroups.length === 0) {
        gs.addErrorMessage("No child records found. Please create child records before saving.");
        current.setAbortAction(true);
    }
})(current, previous);

 

 


ɪꜰ ᴍʏ ᴀɴꜱᴡᴇʀ ʜᴀꜱ ʜᴇʟᴘᴇᴅ ᴡɪᴛʜ ʏᴏᴜʀ Qᴜᴇꜱᴛɪᴏɴ, ᴘʟᴇᴀꜱᴇ ᴍᴀʀᴋ ᴍʏ ᴀɴꜱᴡᴇʀ ᴀꜱ ᴛʜᴇ ᴀᴄᴄᴇᴘᴛᴇᴅ ꜱᴏʟᴜᴛɪᴏɴ ᴀɴᴅ ɢɪᴠᴇ ᴀ ᴛʜᴜᴍʙꜱ ᴜᴘ.




ʙᴇꜱᴛ ʀᴇɢᴀʀᴅꜱ


ꜱʀᴇᴇʀᴀᴍ