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
Tera Contributor

@Medi C  Thank you for the reply

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

sakshi18
Tera Contributor

Greetings,

@Medi C , @Chaitanya ILCR , @Sandeep Rajput , @Vishal Jaswal - Thank you for the replies.

This is an automated process where the RITM records need to get created based on few no of days related conditions/filters used on a different table (ex. expiry date of a request). i tried writing a scheduled job and a flow chart for this. And both are working fine in creation of data and addition of values.

 

But what would be the best approach for creating of records (flow or Scheduled job that runs at night).

Note: we have checked an approx no of records for 8 days. it has max of 250 and min of 20. The number will increase when the application goes live.

Please suggest me the best approach.

Thank you.

@sakshi18 

Better to go with flow (low-code, no-code) approach and easy to maintain.

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

vinay167
Tera Contributor

Hello Sakshi, Please try below code

function onSubmit() {
// Get the value of the 'requested_for' field
var reqFor = g_form.getValue('requested_for');

// Create a GlideAjax object to call the server-side script
var fetchValue = new GlideAjax('Automation_Client_UTILS');
fetchValue.addParam('sysparm_name', 'fetchGroup'); // Specify the function to call
fetchValue.addParam('sysparm_sysID', reqFor); // Pass the sysID of the requested_for user

// Use an asynchronous call with getXMLAnswer
fetchValue.getXMLAnswer(function(answer) {
// Set the value of the 'description' field with the response from the server
g_form.setValue('description', answer);

// Programmatically submit the form after receiving the response
g_form.submit();
});

// Prevent the default form submission during the initial call
return false;
}
please mark helpful if it works.

Ankur Bawiskar
Tera Patron
Tera Patron

@sakshi18 

why not use before insert/update business rule and set the value?

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