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.

Logic to force purge all field values OnCreate

PB7
Mega Guru

Hi Team,

 

A recurring issue in our environment is something will have the Case [sn_customerservice_case] table filtered to 'assigned_to' IS "[X]" or 'Account'//'Company' IS "[Y]" , and then they will click the [NEW] UI action button on the Case table to draft a new record --- the defined filtered logic I mentioned above will carry over onto the draft Case record [i.e. draft Case record 'Assigned To' will populate as "[X]" and/or 'Account' field will populate as "[Y]" --- therefore certain client scripts will not trigger correctly due to this.

 

I've attached two pairs of snips to demonstrate this behavior.

 

Can anyone recommend to me an efficient and low-resource way to ensure that OnCreate every single field value on a draft Case record is purged ?  I see this as the simplest / best way to handle this.

 

 

Thanks,

Pat

1 ACCEPTED SOLUTION

Ratnakar7
Mega Sage

Hi @PB7 ,

To ensure that all field values are cleared when creating a new record (OnCreate), you can use an "OnBefore" business rule. Here's an example of a simple server-side script that you can use:

(function executeRule(current, previous /*null when async*/) {
    // This business rule runs before the record is inserted.

    // Check if the record is new (OnCreate)
    if (current.isNewRecord()) {
        // Loop through each field on the current record
        for (var field in current) {
            // Exclude system fields and fields you want to preserve
            if (current[field] !== undefined && !field.startsWith('sys_') && field !== 'assigned_to' && field !== 'account') {
                // Clear the field value
                current[field] = '';
            }
        }
    }

})(current, previous);

This script does the following:

  • Checks if the record is new.
  • Loops through each field on the current record.
  • Excludes system fields (those starting with 'sys_'), and fields like 'assigned_to' and 'account' that you want to preserve.
  • Clears the value for each field.

Make sure to adjust the fields you want to preserve in the exclusion list (field !== 'assigned_to' && field !== 'account'), and you can add more fields as needed.

 

Thanks,

Ratnakar

View solution in original post

1 REPLY 1

Ratnakar7
Mega Sage

Hi @PB7 ,

To ensure that all field values are cleared when creating a new record (OnCreate), you can use an "OnBefore" business rule. Here's an example of a simple server-side script that you can use:

(function executeRule(current, previous /*null when async*/) {
    // This business rule runs before the record is inserted.

    // Check if the record is new (OnCreate)
    if (current.isNewRecord()) {
        // Loop through each field on the current record
        for (var field in current) {
            // Exclude system fields and fields you want to preserve
            if (current[field] !== undefined && !field.startsWith('sys_') && field !== 'assigned_to' && field !== 'account') {
                // Clear the field value
                current[field] = '';
            }
        }
    }

})(current, previous);

This script does the following:

  • Checks if the record is new.
  • Loops through each field on the current record.
  • Excludes system fields (those starting with 'sys_'), and fields like 'assigned_to' and 'account' that you want to preserve.
  • Clears the value for each field.

Make sure to adjust the fields you want to preserve in the exclusion list (field !== 'assigned_to' && field !== 'account'), and you can add more fields as needed.

 

Thanks,

Ratnakar