Set Display Value on a field using client script

Sriharsha3
Tera Contributor

Hi i need to copy field display value of a reference field parent_incident to u_parent_incident(String field).

Kindly check the script  which doesnt work and suggest

function onLoad() {

 

 

 

var tes = g_form.getValue('parent_incident') ; //reference field

 

var test2=g_form.getValue('u_parent_incident'); //

 

 

 

if(test2== '' )

 

 

 

{

 

 

 

g_form.setDisplayValue('u_parent_incident',tes.number);

 

}

 

}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Param,

That won't work. you need to use getReference with callback as recommended

function onLoad() {

var incident = g_form.getReference(‘parent_incident’, myCallbackFunction);

}

function myCallbackFunction(incident){

g_form.setValue('u_parent_incident', incident.number);

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Param,

That won't work. you need to use getReference with callback as recommended

function onLoad() {

var incident = g_form.getReference(‘parent_incident’, myCallbackFunction);

}

function myCallbackFunction(incident){

g_form.setValue('u_parent_incident', incident.number);

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

amlanpal
Kilo Sage

Hi,

 

You can not do dot walk like tes.number at the client side. For this either you can refer getReference method or go for GlidaAjax. Check this out for your script reference: 

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_GlideFormGetReference_String_Function

 

I hope this helps.Please mark correct/helpful based on impact

manish64
Giga Guru

var tes = g_form.getDisplayValue('parent_incident')  and g_form.setValue('u_parent_incident',tes);

it will work

Shalaka
Tera Contributor

function onLoad() {

//var tes = g_form.getDisplayValue('parent_incident') ; //reference field
var tes = g_form.getDisplayBox('parent_incident').value;
//alert(tes);
var test2=g_form.getValue('u_parent_incident'); //

if(test2== ' ' )

{

g_form.setDisplayValue('u_parent_incident',tes);

}

}

 

When you use g_form.getValue for reference fields, it will provide a sys_id of the record.

You can use g_form.getDisplayValue('fieldname').value function or else use scratchpad(using display business rule).

 

Thanks,

Shalaka