Auto Populate catalog item variable from a table's variable

Shalika
Tera Expert

I have a variable service_application_name which is a reference variable and it is referring to cmdb_ci_appl table. 

The other variable is u_privacy_data which is a multiple choice having values 'Yes' and 'No'.

Now, when I select a value in service_application_name then, it should fetch the data from  cmdb_ci_appl table and autopopulate  "u_privacy_data" variable. 

I am using client script and script include as follows, but it is not working --

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

 if (newValue == '') {
      g_form.setValue('u_privacy_data', '');
   }

    var ga = new GlideAjax('privacydatacheck');
 
    ga.addParam('sysparm_name','getProductCode');
    ga.addParam('sysparm_ci', g_form.getValue('servicenow_application_name'));
    ga.getXML(doPopulate);
}
 
function doPopulate(response) {
 
    var answer = response.responseXML.documentElement.getAttribute("answer");
 
    g_form.setValue('u_privacy_data', answer);
}
 

Script Include ---

var privacydatacheck = Class.create();
 
productCodeCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getPrivacyData: function() {
 
        var retVal = '';
        var ciID   = this.getParameter('sysparm_ci');        
        var ci  = new GlideRecord('cmdb_ci_appl'); 
        // If Business Service found, return its product code
        if (ci.get(ciID)) {
            if (ci.u_privacy_data != '') {
                retVal = ci.u_privacy_data;
            } 
        }
        return retVal;
    }  
    
});
   

1 ACCEPTED SOLUTION

Then you don't need to write 3 different function for this, you can get all 3 variables via only one function and one client script.

Client Script :

function onChange(control, oldValue, newValue, isLoading) {
 if (newValue == '') {
      g_form.setValue('u_product_code', '');
   }
    var ga = new GlideAjax('productCodeCheck');
    ga.addParam('sysparm_name', 'getProductCode');
    ga.addParam('sysparm_ci', newValue);
    ga.getXML(doPopulate);
// Callback function to process the response returned from the server
function doPopulate(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    answerJson=JSON.parse(answer);
    g_form.setValue('u_product_code', answerJson.u_product_code);
   g_form.setValue("u_privacy_data",answerJson.u_privacy_data);
   g_form.setValue("add email field name herer",answerJson.email);
}
}
 

Script Include Function :

getProductCode: function() {
 
        var productCode= '',privacyData="",email="";
        var ciID   = this.getParameter('sysparm_ci');        
        var ci  = new GlideRecord('cmdb_ci_appl'); 
        // If Business Service found, return its product code
        if (ci.get(ciID)) {
            if (ci.u_product_code != '') {
                productCode= ci.u_product_code;
            } 
         if (ci.u_privacy_data != '') {
                privacyData= ci1.u_privacy_data;
            } 
         if (ci2.u_ict_owner.email != '') {
                email= ci.u_ict_owner.email;
            } 
        }
      var resultData = {
             "u_product_code": productCode,
             "u_privacy_data": privacyData,
              "email":email
        };  
        return JSON.strigify(resultData);
    }  ,


 
Please mark this as Correct or Helpful based on the impact.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

15 REPLIES 15

I want to fetch the details of u_product_code, u_privacy_data and u_ict_owner email's from cmdb_ci_appl table.

I have written 3 different functions for them in the same script include as follows -

Script Include --

var productCodeCheck = Class.create();
 
productCodeCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getProductCode: function() {
 
        var retVal = '';
        var ciID   = this.getParameter('sysparm_ci');        
        var ci  = new GlideRecord('cmdb_ci_appl'); 
        // If Business Service found, return its product code
        if (ci.get(ciID)) {
            if (ci.u_product_code != '') {
                retVal = ci.u_product_code;
            } 
        }
        return retVal;
    }  ,
    
    getPrivacyData: function() {
 
        var retVal1 = '';
        var ciID1   = this.getParameter('sysparm_ci');        
        var ci1  = new GlideRecord('cmdb_ci_appl'); 
       
        if (ci1.get(ciID1)) {
            if (ci1.u_privacy_data != '') {
                retVal1 = ci1.u_privacy_data;
                
            } 
        }
        return retVal1;
    }  ,
    
    getITResponsibleEmail: function() {
 
        var retVal2 = '';
        var ciID2   = this.getParameter('sysparm_ci');        
        var ci2  = new GlideRecord('cmdb_ci_appl'); 
       
        if (ci2.get(ciID2)) {
            if (ci2.u_ict_owner.email != '') {
                retVal2 = ci1.u_ict_owner.email;
            } 
        }
        return retVal2;
    }  
    
    
});

Then I have written 3 different client scripts as follows ---

function onChange(control, oldValue, newValue, isLoading) {
 if (newValue == '') {
      g_form.setValue('u_product_code', '');
   }

    var ga = new GlideAjax('productCodeCheck');
 
    ga.addParam('sysparm_name', 'getProductCode');
    ga.addParam('sysparm_ci', g_form.getValue('servicenow_application_name'));
    ga.getXML(doPopulate);
}
 
// Callback function to process the response returned from the server
function doPopulate(response) {
 
    var answer = response.responseXML.documentElement.getAttribute("answer");
 
    g_form.setValue('u_product_code', answer);
}

Similary, I have written client scripts for fetching privacy data and IT Responsible's email

I am able to fetch only u_product_code.

OnChange of 1 field, you want to set 3 different fields (u_product_code,privacy data and IT responsible email)? or onChange of 3 different fields, you are setting 3 different fields?

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

OnChange of 1 field, i.e.  service_application_name, I want to fetch 3 different fields (u_product_code,privacy data and IT responsible email)

Then you don't need to write 3 different function for this, you can get all 3 variables via only one function and one client script.

Client Script :

function onChange(control, oldValue, newValue, isLoading) {
 if (newValue == '') {
      g_form.setValue('u_product_code', '');
   }
    var ga = new GlideAjax('productCodeCheck');
    ga.addParam('sysparm_name', 'getProductCode');
    ga.addParam('sysparm_ci', newValue);
    ga.getXML(doPopulate);
// Callback function to process the response returned from the server
function doPopulate(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    answerJson=JSON.parse(answer);
    g_form.setValue('u_product_code', answerJson.u_product_code);
   g_form.setValue("u_privacy_data",answerJson.u_privacy_data);
   g_form.setValue("add email field name herer",answerJson.email);
}
}
 

Script Include Function :

getProductCode: function() {
 
        var productCode= '',privacyData="",email="";
        var ciID   = this.getParameter('sysparm_ci');        
        var ci  = new GlideRecord('cmdb_ci_appl'); 
        // If Business Service found, return its product code
        if (ci.get(ciID)) {
            if (ci.u_product_code != '') {
                productCode= ci.u_product_code;
            } 
         if (ci.u_privacy_data != '') {
                privacyData= ci1.u_privacy_data;
            } 
         if (ci2.u_ict_owner.email != '') {
                email= ci.u_ict_owner.email;
            } 
        }
      var resultData = {
             "u_product_code": productCode,
             "u_privacy_data": privacyData,
              "email":email
        };  
        return JSON.strigify(resultData);
    }  ,


 
Please mark this as Correct or Helpful based on the impact.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

No value fetched 😞