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

Will try and get back to you

Not Working

@ABC6 

please share your updated scripts and what debugging did you perform so far and what's your findings?

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

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!

HarshithaK
Tera Contributor

Hi @ABC6 

No need of using isUserMember() function, directly call getGroup.addParam('sysparm_name', 'hasExternalReference') instead.


Thanks