Auto populating fields from one table to another

Spandana P
Tera Contributor

Hello ,

We have a requirement where we have to populated info from one table (say model table) to other (say CI table). For the relation between these two tables, we have this field called Model name on CI table. Requirement is that we populate some of the info like manufacturer, name etc which is present on this model table to CI table. For this I have written a script include and On change client script on CI table (based on model name field changes).

This is working fine but this works only during the load , and when I don't save the form, values are not getting updated.

For example:

manufacturer field present on CI form gets populated from model table's manufacturer when I open the CI, but when i close it without saving, on the list view of CI , I can see manufacturer field is empty. How to populate these values without the need to save them.

Script Include:

var DAS_modeldetails = Class.create();
DAS_modeldetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
requestor_info: function() {
var arr=[];
var output='';
var json = new JSON();
var details=this.getParameter('sysparm_user_name');
var model= new GlideRecord('cmdb_model');
model.addQuery('sys_id',details);
model.query();
while(model.next())
{
var obj = {};
obj.manuf=model.manufacturer.toString();
obj.baseopt=model.u_base_option.toString();
obj.shtname=model.u_short_name.getDisplayValue();
obj.licname=model.u_license_name.getDisplayValue();
obj.licnum=model.u_license_number.getDisplayValue();
obj.name=model.u_name.getDisplayValue();
obj.key=model.u_r_d_key.getDisplayValue();
obj.lic=model.u_licensable.getDisplayValue();

arr.push(obj);
}
gs.log('value is :'+ json.encode(arr));
return (json.encode(arr));
},
type: 'DAS_modeldetails'
});

On change client script (model name field):

function onChange(control, oldValue, newValue, isLoading) {
if (newValue === '') {   //here, I removed isLoading flag because they wanted to see model field values populating in CI during form load also
return;
}
var table = g_form.getTableName();
if (table == 'u_cmdb_ci_product_line' || table == 'u_cmdb_ci_product_group'|| table == 'u_cmdb_ci_product'|| table == 'u_cmdb_ci_component' )
{
//Type appropriate comment here, and begin script below
var model = g_form.getValue('u_model');
var ga = new GlideAjax('DAS_modeldetails');
ga.addParam('sysparm_name','requestor_info');
ga.addParam('sysparm_user_name',model);
ga.getXML(autopopulate);
function autopopulate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var ans = answer.evalJSON();
for (var k = 0; k < ans.length; k++) {
g_form.setValue('manufacturer',ans[k].manuf);
g_form.setValue('u_base_option',ans[k].baseopt);
g_form.setValue('u_short_name',ans[k].shtname);
g_form.setValue('u_license_name',ans[k].licname);
g_form.setValue('u_license_number',ans[k].licnum);
var nam = g_form.getValue('u_name');
if(nam =="" ){
g_form.setValue('u_name',ans[k].name);
}
g_form.setValue('key',ans[k].key);
g_form.setValue('u_licensable',ans[k].lic);
}
}
}
}

 

They can manually save , but it becomes a problem during bulk data import. Please kindly suggest on this.

 

 

 

Regards,

JP.

 

3 REPLIES 3

Vismit Ambre
Giga Guru

There has to be some BR or an onSubmit client script that is not allowing the values to save or clearing them out. This generally happens in case of cross-relationship tables.

Regards,

Vismit

Hello,

I checked but there is no BR or client script that is clearing the values. Could you please suggest if there is any possible way to save the changes during this form load itself, like if there is any part of code to be added to the same script to save these changes.

I'm sorry, I will have to implement the script in my PDI to provide you any solution then. I will let you know if I find anything on this.

 

Regards,

Vismit