set value on list field using client script

sakshi18
Tera Contributor

Greetings,

We have created an onSubmit Client Script to fetch groups based on name, and save it to a list field.

the values are not getting saved. 

we tried to set values on a string field and we see that the values getting entered and then cleared out after few seconds.

Client Script:

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var reqFor = g_form.getValue('requested_for');
    var fetchValue = new GlideAjax('Automation_Client_UTILS');
    fetchValue.addParam('sysparm_name', 'fetchGroup');
    fetchValue.addParam('sysparm_sysID', reqFor);
    fetchValue.getXML(getResponse);
    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        g_form.setValue('description', answer);
        // g_form.setValue('u_assignment_group', answer);
    }
}
Script include:
fetchGroup: function() {
        var reqFor = this.getParameter('sysparm_sysID');
        var cityName = '';
        var userGlide = new GlideRecord('sys_user');
        userGlide.addEncodedQuery('sys_id=' + reqFor);
        userGlide.query();
        if (userGlide.next()) {
            cityName = userGlide.u_psa_text;
        }
        var group = '';
        var glide = new GlideRecord('sys_user_group');
        glide.addEncodedQuery('nameSTARTSWITHL2 - EUC^nameLIKE' + cityName);
        glide.query();
        while (glide.next()) {
            group += glide.sys_id + ', ';
        }
        return group;
    },
 
We are getting correct values in alert but the values are not getting saved in description(String) or u_assignment_group(list) fields.
Thank you,
Sakshi.
14 REPLIES 14

Vishal Jaswal
Giga Sage

Hello @sakshi18 

 

You can relate this scenario as a race between Submit (submission of form/record) vs asynchronous GlideAjax and you have already figured that Submission of the form/record is winner because before your getResponse callback is executed, the form submission is already done.

 

Solution: you need to ensure that the form submission waits for the GlideAjax call to complete. You can achieve this by returning false from the onSubmit function to prevent the form from submitting immediately.

 

So in your client script make two changes:

add return false;

below this line: fetchValue.getXML(getResponse);

 

and add g_form.submit();

below this line:  g_form.setValue('description', answer);


Hope that helps!

Thank you for the reply @Vishal Jaswal ,

i tried this 
but the form is loading on a loop and won't stop.

Sandeep Rajput
Tera Patron
Tera Patron

@sakshi18 This issue is occurring because by the time your GlideAjax call return the response the form is already submitted and the value in the filed is not saved.

 

Please refer to this article https://www.servicenow.com/community/developer-articles/async-validation-in-onsubmit-catalog-client-... where the steps to handle this is mentioned.

 

If you wish to handle this in Service portal then you can refer to https://snowexpertrohit.com/itsm-code/glideajax-in-portal/

 

Hope this helps.

@Sandeep Rajput Thanks for the reply,

we are doing this on form and catalog is not involved