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

Kruthika BM
Tera Contributor

@Brad Bowman  Below is the OnChange Client script created on the MRVS. Could you please help me, where we can make the changes? also on the main catalog item 

 

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {

      return;

   }

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’);

}

 

   //Type appropriate comment here, and begin script below

   

}

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;
	}
}

@Brad Bowman  Thank you so much for the quick help!!!

The modified script which you have shared is working as expected now for the correct catalog item.

But, we are getting an error on the other catalog item MRVS(here we have work package start date and end date). It is allowing to provide the dates, but throwing the error.

 

Thanks and Regards,

Kruthika 

KruthikaBM_0-1691076411448.png

 

Do I need to create onload client script for the other catalog item as well?

 

Thanks,

Kruthika

@Brad Bowman  Now, I created the Onload Client script for the other catalog item as well. Now everything is working as expected.

Thanks again😊

 

Regards,

Kruthika