Variables not working on the form frontend

pk2184046
Tera Expert

pk2184046_0-1728296265273.png

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading){
        return;
    }

    // Clear or populate Subject Person details
    if (newValue == '') {
        g_form.clearValue('subj_name');
        g_form.clearValue('subj_emp_no');
        g_form.clearValue('subj_title');
        g_form.clearValue('subj_emp_entity');
        g_form.clearValue('subj_location');
        g_form.clearValue('subj_manager');
        g_form.clearValue('subj_country');
    } else {
        var glideAjax = new GlideAjax('sn_hr_sp.DXCHRCatalogUtilsAjax');
        glideAjax.addParam('sysparm_name', 'getSubjectPersonDetails');
        glideAjax.addParam('sysparm__user', newValue);
        glideAjax.getXMLAnswer( function (response) {
            var answer = JSON.parse(response);
            if (answer) {
    var unq = g_form.getUniqueValue();
    if (unq != 'f517ff6f1bfdf8d093602f49274bcbd9') {// sys id of the form
        g_form.setValue('subj_name', answer.name);
        g_form.setValue('subj_emp_no', answer.emp_no);
        g_form.setValue('subj_title', answer.title);
        g_form.setValue('subj_emp_entity', answer.emp_entity);
        g_form.setValue('subj_location', answer.location);
        g_form.setValue('subj_manager', answer.manager, answer.manager_dv);
        g_form.setValue('subj_country', answer.country);
            }
            else {
 var subj_person = g_form.getValue('subject_person');
   var reqfor = g_form.getValue('opened_for');// opened_for variable is logged in user which is hidden in the form
 var gr1 = g_form.getReference('subj_person', function(gr1) {
             var div1 = gr1.u_division;
             var gr2 = g_form.getReference('reqfor', function(gr2) {
                         var div2 = gr2.u_division;
            if(div1 == div2){
                   
                g_form.setValue('subj_name', answer.name);
                g_form.setValue('subj_emp_no', answer.emp_no);
                g_form.setValue('subj_title', answer.title);
                g_form.setValue('subj_emp_entity', answer.emp_entity);
                g_form.setValue('subj_location', answer.location);
                g_form.setValue('subj_manager', answer.manager, answer.manager_dv);
                g_form.setValue('subj_country', answer.country);

                }
                else{
                    alert('test');
                g_form.clearValue('subj_name');
                g_form.clearValue('subj_emp_no');
                g_form.clearValue('subj_title');
                g_form.clearValue('subj_emp_entity');
                g_form.clearValue('subj_location');
                g_form.clearValue('subj_manager');
                g_form.clearValue('subj_country');
            }
 });
                    });
            }
        }
});
}
}
 
 
I have variable sets to populate the values of the user. Here I have a requirement to show alert msg whenerver the loggen in user division is different from the subject person it should give an alert msg. For a particular form. This variable set is used across 100 forms all are working fine. But when the sys id of the form is evaluted true. Neither it is giving alert  msg not populating the fields.
Note: script include is working fine as it is working for other forms.
 
 
1 ACCEPTED SOLUTION

pk2184046
Tera Expert

By changing the script include now its working as expected

View solution in original post

4 REPLIES 4

pk2184046
Tera Expert

FYI Below is the used script include

 getSubjectPersonDetails: function() {
        var answer = {};
        var userRec = new GlideRecord('sys_user');
        if (userRec.get(this.getParameter('sysparm__user'))) {
            answer.name = userRec.getDisplayValue('name');
            answer.emp_no = userRec.getDisplayValue('employee_number');
            answer.title = userRec.getDisplayValue('u_position');
            answer.emp_entity = userRec.getDisplayValue('u_employing_entity') || '';
            answer.location = userRec.getDisplayValue('location');
            answer.manager = userRec.getValue('manager');
            answer.manager_dv = userRec.getDisplayValue('manager');
            answer.country = userRec.getDisplayValue('country'); // || GlideLocale.get().getCurrent().getCountry();
            answer.ldap_company = userRec.getDisplayValue('u_ldap_company'); //HRP98
            answer.division = userRec.getDisplayValue('u_division'); //Triage158
            answer.email = userRec.getDisplayValue('email');
        }
        return JSON.stringify(answer);
    },

Rajesh Chopade1
Mega Sage

Hi @pk2184046 

 

Your script looks fine but consider some points that might affecting you:

You have a conditional check for a specific form's sys ID. If this condition evaluates to true, it executes the else block. Ensure that this condition is set correctly and that you are indeed working with the intended form.

You can print log the unique ID to confirm that the expected form is being evaluated.

 

Also you're using 'g_form.getReference(), make sure that any logic that relies on the results of these calls is executed within the callback. For example, if the 'gr1' or 'gr2' references return null, your subsequent logic might not execute as expected.

 

I would suggest use log in your script. This can help identify where the logic may not be behaving as expected.

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

 

Moin Kazi
Kilo Sage
Kilo Sage

Hi @pk2184046 ,

 

Instead of Synchronous Glide Ajax call, can you try Asynchronous Glide Ajax call?

 

Regards

Moin

pk2184046
Tera Expert

By changing the script include now its working as expected