Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

Trying to auto-populate the configuration item field based on the requested for field

BenFan
Mega Guru

Hi,

I am trying to get a request to auto-populate the user's assigned PC in the Configuration Item field. Still, after creating a client-callable script, a catalog client script, and a regular client script, the catalog client script did not work.

None are working. (See autopopci-1 - autopopci-3)

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron

@BenFan Here are some improvements to make this work.

 

autopopci-3.jpegautopopci-2.jpeg

 

Hope this helps.

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron

@BenFan Here are some improvements to make this work.

 

autopopci-3.jpegautopopci-2.jpeg

 

Hope this helps.

Tanushree Maiti
Tera Patron

Hi @BenFan 

 

You can try this as well.

 

1.Create the Catalog Client Script:

  • Navigate to Service Catalog > Catalog Policy > Catalog Client Scripts.
  • Click New.
  • Set Name (e.g., Auto-populate Assigned PC).
  • Set UI Type to All.
  • Set Type to onChange.
  • Set Variable set or Catalog Item accordingly, and choose the requested_for variable.

Add this sample script:

 

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

    if (isLoading || newValue === '') {

        return;

    }

    var requestedFor = g_form.getValue('requested_for');// Replace 'requested_for' with your actual variable name

    if (requestedFor) {

        var ga = new GlideAjax('populateReqForPC');

        ga.addParam('sysparm_name', 'getAssignedPC');

        ga.addParam('sysparm_user_id', requestedFor);

        ga.getXMLAnswer(function(response) {

            if (response) {

                g_form.setValue('cmdb_ci', response);

            }

        });

    }

}

 

2. Create the Script Include (Server-side logic):

  • Navigate to System Definition > Script Includes.
  • Client Callable : checked

 

var populateReqForPC= Class.create();

populateReqForPC.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    getAssignedPC: function() {

        var userId = this.getParameter('sysparm_user_id');

        var gr = new GlideRecord('cmdb_ci_computer');

        gr.addQuery('assigned_to', userId);

        gr.query();

        if (gr.next()) {

            return gr.sys_id;

        }

        return '';

    },

 

    type: 'populateReqForPC'

});

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti