Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

calculate the days when option one weel or one month is selected

Community Alums
Not applicable

Hi All, 

 

Having a requirement that, on a catalog form we have a multiple option selection field. 

contains options 1 day, one week, one month, and six months. 

i created a date field which is hiding in the form view. here i need to set the value in  this date field based on the option selected in the multiple choice. if one week i selected then in the date filed calculate from the requested date to one week. vise versa same for the one month also. 

 

Need any suggestions. 

Thanks. 

2 REPLIES 2

Community Alums
Not applicable

Hi @Community Alums ,

I tried your problem in my PDI and it works for me 

I created 2 variable One contains select box like 1 Day, 1 Week, 1 Month and another contains the date 

SarthakKashyap_0-1716102485114.png

 

I created onSubmit clint script and added below code 

function onSubmit() {
    //Type appropriate comment here, and begin script below

	alert(g_form.getValue("select_range"));
    var ga = new GlideAjax('updateDate');
    ga.addParam('sysparm_name', 'updateDateFunction');
    ga.addParam('sysparm_dateRange', g_form.getValue("select_range"));
	ga.addParam('sysparm_dateValue', g_form.getValue("show_date"));
    ga.getXML(updateCampus);

    function updateCampus(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
		g_form.setValue('show_date', answer);
	}

}

 

Script Include 

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

    updateDateFunction: function() {
        var dateRange = this.getParameter('sysparm_dateRange');
        var dateShow = this.getParameter('sysparm_dateValue');
        gs.log('DateRange = ' + dateRange + " dateShow = " + dateShow);
        if (dateRange == 'one_day') {
            dateShow.addDaysUTC(1);
            gs.log('One day ' + dateShow.getValue());
			return dateShow;
        }
        if (dateRange == 'one_week') {
            dateShow.addDaysUTC(7);
            gs.log('RFS created ' + dateShow.getValue());
			return dateShow;
        }
        if (dateRange == 'one_month') {
            dateShow.addDaysUTC(30);
            gs.log('One moth ' + dateShow.getValue());
			return dateShow;
        }
    },

    type: 'updateDate'
});

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Community Alums
Not applicable

Hi @Community Alums 

Thanks for the swift response. 

I tried the above suggestion its not working for me it returning the null value. 

And one more here i have a variable type is multiple choice not as the select box(drop down)

even i tried with the select box also its not working. 

 

Client script: 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
 
alert(g_form.getValue("select_range")); // select_range is selct box variable type 
    var ga = new GlideAjax('updateDate');
    ga.addParam('sysparm_name', 'updateDateFunction');
    ga.addParam('sysparm_dateRange', g_form.getValue("select_range"));
ga.addParam('sysparm_dateValue', g_form.getValue("date_range")); //date_range is the date field 
    ga.getXML(updateCampus);
 
    function updateCampus(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue('date_range', answer);
}
 
}

And the script include is 

 

var updateDate = Class.create();
updateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
updateDateFunction: function() {
var dateRange = this.getParameter('sysparm_dateRange');
var dateShow = this.getParameter('sysparm_dateValue');
gs.log('DateRange = ' + dateRange + " dateShow = " + dateShow);
if (dateRange == 'one_day') {
dateShow.addDaysUTC(1);
gs.log('One day ' + dateShow.getValue());
return dateShow;
}
if (dateRange == 'one_week') {
dateShow.addDaysUTC(7);
gs.log('RFS created ' + dateShow.getValue());
return dateShow;
}
if (dateRange == 'one_month') {
dateShow.addDaysUTC(30);
gs.log('One moth ' + dateShow.getValue());
return dateShow;
}
},
type: 'updateDate'
});

 

Even in the logs also getting empty value. 

Any suggestions.