write client script to achieve below requirement

DhanashriB
Tera Contributor

Hi All,

 I need to write client script for below requirement.

1. "Pending disposal" [pending_disposal] choice already exists in Substate field
2. Call GlideAjax function (new) in client script "Hide Pending Disposal Option for Retired" to check if logged in user is a member of any group of name starting with "FS" (query sys_user_grmemebr table)
 
so for that I have written script and script include as mentioned below.
script include
var CheckFSGroupMembership = Class.create();
CheckFSGroupMembership.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
  isUserInFSGroup: function() {
    var userID = gs.getUserID();
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', userID);
    gr.query();
 
    while (gr.next()) {
      var group = gr.group.getRefRecord();
      if (group && group.name.toString().startsWith('FS')) {
        return 'true';
      }
    }
    return 'false';
  }
 
});
 
Client script
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || !newValue) return;
 
  console.log('State changed to: ' + newValue);
  alert('State changed to: ' + newValue); // TEMP
 
  if (parseInt(newValue) === 7) { // 7 = Retired in your instance
    var ga = new GlideAjax('CheckFSGroupMembership');
    ga.addParam('sysparm_name', 'isUserInFSGroup');
 
    ga.getXMLAnswer(function(answer) {
      console.log('FS Group check result: ' + answer);
      alert('Is user in FS group? ' + answer); // TEMP
 
      if (answer === 'true') {
        g_form.addOption('substate', 'pending_disposal', 'Pending Disposal');
        console.log('Added Pending Disposal option');
        alert('Added Pending Disposal option'); // TEMP
      } else {
        g_form.removeOption('substate', 'pending_disposal');
        console.log('Removed Pending Disposal option (user not in FS group)');
      }
    });
  } else {
    g_form.removeOption('substate', 'pending_disposal');
    console.log('Removed Pending Disposal (state is not Retired)');
  }
}
 
script is working as expected but after removing console log and alerts this is not working. Kindly provide me solution on this
 
Thanks,
Dhanashri
 
4 REPLIES 4

JenniferRah
Mega Sage

Does your updated code look just like this? 

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

    if (parseInt(newValue) === 7) { // 7 = Retired in your instance
        var ga = new GlideAjax('CheckFSGroupMembership');
        ga.addParam('sysparm_name', 'isUserInFSGroup');

        ga.getXMLAnswer(function(answer) {
            if (answer === 'true') {
                g_form.addOption('substate', 'pending_disposal', 'Pending Disposal');
            } else {
                g_form.removeOption('substate', 'pending_disposal');
            }
        });
    } else {
        g_form.removeOption('substate', 'pending_disposal');
    }
}

If so, can you tell how far it's getting? Or is it failing for one particular scenario?

It is not showing substate pending disposal choice on selection of state retired only the script I am using with console and alert it is giving expected output, but after removing that the pending disposal choice is not showing in substate field

The Substate field's internal name is "substatus". You need to change that in your code. Sorry I didn't catch that at first.

sunil maddheshi
Tera Guru

@DhanashriB 

Please try with Below script:

Script include:

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

  isUserInFSGroup: function() {
    var userID = gs.getUserID();
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', userID);
    gr.query();

    while (gr.next()) {
      var group = gr.group.getRefRecord();
      if (group && group.name.toString().startsWith('FS')) {
        return 'true';
      }
    }
    return 'false';
  }

});

onLoad Client Script (fetch FS group status)

// Type: onLoad
(function() {
  var ga = new GlideAjax('CheckFSGroupMembership');
  ga.addParam('sysparm_name', 'isUserInFSGroup');
  ga.getXMLAnswer(function(answer) {
    g_scratchpad.isInFSGroup = answer === 'true';

    // Initial check in case state is already 'Retired'
    var state = g_form.getValue('state');
    if (parseInt(state) === 7) {
      updateSubstateOption(true, g_scratchpad.isInFSGroup);
    }
  });

  function updateSubstateOption(isRetired, isInFSGroup) {
    if (isRetired && isInFSGroup) {
      g_form.addOption('substate', 'pending_disposal', 'Pending Disposal');
    } else {
      g_form.removeOption('substate', 'pending_disposal');
    }
  }
})();

onChange Client Script on State field

// Type: onChange
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || !newValue) return;

  var isRetired = parseInt(newValue) === 7;
  var isInFSGroup = g_scratchpad.isInFSGroup;

  // If FS Group check is still loading, wait briefly and retry
  if (typeof isInFSGroup === 'undefined') {
    setTimeout(function() {
      isInFSGroup = g_scratchpad.isInFSGroup;
      updateSubstateOption(isRetired, isInFSGroup);
    }, 300); // Delay slightly to allow onLoad GlideAjax to complete
  } else {
    updateSubstateOption(isRetired, isInFSGroup);
  }

  function updateSubstateOption(isRetired, isInFSGroup) {
    if (isRetired && isInFSGroup) {
      g_form.addOption('substate', 'pending_disposal', 'Pending Disposal');
    } else {
      g_form.removeOption('substate', 'pending_disposal');
    }
  }
}

Please mark correct/helpful if this helps you.