Catalog variable value from script

magnushedlu
Tera Guru

Hi,

 

I have a catalog form where one of the variables are a reference to the cmdb for the user to select a server.

Based on this server I would like to have a read only field that dynamically lists any Application Service that has either has a "Depends on" or "Hosted on" type of relationship to the selected server.

How can I achieve this?

 

I assume that I need to work with the cmdb_rel_ci table and I guess I need to write a script in some way. I really appreciate any input I can get.

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@magnushedlu 

yes that's correct.

What type of variable did you create and what ref qualifier you applied?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Rohit  Singh
Mega Sage

Hi @magnushedlu ,

 

You can do the following:

  1. Create On Change Client Script on variable reference "CMDB" table.
  2. Pass the Value of reference variable to script include function.
  3. Create Client callable Script Include.
  4. Do a glide record in cmdb_rel_ci and use addQuery and the value of CI from client script as per requirement. 
  5. Declare array variable (if in case there are more than one CI are getting query)
  6. Push the sys_id in array and use getDisplayValue() so that display of CI's are stored in your readonly variable. 
  7. Return the array
  8. Now in Client Script us g_form.setValue("variable_name",answer)

If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Rohit

sunil maddheshi
Tera Guru

@magnushedlu 

Add a reference variable in your catalog item that allows users to select a server (e.g., referencing cmdb_ci).

Add a read-only variable (e.g., a multi-line text or a list) to display the related Application Services.

se an onChange client script on the server reference variable to query the cmdb_rel_ci table and fetch related Application Services.

Client script: (modify the variable names accordingly)

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }

    var ga = new GlideAjax('GetRelatedAppServices'); // Call Script Include
    ga.addParam('sysparm_server', newValue);
    ga.getXMLAnswer(function(response) {
        g_form.setValue('application_services', response); // Set value to the read-only variable
    });
}

Script inlcude:

 getRelatedServices: function() {
        var serverId = this.getParameter('sysparm_server');
        if (!serverId) return '';

        var serviceList = [];
        var gr = new GlideRecord('cmdb_rel_ci');
        gr.addQuery('parent', serverId); // "Depends on" relationship
        gr.addQuery('type.name', 'IN', 'Depends on, Hosted on'); // Filter by relationship type
        gr.query();

        while (gr.next()) {
            var appService = gr.child.getDisplayValue();
            if (appService && serviceList.indexOf(appService) === -1) {
                serviceList.push(appService);
            }
        }

        return serviceList.join(', ');
    }

 

Please mark correct/helpful if this helps you!

 

Thx for the good input.

Sorry for this noob question, but what table should I set in the client script form to find the form variable?