How to create OnChange Client script for Multiple Row Variable set depending on main Catalog Item?

Kruthika BM
Tera Contributor

Hello Everyone,

 

We have the requirement where there are variables like Project start date and end date and also, Work package start date and end date on the main form. And, on the MRVS we have only Work package start date and End date.

Now, the work package start date and end date on the MRVS, should depend on the project start date and end date of the main form.

Is there any Possibilities to create OnChange Client script for Multiple Row Variable set depending on the main Catalog Item?

Can anyone please help with this?

 

Thanks and Regards,

Kruthika

 

2 ACCEPTED SOLUTIONS

Brad Bowman
Kilo Patron
Kilo Patron

You can simply use g_service_catalog.parent.getValue... to get the main form variable values in an MRVS script.

 

https://developer.servicenow.com/dev.do#!/reference/api/rome/client/g_service_catalogClientAPI#g_ser... 

View solution in original post

So your onChange script would look more like this now to only run for one specific Catalog Item:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var catitem = '';
    if (this) { //Service Portal method
        catitem = this.cat_g_form.getUniqueValue();
    } else { //native UI method
        catitem = parent.g_form.getUniqueValue();
    }

    if (catitem == '964185f78726a9100b25986d3fbb35c1') { //replace with the sysid of your Catalog Item
        var ProjectStartDate = g_service_catalog.parent.getValue('value of project start date of catalog item');
        var ProjectEndDate = g_service_catalog.parent.getValue(‘value of project end date of catalog item');
        var WorkPackageStartDate = g_form.getValue(‘MRVS wrkpckgestart date’);
        if (ProjectStartDate != '' && ProjectEndDate != '') { 
            if(!((WorkPackageStartDate >= ProjectStartDate) && (WorkPackageStartDate <= ProjectEndDate))) {
                g_form.addErrorMessage("Please enter the dates in between Your Project Start Date and Project End Date i.e " +ProjectStartDate+" and "+ProjectEndDate);
                g_form.clearValue((‘MRVS wrkpckgestart date’);
            } 
        } else {
            g_form.addErrorMessage('Please Enter Project Start Date and Project End Date');
            g_form.clearValue((‘MRVS wrkpckgestart date’);
        }
    }
}

then create an onLoad Catalog Client Script that applies to the Catalog Item:

function onLoad() {
	if (this) { // only needed for Service Portal
		// make the g_form object for the parent item available from the MRVS window
		this.cat_g_form = g_form;
	}
}

View solution in original post

17 REPLIES 17

Amit Gujarathi
Giga Sage
Giga Sage

HI @Kruthika BM ,
I trust you are doing great.
Here's an example of how you can implement the onChange client script for the MRVS fields:

function onChangeWorkPackageDates() {
  // Get the project start and end dates from the main form
  var projectStartDate = g_form.getValue('project_start_date');
  var projectEndDate = g_form.getValue('project_end_date');
  
  // Get the MRVS fields
  var workPackages = g_form.getControl('work_packages')._container.childNodes;
  
  // Loop through each MRVS row
  for (var i = 0; i < workPackages.length; i++) {
    var workPackage = workPackages[i];
    
    // Update the work package start and end dates
    var workPackageStartDate = workPackage.querySelector('[name$="start_date"]');
    var workPackageEndDate = workPackage.querySelector('[name$="end_date"]');
    
    workPackageStartDate.value = projectStartDate;
    workPackageEndDate.value = projectEndDate;
  }
}

To implement this solution:

  1. Navigate to the form where you have the project start and end dates and the MRVS.
  2. Open the client script section for that form.
  3. Create a new client script and choose the appropriate conditions for when it should run (e.g., catalog item or variable set).
  4. In the script, use the provided code snippet and customize it based on your field names and form structure.
  5. Save the client script.

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hello Amit,

 

Thank you for the quick support by providing the clear steps.

I tried with this syntax and it worked - g_service_catalog.parent.getValue

 

Regards,

Kruthika

Kruthika BM
Tera Contributor

Hello @Brad Bowman ,

Hope you are doing good!

 

Since, the MRVS is included for 2 catalog items, the client script which we created is affecting the other catalog item as well. Do we have any other way to implement this requirement which will apply for only one of the catalog item?

Thanks in Advance!

 

Regards,

Kruthika

You can get the sys_id of the Catalog Item using this syntax, then add an if condition to only run the script if it matches the one Catalog Item that you want:

 

var catitem = parent.g_form.getUniqueValue();

 

 

This syntax works in the native UI.  If you are using Service Portal / Employee Center you need to add an onLoad Catalog Client Script that applies to the Catalog Item itself, not the MRVS:

 

function onLoad() {
	if (this) { // only needed for Service Portal
		// make the g_form object for the parent item available from the MRVS window
		this.cat_g_form = g_form;
	}
}

 

Then this part of the MRVS script would look more like this to work for both interfaces:

 

var catitem = '';
if (this) { //Service Portal method
    catitem = this.cat_g_form.getUniqueValue();
} else { //native UI method
    catitem = parent.g_form.getUniqueValue();
}

 

 

Kruthika BM
Tera Contributor

@Brad Bowman  Thank you for the quick help!

Could you please elaborate more in the single script?

We are using the Service Portal/ Employee center.

 

Thanks and Regards,

Kruthika