How can we update reference field value on table using Glide Record?

Harshal Sonawa1
Kilo Guru
contractNumber = 'FSD1111';
gr.setValue('u_contract', contractNumber);
gr.update();
 
I m trying to update using setValue() but its not working
1 ACCEPTED SOLUTION

Hi @shloke04 ,

Thanks a lot for helping and having patience. Finally, I am able to resolve this.

ac.update() will not work for saving(updating) values in table for reference field.

ac.updateWithReferences(); is correct method for it.

 

View solution in original post

18 REPLIES 18

Gaurav Shirsat
Mega Sage

Hello

Can You please tell the use case?

which Business Rule You are Using?

to set a particular Variable please do as below

var contractNumber = 'FSD1111';
gr.setDisplayValue('u_contract', contractNumber);
gr.update();

but Point is based on Your Use Case we need to select the Business Rule

before or After followed by insert or update or delete

Mark my Response as Correct or Helpful, if you find it Appropriate.
Gaurav Shirsat
https://www.linkedin.com/in/gauravshirsat/

Hi,

I am creating script include to update records

Hi Gaurav,

I have written background script, where I want to update reference field value. When ac.update() written inside while loop it does not update field, but when written outside of while loop its inserting record instead of updating

 

var facilityContract = 'FCON0004614';
var ac = new GlideRecord('xyz');
ac.addEncodedQuery('contract.number=' + facilityContract);
ac.query();

gs.info("query Count: " + ac.getRowCount());


while(ac.next()) {
    gs.info("before update: " + ac.asset.u_contract.getDisplayValue() +  "  serial number:" + ac.asset.serial_number);
 
    
    var sysIdContact = '035406911b24bc1057c6ff3ecc4bcb5b';
    ac.setValue('asset.u_contract',sysIdContact );

    ac.update();
    
}

Dubz
Mega Sage

Reference fields take a sys_id as their value so you either need to pass in a sys_id eg:

gr.u_contract = '<sys_id>';

Or you can use setDisplayValue() to set the value of the field by the display value, this will obviously require that the display value on the referenced table is unique. eg:

gr.u_contract.setDisplayValue(contractNumber);

Hi Dubz,

I have written background script, where I want to update reference field value. When ac.update() written inside while loop it does not update field, but when written outside of while loop its inserting record instead of updating

 

var facilityContract = 'FCON0004614';
var ac = new GlideRecord('xyz');
ac.addEncodedQuery('contract.number=' + facilityContract);
ac.query();

gs.info("query Count: " + ac.getRowCount());


while(ac.next()) {
    gs.info("before update: " + ac.asset.u_contract.getDisplayValue() +  "  serial number:" + ac.asset.serial_number);
 
    
    var sysIdContact = '035406911b24bc1057c6ff3ecc4bcb5b';
    ac.setValue('asset.u_contract',sysIdContact );

    ac.update();
    
}