Make field Non Mandatory on resolve

ABC6
Tera Contributor

Hello Team,

I have a requirement where if there is any external reference record attached to the related list of incident then assigned to field will be non mandatory for which i have written a piece of code over script include and and client script but it is not working as expected below are the code:

Script Include:

isUserMember: function() {
        var userGrp = new GlideRecord('sys_user_grmember');
        userGrp.addQuery('user', this.getParameter('sysparm_usr'));
        userGrp.addQuery('group', this.getParameter('sysparm_grp'));
        userGrp.setLimit(1);
        userGrp.query();
        if (userGrp.next()) {
            return true;
        }
        else{
            return false;
        }

    },
hasExternalReference: function() {
        var incidentId = this.getParameter('sysparm_is_external_reference');
        var incident = new GlideRecord('incident');
        if (incident.get(incidentId)) {
            // Query the related list for external references (replace 'u_external_references' with your actual table name)
            var gr = new GlideRecord('u_external_references');
            gr.addQuery('u_task', incidentId);  // Assuming there's a reference to the incident record
            gr.query();
            if (gr.next()) {
                return 'true'; // If related list has records
            }
        }
        return 'false'; // If no related records exist
    },
   
 
On CHange Client Script:
 if (g_form.getValue('assigned_to') == '') {
            var getGroup = new GlideAjax('ResetAssignee');
            getGroup.addParam('sysparm_name', 'isUserMember');
            getGroup.addParam('sysparm_usr', g_user.userID);
            getGroup.addParam('sysparm_grp', g_form.getValue('assignment_group'));
            getGroup.addParam('sysparm_is_external_reference',g_form.getValue('incident_id'));
            getGroup.getXML(FCRAssignmentGroup);

        }
    }

}

function FCRAssignmentGroup(response) {
    var ans = response.responseXML.documentElement.getAttribute("answer");
    if (ans == true || ans == 'true' ) {
        g_form.setMandatory('assigned_to', false);
    } else {
        g_form.setMandatory('assigned_to', true);
    }

}



1 ACCEPTED SOLUTION

sunil maddheshi
Tera Guru

@ABC6 

Can you try with below updated code,
Script include:

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

    isUserMember: function() {
        var userGrp = new GlideRecord('sys_user_grmember');
        userGrp.addQuery('user', this.getParameter('sysparm_usr'));
        userGrp.addQuery('group', this.getParameter('sysparm_grp'));
        userGrp.setLimit(1);
        userGrp.query();
        return userGrp.hasNext().toString(); // Return "true" or "false"
    },

    hasExternalReference: function() {
        var incidentId = this.getParameter('sysparm_is_external_reference');
        var gr = new GlideRecord('u_external_references'); // Ensure table name is correct
        gr.addQuery('u_task', incidentId);  // Assuming this is the reference to 'incident'
        gr.setLimit(1);
        gr.query();

        return gr.hasNext().toString(); // Return "true" or "false"
    },

    isAssignedToMandatory: function() {
        var isExternalReference = this.hasExternalReference();
        return isExternalReference === "true" ? "false" : "true";
    }
});

Client script:

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

    if (!g_form.getValue('assigned_to')) {
        var getGroup = new GlideAjax('ResetAssignee');
        getGroup.addParam('sysparm_name', 'isAssignedToMandatory'); // Calls the new method
        getGroup.addParam('sysparm_is_external_reference', g_form.getUniqueValue()); // Gets Incident ID

        getGroup.getXMLAnswer(function(response) {
           alert(response);
            if (response === "false") { 
                g_form.setMandatory('assigned_to', false);
            } else {
                g_form.setMandatory('assigned_to', true);
            }
        });
    }
}

 

Please mark correct/helpful if this helps you!

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@ABC6 

so what debugging did you perform from your side and what's your analysis?

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

like applied log, but did not get even any response

@ABC6 

if logs didn't come it means it's not able to call your script include

are you calling the correct function from your GlideAjax?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

lukasz szumilas
Tera Guru

Your script include defines two functions: isUserMember and hasExternalReference. Your client script is calling isUserMember (via setting sysparm_name to 'isUserMember'), but the requirement is to check for an external reference record. I think you need to use hasExternalReference instead.

getGroup.addParam('sysparm_name', 'hasExternalReference');