Client Script working correctly on the Backend but not on the front end

AdityaK
Tera Contributor

Hello Community, currently I am solving a bug.

There is a Client Script, when it runs, it populates the Machine Name which is assigned to the specific user.

When I enter the name in the field - 'Requested for', automatically all the assets assigned to the user populate in the 'Machine Name' field.

The Client Script works well in the backend, but when I try the same on the portal, it is giving a JavaScript error.

When I try it on the portal, All the assets in the instance get populated into the Machine name field.

 

Client Script-

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.clearValue('does_the_affected_user_have_cell_phones_requiring_investigative_hold');
    var answer = '';
    if (newValue != '') {
        var aj = new GlideAjax('machineName');
        aj.addParam('sysparm_name', 'filter');
        aj.addParam('sysparm_user_name', newValue);
        aj.getXML(machineName);
       
    function machineName(response) {
        answer = response.responseXML.documentElement.getAttribute("answer");
        var collectorName = 'machine_name';
        var filterString = answer;
        eval(collectorName + 'g_filter.reset()');
        eval(collectorName + 'g_filter.setQuery("' + filterString + '")');
        eval(collectorName + 'acRequest(null)');
    }  
       
    }
    //Type appropriate comment here, and begin script below
   
}
 
 
Script Include which the Client Script is calling - 
var machineName = Class.create();
machineName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    filter:function()
    {
        var result = '';
        var user = this.getParameter('sysparm_user_name');
        var us = new GlideRecord('sys_user');
        us.addQuery('sys_id',user);
        us.query();
        us.next();
        var user1 = us.user_name;
        var gr = new GlideRecord('cmdb_ci_computer');    gr.addQuery('assigned_to',user).addOrCondition('u_last_logged_in_user','ENDSWITH',user1).addOrCondition('u_primary_user','ENDSWITH',user1);
        gr.query();
        while(gr.next()){
           
            result = result + gr.asset + ',';
            }
       
       
        return 'sys_idIN'+result;
       
    },

    type: 'machineName'
});

 

 

 

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@AdityaK 

eval won't work in portal.

If you want to set filter then it will work only in native/backend and not in portal.

In portal you have only 1 input and not 2 slush buckets, you can set values directly in portal

check my blog on how to handle that

Dynamically set list collector on change of variable 

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