How can I set reference field with sys_id?

richgk
Kilo Contributor

I have the following code:

var gr = new GlideRecord('new_call');

gr.addQuery('transferred_to',current.request); // we're looking for the original CALL

gr.query();                               // perform the query

// If the call that was transferred to this request task was found

if (gr.getRowCount() == 1) {

      while (gr.next()){

              gs.log('>> RGK <<' + gr.number);

              gs.log('>> RGK <<' + gr.sys_id);

              current.u_call_number = gr.sys_id;

      }

}

Which produces the following output:

Information       >> RGK <<CALL0004026

Information       >> RGK <<f87c10b4dba083402d4dfb041d9619e4

The field on the current form named u_call_number is a reference to a call (new_call table), but when setting this field to be the sys_id the form does not display anything. The 2nd line in the log output looks to be the sys_id for the CALL so what can be wrong?

Thanks,

Rich.

1 ACCEPTED SOLUTION

ruthrav
Mega Guru

Hello Richard,


        Please use current.update() function to update the record after setting the value.


like


var gr = new GlideRecord('new_call');


gr.addQuery('transferred_to',current.request); // we're looking for the original CALL


gr.query();                               // perform the query



// If the call that was transferred to this request task was found



if (gr.getRowCount() == 1) {


      while (gr.next()){


              gs.log('>> RGK <<' + gr.number);


              gs.log('>> RGK <<' + gr.sys_id);


              current.u_call_number = gr.sys_id;


                  current.update();


      }


}


View solution in original post

15 REPLIES 15

The field named 'Number' which is a string is already ticked true.


ruthrav
Mega Guru

Hello Richard,


        Please use current.update() function to update the record after setting the value.


like


var gr = new GlideRecord('new_call');


gr.addQuery('transferred_to',current.request); // we're looking for the original CALL


gr.query();                               // perform the query



// If the call that was transferred to this request task was found



if (gr.getRowCount() == 1) {


      while (gr.next()){


              gs.log('>> RGK <<' + gr.number);


              gs.log('>> RGK <<' + gr.sys_id);


              current.u_call_number = gr.sys_id;


                  current.update();


      }


}


Please don't use current.update() if you are running the code in before Business Rule.


richgk
Kilo Contributor

Many thanks! This did the trick 🙂


Hello Richard,



Just to add to what Shishir said.


Never use current.update() in business rule.


You can instead run your business rule on Before



Check this link for more info


Never use current.update in a Business Rule