To save value from deleted record

Manisha Idhate
Tera Contributor

Hi Team,

 

We are working on Operational Resilience module, in that one we are creating Asset for Business services, 

On Asset table we have added on custom field, which we are using to track some process.

But Asset records gets refreshed when scheduled job related to Compliance gets runs, in that all old records gets deleted which was created with Relationship with Business service/Process. 

And that Relationship records gets created again by same scheduled job.

 

But here in old records we have custom field with value <true/false>, when new record with relationship gets create, we want to update same custom field value for new record. 

 

How we can same old record data to again paste on new record.

 

Thanks,

Manisha I

1 REPLY 1

Riya Verma
Kilo Sage
Kilo Sage

HI There,

we can follow the steps outlined below:

  1. Identify the Custom Field:

    • First, we need to identify the custom field on the Asset table that holds the value we want to preserve.
  2. Create a Script to Copy Data:

    • We will create a server-side script (Business Rule or Script Include) that runs when a new relationship record is created. This script will check if there are any corresponding old records with the same relationship. If found, it will copy the custom field value from the old record to the newly created record.
  3. Attach the Script to Relationship Creation Event:

    • We will configure the server-side script to run when a new relationship record is created, ensuring it performs the data copying process.

Here's a sample script (written in pseudo-code) to achieve this:

// Identify the custom field name
var customFieldName = 'your_custom_field';

// Business Rule or Script Include to run on relationship creation
function copyCustomFieldValueOnRelationshipCreation() {
    var newRelationship = current; // Assuming 'current' holds the newly created relationship record
    var businessService = newRelationship.business_service; // Assuming 'business_service' is the reference field to Business Service
    var process = newRelationship.process; // Assuming 'process' is the reference field to Process
    var oldValueToCopy;

    // Check for old records with the same Business Service and Process
    var oldAssetGR = new GlideRecord('asset');
    oldAssetGR.addQuery('business_service', businessService);
    oldAssetGR.addQuery('process', process);
    oldAssetGR.query();
    
    while (oldAssetGR.next()) {
        oldValueToCopy = oldAssetGR.getValue(customFieldName);
        break; // Assuming there will be only one matching old record
    }

    // Copy the custom field value to the newly created relationship record
    if (oldValueToCopy !== null && oldValueToCopy !== '') {
        newRelationship.setValue(customFieldName, oldValueToCopy);
    }
}

// Attach the script to the relationship creation event
copyCustomFieldValueOnRelationshipCreation();
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma