G_form.setvalue for a date field is not holding the value unless someone types over the date?

Colleen1
Tera Contributor

I recently implemented the populating of the variables from a URL with help from Michael but for some reason the new expiration date value is not storing what is passed in but the other two fields are.  It is defaulting to today's date?  From what I have read online, the date fields in ServiceNow can be quite picky.  Below is my dilemma:

Here is the URL that I am testing with and is currently pulling the values onto the form (screenshot below).

https://<my instance>.service-now.com/sp?id=sc_cat_item&sys_id=d0431cdddbe22e004db470d9af9619ef&sysp...

find_real_file.png

I do not touch any of the fields they are what I want so I click submit.  When I do, here is the RITM record:

find_real_file.png

Here is my code from the Catalog Client Script that is running.  It populates all 3 variables but the date is not sticking it is defaulting to the current date?  BUT if I update the pre-populated date with a new date value, it stores it then??

function onLoad() {
var User = getParameterValue('sysparm_requestedfor');

var usr = new GlideAjax('FindRequestedFor');
usr.addParam('sysparm_name', 'getRequestedFor');
usr.addParam('sysparm_user', User);
usr.getXML(getUserParse);

function getUserParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(User != undefined || User != '') g_form.setValue('requested_for', answer);
}

var Date = getParameterValue('sysparm_expdte');
var comments = getParameterValue('sysparm_comments');

if(Date != undefined || Date != '') g_form.setValue('new_expiration_date', Date);
if(comments != undefined || comments != '') g_form.setValue('sc_all_items_notes', comments);
}

function getParameterValue(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(top.location);
if (results == null) {
return "";
} else {
return unescape(results[1]);
}
}

 

Any assistance would be greatly appreciated.  This last thing is preventing me from migrating my code to Production.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

That's because the date format you are receiving in the url is

20190801

but the date field accepts format as 2019-09-01 i.e. YYYY-MM-DD

better to send the code in the above format and then set value

var Date = getParameterValue('sysparm_expdte');

 if you can't change the date format while sending to the URL then have this script to manipulate the date

var Date = getParameterValue('sysparm_expdte');

var year = Date.substring(0,4);
var month = Date.substring(4,6);
var day = Date.substring(6,str.length);

var updatedDate = year + '-' + month + '-' + day;
g_form.setValue('new_expiration_date', updatedDate);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

That's because the date format you are receiving in the url is

20190801

but the date field accepts format as 2019-09-01 i.e. YYYY-MM-DD

better to send the code in the above format and then set value

var Date = getParameterValue('sysparm_expdte');

 if you can't change the date format while sending to the URL then have this script to manipulate the date

var Date = getParameterValue('sysparm_expdte');

var year = Date.substring(0,4);
var month = Date.substring(4,6);
var day = Date.substring(6,str.length);

var updatedDate = year + '-' + month + '-' + day;
g_form.setValue('new_expiration_date', updatedDate);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Thank you.  I tried this and these behavior occurred.  Thank you for the quick response.

Actually, I had an additional line of code further down that was over writing your suggestion.  When I commented that out, it worked!!!!!!  Thank you!!! it was the format that it was being 'particular' with when saving the record.  Thank you again!!!

johnfeist
Mega Sage
Mega Sage

Hi Colleen,

It sounds like you need to check what's happening on the back end.  You may have data policies, business rules or scripts that are rejecting your change.

If not, do a simple test.  Write a simple client script against a test table that will simply do the same as you back end function in terms of taking data and returning as date in the format yyyy-mm-dd and see if it will save.

Hope this helps.

:{)

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster