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

That should work.

please add gs.addInfoMessage on script include to make sure that values are getting set properly as shown below, you can also try adding alert in client script to see values coming in answerJson.

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
        };  
gs.addInfoMessage("Product Code : "+resultData.u_product_code+"Privacy Data:"+resultData.u_privacy_data+"Email:"+resultData .email);
        return JSON.strigify(resultData);
    }  ,

Please share result to analyse it further

If you are still facing problem then please share your updated script also.

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