auto populate a field based on a lookup select variable

Cupcake
Mega Guru

I have a catalog item that uses a table (which includes two fields: Name and Cost).

find_real_file.png

In the catalog item form - the name fields is a "Lookup select box type", and the cost field is a single line text

I want to auto populate the value of the cost based on the name selected from the lookup select box.

My script is not working - this is what i have. NOTE: The script will work perfectly if I change the name field to a reference field - but the issue with that is the existing tickets will loose the value selected.

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

      if (newValue == '') {

              g_form.setValue("sw_cost", "");

                                             

              return;

      }

     

      var u_sw_apps = g_form.getReference('app_select',CallBack);

}

function CallBack(u_sw_apps) {

      g_form.setValue("sw_cost", u_sw_apps.u_cost);

     

}

Any assistance is most appreciative.

Thank you,

Karen

24 REPLIES 24

Thank you I will try this today.


Good day Shishir - I tried this and it is still not pulling back the value. Here is my script:


Client Script:


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



if (isLoading || newValue == '') {


      //g_form.setValue('sw_cost', '');


return;


}


// Type appropriate comment here, and begin script below


var ga = new GlideAjax("getCostValue");


ga.addParam("syparm_name","getDetails");


ga.addParam("sysparm_app_select", g_form.getValue('app_select'));


ga.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


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


g_form.setValue('sw_cost', answer);


}


}



Script Include:


var CostValue = Class.create();


getCostValue.prototype = Object.extendsObject (AbstractAjaxProcessor, {


getDetails: function() {


var newRecord = new GlideRecord ('sw_apps');


if(newRecord.addQuery ('sw_cost', this.getParameter('sysparm_app_select')))


return newRecord.getValue('sw_cost');


},


type: 'getCostValue'


});



Can you have a look at it?



Thanks,


Karen


small correction in script include:



var getCostValue = Class.create();


getCostValue.prototype = Object.extendsObject (AbstractAjaxProcessor, {


getDetails: function() {


var newRecord = new GlideRecord ('sw_apps');


if(newRecord.addQuery ('sw_cost', this.getParameter('sysparm_app_select')))


return newRecord.getValue('sw_cost');


},


type: 'getCostValue'


});


Still not working.



Here is my table information and the field information (on the catalog form itself).




find_real_file.png                  



These are the variables that are on the form:



find_real_file.png


okay, so based upon the app name (which is pointing to name field in u_sw_apps table) cost has to be auto populated? If yes, please try below script see if this helps.



var getCostValue = Class.create();


getCostValue.prototype = Object.extendsObject (AbstractAjaxProcessor, {


getDetails: function() {


var newRecord = new GlideRecord ('u_sw_apps');


if(newRecord.addQuery (this.getParameter('sysparm_app_select')))


return newRecord.getValue('u_cost'); //Please check the cost field name if that's correct or not?


},


type: 'getCostValue'


});