The CreatorCon Call for Content is officially open! Get started here.

Mass populate reference field with value from parent request

heathers_
Kilo Sage

Hello, is there an effective method to mass populate the Requested Item value on the Contract request?

 

Scenario: sc_req_item has the ast_contract value populated, however the ast_contract does not have the sc_req_item value populated. There are thousands of records needing to be updated, looking for the most efficient option, other than manually updating each record.

Example 1: RITM has Contract relationship

heathers__0-1759771137690.png


Example 2: Contract does not have RITM relationship.

heathers__1-1759771223838.png

 

2 REPLIES 2

YaswanthKurre
Tera Guru

Hi @heathers_ ,

 

You can  write a Fix Script in ServiceNow to mass populate the sc_req_item reference on the ast_contract table based on existing relationships.

 

For each ast_contract record missing a sc_req_item value:

  • Find the corresponding sc_req_item where ast_contract is already populated.
  • Update the ast_contract.sc_req_item field with that value.

Script:

// Get all ast_contract records where sc_req_item is empty
var contractGR = new GlideRecord('ast_contract');
contractGR.addNullQuery('sc_req_item');
contractGR.query();

while (contractGR.next()) {
    // Look for sc_req_item that references this contract
    var ritmGR = new GlideRecord('sc_req_item');
    ritmGR.addQuery('ast_contract', contractGR.sys_id);
    ritmGR.query();

    if (ritmGR.next()) {
        // Update the contract with the found sc_req_item
        contractGR.sc_req_item = ritmGR.sys_id;
        contractGR.update();   
    } 
}

NOTE: Make sure field names are correct.

 

Mark this as helpful and correct, if this helps you.

 

Thanks,

Yaswanth

vignesh parthib
Tera Guru

Hi @heathers_ 

 

Yes, History of record you can go head with FIX script and also consider the future record you can go for BR

 

FIX script:

 

var reqItemGR = new GlideRecord('sc_req_item');
reqItemGR.addNotNullQuery('ast_contract'); // Only where ast_contract is populated
reqItemGR.query();

var updatedCount = 0;

while (reqItemGR.next()) {
    var contractGR = new GlideRecord('ast_contract');
    if (contractGR.get(reqItemGR.ast_contract)) {
        if (!contractGR.sc_req_item) {
            contractGR.sc_req_item = reqItemGR.sys_id;
            contractGR.update();
             contractGR.autoSysFields(false);   // Prevent updated_by, updated_on from changing
             contractGR.setWorkflow(false);     // Prevent BRs and Flows from triggering
            updatedCount++;
        }
    }
}

 

 

Business Rule:

 

  • Table: sc_req_item

  • When: after

  • Insert: check

  • Update:  Check

  • Condition: current.ast_contract.changes() or current.ast_contract is not empty

 

 

if (current.ast_contract) {
    var contractGR = new GlideRecord('ast_contract');
    if (contractGR.get(current.ast_contract)) {
        if (!contractGR.sc_req_item) {
            contractGR.sc_req_item = current.sys_id;
            contractGR.update();
        }
    }
}

 

 

Thanks,
Vignesh
"If this solution resolves your issue, kindly mark it as correct."