llDate format conversion

Ashwin Perumal1
Tera Contributor

Hello @Ankur Bawiskar 
I have used the script include from your previous blog  and client script for changing my date format on my catalog variable form for a date type field. The problem is that I am able to change date format as required but my on change client script keeps on changing the field value continuously on the form. Can you pls help on this. Thanks in advance

 

Script Include:

var testDateFormat = Class.create();
testDateFormat.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
convertDateFormat: function(){
 
var incomingDate = this.getParameter('sysparm_format');
var format = 'yyyy-MM-dd' ;
var gdt = new GlideDate();
gdt.setDisplayValue(incomingDate,format);
var gd = new GlideDate(); 
gd.setValue(gdt.getDate());
return gd.getByFormat("MM-dd-yyyy");
 
},

Client script:
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 
   //Type appropriate comment here, and begin script below
   var userValue = g_form.getValue('start_date');
        var ga = new GlideAjax('testDateFormat');
        ga.addParam('sysparm_name', 'convertDateFormat');
        ga.addParam('sysparm_format', userValue);
        ga.getXML(result);
 
 
        function result(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer').toString();
            g_form.setValue('start_date', answer);
        }
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ashwin Perumal1 

you need to stop the recursion by checking the date format

something like this

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	if(oldValue != newValue){
		var isValid = getDateFromFormat(newValue,'MM-dd-yyyy') != 0;
		if(!isValid){

			// trigger the ajax when the format is not valid
			// when the format is valid it won't trigger ajax and will avoid recursion

			//Type appropriate comment here, and begin script below
			var userValue = g_form.getValue('start_date');
			var ga = new GlideAjax('testDateFormat');
			ga.addParam('sysparm_name', 'convertDateFormat');
			ga.addParam('sysparm_format', userValue);
			ga.getXML(result);
			function result(response) {
				var answer = response.responseXML.documentElement.getAttribute('answer').toString();
				g_form.setValue('start_date', answer);
			}
		}
	}
}

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

View solution in original post

13 REPLIES 13

Samaksh Wani
Giga Sage
Giga Sage

Hello @Ashwin Perumal1 

 

Modify your client script :-

 

 

Client script:
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 
   //Type appropriate comment here, and begin script below
var dateField = g_form.getControl('start_date');

if(dateField.changed){
   var userValue = g_form.getValue('start_date');
        var ga = new GlideAjax('testDateFormat');
        ga.addParam('sysparm_name', 'convertDateFormat');
        ga.addParam('sysparm_format', userValue);
        ga.getXML(result);
 
 
        function result(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer').toString();
            g_form.setValue('start_date', answer);
        }

}
}

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

Unfortunately its not now even changing the date format also.

Ankur Bawiskar
Tera Patron
Tera Patron

@Ashwin Perumal1 

you need to stop the recursion by checking the date format

something like this

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	if(oldValue != newValue){
		var isValid = getDateFromFormat(newValue,'MM-dd-yyyy') != 0;
		if(!isValid){

			// trigger the ajax when the format is not valid
			// when the format is valid it won't trigger ajax and will avoid recursion

			//Type appropriate comment here, and begin script below
			var userValue = g_form.getValue('start_date');
			var ga = new GlideAjax('testDateFormat');
			ga.addParam('sysparm_name', 'convertDateFormat');
			ga.addParam('sysparm_format', userValue);
			ga.getXML(result);
			function result(response) {
				var answer = response.responseXML.documentElement.getAttribute('answer').toString();
				g_form.setValue('start_date', answer);
			}
		}
	}
}

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

@Ankur Bawiskar It 's working as expected. Thank you so much.