GlideAjax question

tanz
Tera Expert

I want to write a client script which will perform some actions (display alert that number or date is empty ) in catalog item when the number and date fields from table xyz .

 

For this GlideAjax script should be written which should check on the records and return some flag if one of fields in empty.

 

i want to know how to achieve this.

var gr = new GlideRecord('xyz');
gr.addQuery('sys_id','IN' ,sysIds); // pass the sysids in the glideajax query param
gr.query();
{ if any one of the number or date is empty 
return which field is empty against which number
}
In client script 
alert those and do some action
The sys ids will be multiple like sys id in sysid1,sysid2,sysid3
as the field in client script is list collector in which user can input multiple records from xyz table
8 REPLIES 8

Dibyaratnam
Tera Sage

What is the question?

can you share the script on how to achieve this

Ambuj Tripathi
ServiceNow Employee
ServiceNow Employee

Create a new Script Include in the sys_script_include table with client_callable field as true.

---Sample script structure---

var YourScriptIncludeName = Class.create();
YourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    yourSImethodName: function () {
        //Do the processing here..
    },
    type: 'YourScriptIncludeName'
});

 

---Sample client call script---

var ajax = new GlideAjax("YourScriptIncludeName");
ajax.addParam("sysparm_name", "yourSImethodName");
ajax.addParam("sysparm_param1", "randomeValue");
ajax.addParam("sysparm_param2", "someOtherValue");
ajax.getXML(this.processResponse.bind(this));
 
This client script will execute the scripted API, fetch the result and will call the processResponse method of the client script in which further actions can be taken based on the result.
 
In your case, the GlideRecod query will go into the "yourSImethodName" and based on the call, the result will be processed in processResponse method where you can show the message based on the result outcome.

Krushna R Birla
Kilo Sage

Hi @tanz 

 

I didn't completely understand your question. From what I gather, you want to use GlideAjax in a client script to set values in a list collector field

 

You can refer below code and try to modify it as per your requirement.

 

Client Script:

 

function onLoad() {
    var openedForValue = g_form.getValue('opened_for');
    if (openedForValue) {
        var ga = new GlideAjax('MyScriptInclude');
        ga.addParam('sysparm_name', 'getSerialNumbers');
        ga.addParam('sysparm_opened_for', openedForValue);
        ga.getXMLAnswer(function (answer) {
            if (answer) {
                var serialNumbers = answer.split(',');
                g_form.setValue('serial_numbers', serialNumbers.join(','));
            }
        });
    }
}

 

Client Callable Script Include:

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

    getSerialNumbers: function () {
        var openedForSysId = this.getParameter('sysparm_opened_for');
        var serialNumbers = [];
        var gr = new GlideRecord('alm_asset');
        gr.addQuery('assigned_to', openedForSysId);
        gr.query();
        while (gr.next()) {
            serialNumbers.push(gr.serial_number.toString());
        }
        return serialNumbers.join(',');
    },

    type: 'MyScriptInclude'
});

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Best Regards,
Krushna Birla