Take the date from one catalog variable, add 28 days, insert new date into another catalog variable.

LRhodes
Tera Guru

Hey all,

We have a catalogue item which requires the user to enter a date field. There is another variable called 'Completion Date' which should auto-populate with the users entered date + 28 days.

I presume this cannot be done on the catalogue stage as the date the user is entering isn't held anywhere at this point, so instead I have tried to put together a business rule to run on the requested item once it has been created. Essentially I just want it to take the date from the requested_date variable, add 28 days to the value, and then insert this new date into the completion_date variable. I've tried the below and it's inserting a date into the variable but it's not adding the 28 days.

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

	var reqdate = current.variables.requested_date;
	reqdate.addDaysLocalTime(30);
	
	current.variables.completion_date = reqdate;
	current.update();
	
	

})(current, previous);

find_real_file.png

Not tried something like this before so any assistance would be greatly appreciated - thank you!

1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

@LRhodes 

Try the below script:
Please try with before insert BR and remove current.update

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

    var upd = new GlideDateTime(current.variables.requested_date);
    upd.addDaysLocalTime(30);
    current.variables.completion_date = upd.getLocalDate();

})(current, previous);

(=tested)

Hope it helps

 

Thanks,

Murthy

Thanks,
Murthy

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

HI @LRhodes ,

Use the below script :

Type: OnChange()

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('u_date_concern_reported'); //first date field
var addtime = 28; 
var addtype = 'day'; 

var gr = new GlideAjax('global.ClientDateTimeUtils');
gr.addParam('sysparm_name', 'addDateAmount');
gr.addParam('sysparm_fdt', cdt);
gr.addParam('sysparm_addtime', addtime);
gr.addParam('sysparm_addtype', addtype);
gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_th_day', answer);  //second date field
alert(answer);
}

}

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Murthy Ch
Giga Sage

@LRhodes 

Try the below script:
Please try with before insert BR and remove current.update

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

    var upd = new GlideDateTime(current.variables.requested_date);
    upd.addDaysLocalTime(30);
    current.variables.completion_date = upd.getLocalDate();

})(current, previous);

(=tested)

Hope it helps

 

Thanks,

Murthy

Thanks,
Murthy

Thank you Murthy - this has done the trick.