Before submit the catalog item fetch the MRVS variable's values

Venky Kshatriy2
Tera Contributor

Hi Team,

 

I have question like before submit the catalog item need to fetch the Multirow variable set Variable values. Why because I need to equal to this variable's values to back end table filed value's and I need to set one variable value in this catalog item.

 

If any one know this could pls help on this 

 

Advance thanks team.

2 REPLIES 2

Yashsvi
Kilo Sage

Hi @Venky Kshatriy2,

 

  • Create a Catalog Client Script:

    Create a Catalog Client Script on the catalog item that will run onSubmit. This script will fetch the Multi-row variable set values, perform the comparison, and set the desired variable value.

  • Fetch Multi-row Variable Set Values:

    Use the g_form API to get the values of the Multi-row variable set. You can access the Multi-row variable set using the g_form.getValue() method.

  • Perform the Comparison:

    Compare the fetched values with the values in the backend table. You might need to use GlideAjax or a script include to perform server-side operations.

  • Set the Variable Value:

    Based on the comparison, set the desired variable value using the g_form.setValue() method.

function onSubmit() {
  // Fetch the Multi-row variable set values
  var multiRowVariableSet = g_form.getValue('multi_row_variable_set');

  // Convert the JSON string to an object
  var multiRowValues = JSON.parse(multiRowVariableSet);

  // Perform the comparison with backend table values
  var isValid = true;
  for (var i = 0; i < multiRowValues.length; i++) {
    var row = multiRowValues[i];
    // Replace 'field_name' with the actual field name in the backend table
    if (!compareWithBackendTable(row.field_name)) {
      isValid = false;
      break;
    }
  }

  // Set the variable value based on the comparison
  if (isValid) {
    g_form.setValue('variable_name', 'valid');
  } else {
    g_form.setValue('variable_name', 'invalid');
  }

  // Prevent form submission if necessary
  return isValid;
}

function compareWithBackendTable(fieldValue) {
  // Use GlideAjax to call a Script Include for the server-side comparison
  var ga = new GlideAjax('ScriptIncludeName');
  ga.addParam('sysparm_name', 'compareWithBackendTable');
  ga.addParam('sysparm_field_value', fieldValue);
  ga.getXMLAnswer(function(response) {
    var result = response.responseXML.documentElement.getAttribute("answer");
    return result === 'true';
  });
}
  • Create a Script Include:
var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = {
  initialize: function() {
  },
  
  compareWithBackendTable: function(fieldValue) {
    // Perform the comparison with the backend table
    var gr = new GlideRecord('backend_table');
    gr.addQuery('field_name', fieldValue);
    gr.query();
    if (gr.next()) {
      return true;
    } else {
      return false;
    }
  },
  
  type: 'ScriptIncludeName'
};

Thank you, please make helpful if you accept the solution. 

 

Thanks for your reply @Yashsvi 

 

I need to changes of variables i need to update the 3rd variable I need to use the on change client script