Need to subtract or minus 5 business days from a date/time field on the Catalog form.

Rmohan
Mega Expert

Hi,

I have a requirement for setting a field value as 5 business days minus the set date on the another field on a catalog form.

For example-

If the Delivery_Date is set as 11-15-2019, the Actual_Start_date should be 11-08-2019, taking into account the 8-5 weekday schedule.

Looking for on how to configure the Actual_Start_date.

The delivery date can be modified by the user submitting the request, so I know it should be a catalog client script, but I am quite new to coding and after thoroughly searching the community, could not find the answer I needed.

So requesting the ServiceNow Community for assistance.

Thanks.

1 ACCEPTED SOLUTION

Rmohan
Mega Expert

Hi All,

I finally made is possible by following a minimal scripting method using the Timer.

As suggested by TJW in set a timer in workflow based on a variable, I was able to set the schedule and make the timer wait as per my needs.

Thank you all for your support 🙂 

View solution in original post

39 REPLIES 39

Please do share where I can put the alert to check the scripts.

add alert before this line 

g_form.setValue('<start_date>', answer);

also try changing your delivery date manually , after default value is set try if that works.

Hi Vignesh,

Added the alert and manually changed the Delivery_date after the form load the default value, but still it does not work.

Also the alert does not show up.

Abhinay Erra
Giga Sage

Script include method name does not match to the one in GlideAjax. Use the following code for client script and script include

Script Include:

Name: AjaxDurCalc 

Client Callable checkbox: true

var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    durCalc: function() {
        var deliver = this.gertParameter('sysparm_deliv');
        var ps = new GlideDateTime(deliver);
        ps.addDays(-5);
        var datebefore = ps.getDate();
        return datebefore;
    },
    type: 'AjaxDurCalc'
});

 

Client script:

Always use Asynchronous glideajax 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var deliverydate = g_form.getValue('delivery_date'); //variable field
    var ajax = new GlideAjax('AjaxDurCalc');
    ajax.addParam('sysparm_name', 'durCalc');
    ajax.addParam('sysparm_deliv', deliverydate);
    ajax.getXML(setDate);

    function setDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('actual_start_date', answer);
    }
}

 

 

Hi Abhinay,

Thank you for your response. I tried your code and still the actual_start_date is coming as blank.

As suggested by Vignesh, I tried to put the alert before-

g_form.setValue('<start_date>', answer);

it did not showed up and also changed the delivery date manually , after default value is set, but it still comes as blank.

 

Thanks