Catalog client script help

Thomas98
Tera Expert

Hi All, 

 

Can someone please help me with the catalog client script?

 

I have two field 

1) select_the_type_of_access_required 

2)  u_description 

I need an on change catalog item script 

when I select the "type of access" the description should auto fill itself. 

Custom table name " u_Azure_role

 

I have something in place but it wont work 😞 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
      return;
   }var user = g_form.getReference('select_the_type_of_access_requireds', populateDetails);

function populateDetails(user) {
    g_form.setValue('description',u_azure_roles.u_description);
}
}

 

 

 

 

3 ACCEPTED SOLUTIONS

Harsh_Deep
Giga Sage
Giga Sage

Hello @Thomas98 ,

 

Is select_the_type_of_access_requireds a reference field? 

If so, replace

 

g_form.setValue('description',u_azure_roles.u_description);

with

g_form.setValue('description',user.u_description);

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

View solution in original post

Harsh_Deep
Giga Sage
Giga Sage

Hello @Thomas98 

You have to use Script Include Call using client script-

 

https://www.servicenow.com/community/developer-articles/how-to-call-script-include-from-client-scrip...

 

Please open the above link and complete your requirement.

 

Please mark correct answer and helpful for others if it helps you
Thanks,

View solution in original post

Hello @Thomas98 

 

In line number 7th you are passing role but you haven't defined role anywhere.

View solution in original post

17 REPLIES 17

Harsh_Deep
Giga Sage
Giga Sage

Hello @Thomas98 

You have to use Script Include Call using client script-

 

https://www.servicenow.com/community/developer-articles/how-to-call-script-include-from-client-scrip...

 

Please open the above link and complete your requirement.

 

Please mark correct answer and helpful for others if it helps you
Thanks,

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @Thomas98 ,

For lookup select box, you have to use GlideAjax to call the script include and get the value.

You can create catalog client script using the below code:-

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getLookupDescription');
    ga.addParam('sysparm_name', 'getDescription');
    ga.addParam('sysparm_number', newValue); //newValue will look up field for
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('description', answer);

    }

}

Also, create script include with name 'getLookupDescription' and check the client callable check box.

and write the below script and according to the comment of line 6,7 & 10 replace the required data:-

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

    getDescription: function() {
        var num = this.getParameter('sysparm_number');
        var gr = new GlideRecord("incident"); //replace the table name with the table that you selected in lookup variable
        gr.addQuery("number", num); // replace 'number' with the field backend name that you have selected in lookup variable
        gr.query();
        if (gr.next()) {
            return gr.description.toString(); //replace description with the backend name of the description field of the table that you selected in lookup variable
        }
    },

    type: 'getLookupDescription'
});

 for reference:-

Alka4_0-1697610600982.png

 

If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks,

Alka

Thank you Alka for taking the time. 

I still couldn't get it working 😞 could you please validate my code: 

 

 

Script Include:

 

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

    getDescription: function() {
        var num = this.getParameter('sysparm_number');
        var gr = new GlideRecord("u_azure_roles"); //replace the table name with the table that you selected in lookup variable
        gr.addQuery("u_role", role); // replace 'number' with the field backend name that you have selected in lookup variable
        gr.query();
        if (gr.next()) {
            return gr.u_description.toString(); //replace description with the backend name of the description field of the table that you selected in lookup variable
        }
    },

    type: 'getLookupDescription'
});

 

 

Catalog client script: 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getLookupDescription');
    ga.addParam('sysparm_name', 'getDescription');
    ga.addParam('sysparm_number', newValue); //newValue will look up field for
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('description', answer);

    }

}
 
 
variable:
 
 
Thomas98_0-1697634568962.png

 

 

Azure table fields:

u_role

u_description 

@Thomas98 , what is the variable name of description where you want to display description from table?

@Thomas98 Also, did you make the script include client callable and with the name 'getLookupDescription'? Send me the screenshots.