Compare two dates of a catalog item (record producer)

Milan13
Giga Expert

Hello,

I need to write a simple code to compare starting and ending dates in order to ensure the ending date is greater than the starting one.

This applies to a record producer which should be a catalog item at the same time - I have done similar task recently but I guess the catalog client script applied to a catalog item but not to a record producer (getting a bit confused here to be honest...).

Anyway previously I used this code and it worked but does not work in case of a record procducer (attaching snap shot of a record producer Date variable which I need to retrieve its value from and compare with another one).

OnChange client script I used previously but does not work at this time:

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

var start_date = getDateFromFormat(g_form.getValue('date_from'), g_user_date_time_format);
var end_date = getDateFromFormat(g_form.getValue('date_to'), g_user_date_time_format);

if (start_date > end_date) {
g_form.addErrorMessage('DATE TO must be greater than DATE FROM');
g_form.setValue('date_to','');
return false;
}

}

When I try to get value this way I get the result of zero, any hel on this?

Many thanks,

Milan

 

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

Hello Milan, 

You can need to create a simple catalog UI Policy and Add a condition is

YourDateVariable BEFORE TODAY

Refer this docs for No Code date validations.

No Code date validations thru (Catalog) UI Policies

I will try to debug your code and share with you.

Please mark as Correct Answer and Helpful, if applicable.
Thank You!
Abhishek Gardade

Thank you,
Abhishek Gardade

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Did you try adding alert whether if condition is getting satisfied?

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

AbhishekGardade
Giga Sage

Hello Milan, 

You can need to create a simple catalog UI Policy and Add a condition is

YourDateVariable BEFORE TODAY

Refer this docs for No Code date validations.

No Code date validations thru (Catalog) UI Policies

I will try to debug your code and share with you.

Please mark as Correct Answer and Helpful, if applicable.
Thank You!
Abhishek Gardade

Thank you,
Abhishek Gardade

Shrutika Surwad
Kilo Guru

hey milan,

 

Use the following before business rule with condition (Change the operators if need be between both the dates) :

 

find_real_file.png

 

and in the script part :

(function executeRule(current, previous /*null when async*/){

 

current.setAbortAction(true);

 

})(current, previous);