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

@sakshi18 The script suggested in the article would still work even if it is applied on form and not on the catalog item.

Chaitanya ILCR
Mega Patron

Hi @sakshi18 ,

The onSubmit client script is primarily used for validation purposes before the database operation. It doesn't seem like your script is performing any validation.

 

It's better to use a Before Business Rule (BR) looking at your script looks like it's on table(RITM). If you're working at the catalog level, using a workflow or flow to set the values would be more appropriate

 

 

if you want to validate follow @Sandeep Rajput's approach

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

@Chaitanya ILCR  Thank you for the reply

i have written a BR, and it is working
i also want to know why my onSubmit script is not working

 

also we are making this automated, there is no Catalog item used to create records

Hi @sakshi18 ,

yes BR suits your requirement

The reason is simple GlideAjax getXML method runs asynchronously. by the time script gets the response from server the form will move ahead get's submitted.

OnSubmit is meant for validations-> if  you want to validate if the user's inputs and alert them we use onsubmit client script.

 

 

coming to your other question you want to create just RITM? without the submitting a form?

I would say check comments in the below thread

https://www.servicenow.com/community/developer-forum/how-to-create-scheduled-task/m-p/3213788#M11892...

 

if this helped

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

 

Medi C
Giga Sage

Hello @sakshi18,

 

Could you please adjust your client script as follow:

function onSubmit() {
    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);

    // Stop the form submission until we process the async response
    return false;

    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('description', answer);
        // If you're writing to a list field like u_assignment_group, you can do:
        // g_form.setValue('u_assignment_group', answer);

        // Now resubmit the form programmatically
        g_form.submit();
    }
}

If you're setting a List field (like u_assignment_group), make sure the answer is a comma-separated list of sys_ids.

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.