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

Musab Rasheed
Tera Sage
Tera Sage

Hello,

Try with look up definition once ? it's no code approach

https://community.servicenow.com/community?id=community_article&sys_id=49deac8ddb92a010ab0202d5ca961967

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

Nice article 🙂

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ha ha thanks to you 🙂

Please hit like and mark my response as correct if that helps
Regards,
Musab

What should  I put in Catalog Matcher Variable definition - Source variable and Matcher table field.

I want to populate the u_privacy_data variable in catalog item with u_privacy_data variable in cmdb_ci_appl table, when I select the service_application_name variable in catalog item