Conditional mandatory field support

PK14
Kilo Guru
Hi Team,

I'm working on the Change module and have a requirement where the Assigned to field on the Change Task should behave as follows:
It should be mandatory for all states when the Organization (from the parent Change Request) is ABC.It should be mandatory only when the state is 'Closed Complete' or 'Skipped' if the Organization is XYZ.

Please note that the Organization field (u_organization) resides on the parent table Change Request, not on the Change Task itself. I’ve implemented the following onLoad client script, but it's not working as expected. Could you please help review and suggest any corrections?

(function onLoad() {
  g_form.getReference('change_request', function(parent) {
    if (parent) {
      var org = (parent.u_organization || '').toString().trim();
      enforceAssignedToMandatory(org);
    }
  });  g_form.onChange('state', function(control, oldValue, newValue, isLoading) {
    if (!isLoading) {
      g_form.getReference('change_request', function(parent) {
        if (parent) {
          var org = (parent.u_organization || '').toString().trim();
          enforceAssignedToMandatory(org);
        }
      });
    }
  });  function enforceAssignedToMandatory(org) {
    var state = g_form.getValue('state');
    console.log('Org:', org, 'State:', state); // Debugging  
  if (org === 'ABC') {

      g_form.setMandatory('assigned_to', true);
    } else if (org === 'XYZ') {
      if (state === '3' || state === '4') { // Closed Complete or Skipped
        g_form.setMandatory('assigned_to', true);
      } else {
        g_form.setMandatory('assigned_to', false);
      }
    } else {
      g_form.setMandatory('assigned_to', false);
    }
  }
})();

Looking forward to your suggestions.
5 REPLIES 5

SumanthDosapati
Mega Sage
Mega Sage

@PK14 

Did you try UI policy first?

Looks like requirement is possible with UI policy.

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

Ankur Bawiskar
Tera Patron
Tera Patron

@PK14 

try this

function onLoad() {

    // On form load, fetch parent organization ONCE
    g_form.getReference('change_request', function(parent) {
        if (parent) {
            // Use sys_id or (preferably) display value as per your system
            var parentOrg = (parent.u_organization || '').toString().trim();

            var state = g_form.getValue('state');

            // Adjust state values according to your environment!
            var CLOSED_COMPLETE = '3'; // Value for 'Closed Complete'
            var SKIPPED = '4'; // Value for 'Skipped'

            if (parentOrg === 'ABC') {
                g_form.setMandatory('assigned_to', true);
            } else if (parentOrg === 'XYZ') {
                if (state === CLOSED_COMPLETE || state === SKIPPED) {
                    g_form.setMandatory('assigned_to', true);
                } else {
                    g_form.setMandatory('assigned_to', false);
                }
            } else {
                g_form.setMandatory('assigned_to', false);
            }
        }
    });
}

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

Hi @Ankur Bawiskar,

This works for ABC organzation but not on the xyz. 

@PK14 

what debugging did you do?

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