Can we add default values for a multi row variable set on a catalog form

Joshuu
Kilo Sage

Hi All,

 

Is it possible to add default values for a multi row variable set on a catalog form On Load?

 

priyarao_0-1685102974165.png

 

 

Could you please suggest.

 

Thank you.

1 ACCEPTED SOLUTION

Your onLoad script was adding an JSON object, but you need to add JSON array (of objects):

function onLoad(){
  var objs = [{
    "add_application": "9b57780587f6e510273abb3acebb3505"
  }];

  g_form.setValue('add_applications_for_knowledge_workers', JSON.stringify(objs));
}

 

View solution in original post

9 REPLIES 9

Riya Verma
Kilo Sage
Kilo Sage

Hi @Joshuu ,

 

Hope you are doing great.

 

yes , you can add default value for multi row variable set using on load catalog client script.

 

Use below code for reference :

function onLoad() {
  // Get the multi-row variable set field
  var multiRowVariableSet = g_form.getControl('variable_set_field_name');

  // Check if the multi-row variable set field exists on the form
  if (multiRowVariableSet) {
    // Retrieve the variable rows within the multi-row variable set
    var variableRows = multiRowVariableSet.getMultiRowVariables();

    // Iterate through each variable row and set the default values
    for (var i = 0; i < variableRows.length; i++) {
      var variableRow = variableRows[i];

      // Set the default value for each variable within the variable row
      variableRow.setValue('variable_field_name', 'default_value');
    }
  }
}
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Ankur Bawiskar
Tera Patron
Tera Patron

@Joshuu 

you can use onLoad catalog client script and set the value using json structure

something like this

function onLoad(){

	var obj = {};
	obj["variable1Name"] = "your value"; // give your variable name which is inside MRVS and correct value
	obj["variable2Name"] = "your value"; // give your variable name which is inside MRVS and correct value
	obj["variable3Name"] = "your value"; // give your variable name which is inside MRVS and correct value

	g_form.setValue('mrvsVariableSetName', JSON.stringify(obj)); // give mrvs variable set name here

}

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

Joshuu
Kilo Sage

Hi @Riya Verma , @Ankur Bawiskar ,

 

I have tried in both the ways, but it didn't worked for me. Also, here the add application variable is a lookup select box.

 

Could you please help me, below is my code.

 

 

function onLoad(){

	var obj = {};
	obj["add_application"] = "Office 365 Application"; // give your variable name which is inside MRVS and correct value

	g_form.setValue('add_applications_for_knowledge_workers', JSON.stringify(obj)); // give mrvs variable set name here

}

 

 

 

function onLoad() {
  // Get the multi-row variable set field
  var multiRowVariableSet = g_form.getControl('add_applications_for_knowledge_workers');

  // Check if the multi-row variable set field exists on the form
  if (multiRowVariableSet) {
    // Retrieve the variable rows within the multi-row variable set
    var variableRows = multiRowVariableSet.getMultiRowVariables();

    // Iterate through each variable row and set the default values
    for (var i = 0; i < variableRows.length; i++) {
      var variableRow = variableRows[i];

      // Set the default value for each variable within the variable row
      variableRow.setValue('add_application', 'Office 365 Application');
    }
  }
}

 

 

Thank you.

@Joshuu 

add_application is reference type?

if yes then please give sys_id and not name

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