Unable to clear dependent values when a Reference Field is cleared.

denisdoucet
Tera Guru

Hello,

I have functionality on my application where when you lookup an agent it populates 4 other fields.
This functionality is working perfectly fine, the issue I'm coming across is when I clear the reference field (u_name),
the other 4 fields retain their values.

I'm looking for help on how to clear the number, advisor region, district and manager fields once the Name field is cleared.

I've attached my info below.

Any help would be great.

find_real_file.png

//Script Include

var AdvisorInformation = Class.create();
AdvisorInformation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getAdvisorInfo: function (){

var advisorArray = [];
var advisorDetails = new GlideRecord ('sys_user');
advisorDetails.addQuery('sys_id',this.getParameter('sysparm_advisorName'));
advisorDetails.query();

while(advisorDetails.next()){

var advisor = {};
advisor.number = advisorDetails.u_primary_agent_number.toString();
advisor.region = advisorDetails.location.u_region.toString();
advisor.district = advisorDetails.location.u_district.toString();
advisor.manager = advisorDetails.manager.getDisplayValue();
advisorArray.push(advisor);

}

return JSON.stringify(advisorArray);
},

type: 'AdvisorInformation'
});

 

//Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}


if (!newValue){

g_form.clearValue('u_advisor_number');
g_form.clearValue('u_advisor_region');
g_form.clearValue('u_advisor_district');
g_form.clearValue('u_district_manager');

}

var advisorGA = new GlideAjax ('AdvisorInformation');
advisorGA.addParam('sysparm_name', 'getAdvisorInfo');
advisorGA.addParam('sysparm_advisorName', newValue);
advisorGA.getXMLAnswer(AdvisorInformationParse);

function AdvisorInformationParse(response){

var myObj = JSON.parse(response);

for (var i=0; i< myObj.length; i++){
g_form.setValue('u_advisor_number',myObj[i].number);
g_form.setValue('u_advisor_region',myObj[i].region);
g_form.setValue('u_advisor_district',myObj[i].district);
g_form.setValue('u_district_manager',myObj[i].manager);
}
}
}

1 ACCEPTED SOLUTION

Elijah Aromola
Mega Sage

You're only clearing the value when there isn't a new value. When the field is erased there is still a value there. Make the following changes to your code.

 

if (isLoading) {
  return;
}

 

AND 

if (newValue == ""){


g_form.clearValue('u_advisor_number');
g_form.clearValue('u_advisor_region');
g_form.clearValue('u_advisor_district');
g_form.clearValue('u_district_manager');

}

 

Please mark this as helpful is this resolved your question.

View solution in original post

3 REPLIES 3

Shane J
Tera Guru

Do you have this piece setup appropriately as an OnChange Client Script?

 

//Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}


if (!newValue){

g_form.clearValue('u_advisor_number');
g_form.clearValue('u_advisor_region');
g_form.clearValue('u_advisor_district');
g_form.clearValue('u_district_manager');

}

 

 

Elijah Aromola
Mega Sage

You're only clearing the value when there isn't a new value. When the field is erased there is still a value there. Make the following changes to your code.

 

if (isLoading) {
  return;
}

 

AND 

if (newValue == ""){


g_form.clearValue('u_advisor_number');
g_form.clearValue('u_advisor_region');
g_form.clearValue('u_advisor_district');
g_form.clearValue('u_district_manager');

}

 

Please mark this as helpful is this resolved your question.

Thank you, this resolved my issue!