We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Reference field display value to String field on Catalog Item

Shane J
Tera Guru

How can I get a value from a Reference field to populate a String field via an OnChange client script (for a Catalog Item)?   Right now, all I can get is the Sys_ID.   The field needs to get populated initially with the cost_center field, based on the logged in under, but after that it may need to be manually changed (to a non-reference value).

1 ACCEPTED SOLUTION

This will work



Script Include:



var UserDetailsAjax = Class.create();


UserDetailsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {      


      getCostCenterDetails: function()


      {


              var getUserCostDetails = new GlideRecord('sys_user');


              if(getUserCostDetails.get(this.getParameter('sysparm_requested_for')))


                      {


                      return getUserCostDetails.cost_center.getDisplayValue();


              }


      }


});



Client script



var getCost = new GlideAjax('UserDetailsAjax');


getCost.addParam('sysparm_name','getCostCenterDetails');


getCost.addParam('sysparm_requested_for',newValue);


getCost.getXML(setCostCenter);



function setCostCenter(response) {


var answer = response.responseXML.documentElement.getAttribute("answer");


g_form.setValue('cost_center', answer);


}


View solution in original post

10 REPLIES 10

Anurag Tripathi
Mega Patron

Hi Jued



Try this



g_form.getDisplayBox('reference field').value;


-Anurag

How would that play into this OnChange script (or is this the wrong approach entirely)?   I tried using it in a couple different ways but without success.



function onChange(control, oldValue, newValue, isLoading) {


  if(newValue != ""){


  var reqFor2 = g_form.getReference('requested_for');


// g_form.getDisplayBox('reference field').value;


      g_form.setValue('cost_center', reqFor2.cost_center);


  }


}



A screenshot would help us to see your current setup and respond accordingly


Ask and ye shall receive:



2015-01-07_9-20-55.png