auto populate a field based on a lookup select variable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2017 11:55 AM
I have a catalog item that uses a table (which includes two fields: Name and Cost).
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2017 10:32 AM
Got it working. Here is the new catalog client script
function onChange (control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}else if(newValue == ''){
g_form.clearValue("sw_cost", "");
}else{
//var sw_app = new GlideRecord("u_sw_apps");
//var sel_app = sw_app.get(newValue);
//if(sel_app == true){
// g_form.setValue("sw_cost",sel_app.u_cost);
//}
var ga = new GlideAjax('CostValue');
ga.addParam('sysparm_name','getDetails');
ga.addParam('sysparm_app_select',g_form.getValue('app_select'));
ga.getXML(function(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('sw_cost',answer,answer);
});
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2017 10:35 AM
Perfect. Thank you for the update.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2017 10:37 AM
Yeah sure. I am still interested in Sachin's fix script if I ever need to use it.
Thanks,
Karen

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2017 01:37 PM
Hi Karen,
the issue here is that the variables and responses are not ON the item table they are stored on the mtom and anouther table..
here is another script i use in catalog workflows occasionally.. this needs the sid for the item, and two arrays as an input.. and returns them with the questions in array 1 and the answers in array 2...
function getvariables(incoming_id,question_out,answer_out){
//incoming ID is the sid that contains the item, question out will return an array with questions, answer our will return an array of responces
var item = new GlideRecord("sc_req_item");
item.addQuery('sys_id', incoming_id);
item.query();
while (item.next()) {
var keys = new Array();
var set = new Packages.com.glideapp.servicecatalog.variables.VariablePoolQuestionSet();
set.setRequestID(item.sys_id);
set.load();
var vs = set.getFlatQuestions();
for (var i=0; i < vs.size(); i++) {
if(vs.get(i).getLabel() != '' && (vs.get(i).getLabel() != "Order Guide Name") && (vs.get(i).getLabel() != "Requested for") && (vs.get(i).getLabel() != "Approving manager") && (vs.get(i).getLabel() != "Request manager approval") && (vs.get(i).getDisplayValue() != "-- None --") && (vs.get(i).getDisplayValue() != "") && (vs.get(i).getDisplayValue() != "false")) {
question_out.push(vs.get(i).getLabel());
answer_out.push(vs.get(i).getDisplayValue());
}
}
}
}
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-13-2017 01:50 PM
Hi Karen,
In your case, you can follow below approach
- Create staging table to store data ( before changing data type from reference to string)
- Move data from source to staging table.
- Change data type from reference to string
- move data from staging to target table.
- run fix script to update name based on cost.
Regards,
Sachin