Sync two fields in different tables.

crhistopherjuar
Kilo Expert

Hi Experts!

I am trying to sync two fields in different tables.

When a Company is selected in the table   ['u_contrato_esquema_de_servicio'], in the other form [core_company], the system has to sync with the "u_compania" field.

find_real_file.png

find_real_file.png

I try to do this with a business rule when the u_compania changes

var getCompa = current.u_compania.getRefRecord();

getCompa.u_compania = current.u_contrato;

getCompa.update();

and other business rule that I tried is:

var prj = new GlideRecord('core_company');      

    prj.addQuery('sys_id',current.u_compania);          

    prj.query();      

    var rp = new GlideRecord('u_contrato_esquema_de_servicio');      

    rp.addQuery('u_compania',current.u_compania);      

    rp.query();  

if(prj.next()) {      

 

prj.u_contrato = rp.u_compania;

}

but any of those scripts worked,

Any idea what do I am missing?

1 ACCEPTED SOLUTION

veena_kvkk88
Mega Guru

Hi Christopher,



I'm not really sure whats wrong with the script since we don't know what tables they are running on..will need more info. Also, you don't need to query both the tables if you are writing a business rule since you already have access to one using 'current' object.



However here is the script that I think should work: (Untested code)



After Update Business Rule on Table 'u_contrato_esquema_de_servicio':



//After updating Contrato table with the new Company field value, this script queries the Company table for the record referred here and updates the contrato field with current sys_id (since it is a reference field)



var gr = new GlideRecord('core_company');


gr.addQuery('sys_id', current.u_compania);


gr.query();


while(gr.next()){


  gr.u_contrato = current.sys_id;


  gr.update();


}


View solution in original post

3 REPLIES 3

veena_kvkk88
Mega Guru

Hi Christopher,



I'm not really sure whats wrong with the script since we don't know what tables they are running on..will need more info. Also, you don't need to query both the tables if you are writing a business rule since you already have access to one using 'current' object.



However here is the script that I think should work: (Untested code)



After Update Business Rule on Table 'u_contrato_esquema_de_servicio':



//After updating Contrato table with the new Company field value, this script queries the Company table for the record referred here and updates the contrato field with current sys_id (since it is a reference field)



var gr = new GlideRecord('core_company');


gr.addQuery('sys_id', current.u_compania);


gr.query();


while(gr.next()){


  gr.u_contrato = current.sys_id;


  gr.update();


}


Thank you so much veena.kvkk88 It works.


dvp
Mega Sage
Mega Sage

Hey Juarez,



Write a after business rule on core_company when the u_compania changes


and use the following script



  var rp = new GlideRecord('u_contrato_esquema_de_servicio');      
  rp.addQuery('sys_id',current.u_contrato);      
  rp.query();


while(rp.next()) {      
  rp.u_compania = current.sys_id;
  rp.update();  
}