Compare current date with pre defiend date on a Business Rule

TejasviR
Tera Contributor

Hello Everyone.

My requirement is, need to compare with the current date and time with predefined date and time (Filled by user), if the current date and time breached or crossed the predefined date and time need to set the value on a text field 'yes' if not then set value 'no'.

 

Thanks in advance

1 ACCEPTED SOLUTION

Shakeel Shaik
Giga Sage
Giga Sage

Hi @TejasviR ,

 

Create OnLoad Client Script on Custom Table.

sample script:

var currentDate = new Date(); //Current Date
var plannedDate = new Date(gr.getValue('u_end_date')); //Planned End Date

if (currentDate.getTime() > plannedDate.getTime()) {

g_form.setValue('NameOFtheField','Yes');

} else{
g_form.setValue('NameOFtheField','No');
}



please change as per your requirement.

 

 

Please check and Let us know

 

Thanks 🙂

Thanks,
Shakeel Shaik 🙂

View solution in original post

8 REPLIES 8

Raghu Ram Y
Kilo Sage

Hi @TejasviR 

You can achieve your requirement just with client script... no need to use business rule...

Use the following codes, it's working..tested.

 

Client Script : Create onChange Client script on the field where user is filling manually..

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var udt = newValue;
    var ga = new GlideAjax("GetCurrentDateAndTime");
    ga.addParam('sysparm_name', 'GetDate');
    ga.addParam('sysparm_get_date', udt);
    ga.getXML(HelloWorldParse);

    function HelloWorldParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == '5') {
            g_form.setValue('write your text field name here', "yes");
        } else if (answer == '6') {
            g_form.setValue('write your text field name here', "no");
        }
    }
}

 

Script Include

var GetCurrentDateAndTime = Class.create();
GetCurrentDateAndTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    GetDate: function() {
        var getNum = this.getParameter('sysparm_get_date');
        var nd = new GlideDateTime();
        if (getNum > nd) {
            return 5;
        } else {
            return 6;
        }
    },
    type: 'GetCurrentDateAndTime'
});

Hello Raghu Ram. why you use return 5 and return 6?

Hi @TejasviR 

Just returning some value to know whether it is before date or after date.. you can use any value/variable whatever you want..

Have you tested?

I hope it definitely helps you, If so please mark it as both HELPFUL and CORRECT

@TejasviR it's not a best practice to uses server side calls from client scripts so I suggested the above solution.

However, it's your wish to follow the solution which ever you want...