web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Add Days to Date from Option field selection using Javascript

(0) ShareShare
ReportReport
Posted on by 85

Hi community

This forum is amazing at assisting young developers to improve. Thank you!

I have a need to add days to a date in combination with a 3rd option field selection.

Here are the conditions:
Option Field = Duration (2 Weeks / 1 Month / 2 Months)
StartDate Field = Date Only (manual entry)
EndDate Field = Date Only (javascript needing to calculate result)

I have tried multiple options to get this right without success. Below is the final 2 scripts I have been working on.


function SetEndDate(executionContext) {
    //Get the context of the form
    var formContext = executionContext.getFormContext();
    //The logical name of the field of interest
    var Duration = "new_duration";
	var StartDate = "new_startdate";
    var EndDate = "new_enddate";
    //Declare the other variables as needed
    var StartDateValue = "new_startdate";
    var EndDateValue = new Date();
    var TwoWeeks = 14;
	var OneMonth = 30;
	var TwoMonths = 60;
    var DateTimeConverter = 24 * 60 * 60 * 1000;

	if(Duration.getValue() == 100000000) {
		//Set the EndDate to 14 days after the StartDate
		EndDateValue.setTime(StartDateValue.getTime()   (TwoWeeks * DateTimeConverter));
		// Set the value of the field
		EndDate.setValue(EndDateValue);
	}
	else if(Duration.getValue() == 100000001) {
		//Set the EndDate to 30 days after the StartDate
		EndDateValue.setTime(StartDateValue.getTime()   (OneMonth * DateTimeConverter));
		// Set the value of the field
		EndDate.setValue(EndDateValue);
	}
	else if(Duration.getValue() == 100000002) {
		//Set the EndDate to 60 days after the StartDate
		EndDateValue.setTime(StartDateValue.getTime()   (TwoMonths * DateTimeConverter));
		// Set the value of the field
		EndDate.setValue(EndDateValue);
	}
    else {
    }
}




function SetEndDate(executionContext) {
    //Get the context of the form
    var formContext = executionContext.getFormContext();
    //The logical name of the field of interest
    var Duration = "new_duration";
	var StartDate = "new_startdate";
    var EndDate = "new_enddate";
    //Declare the other variables as needed
    var StartDateValue = "new_startdate";
    var EndDateValue = new Date();
    var TwoWeeks = 14;
	var OneMonth = 30;
	var TwoMonths = 60;

	if(Duration.getValue() == 100000000) {
		//Set the EndDate to 14 days after the StartDate
		EndDateValue.setTime(StartDateValue.getTime()   TwoWeeks);
		// Set the value of the field
		EndDate.setValue(EndDateValue);
	}
	else if(Duration.getValue() == 100000001) {
		//Set the EndDate to 30 days after the StartDate
		EndDateValue.setTime(StartDateValue.getTime()   OneMonth);
		// Set the value of the field
		EndDate.setValue(EndDateValue);
	}
	else if(Duration.getValue() == 100000002) {
		//Set the EndDate to 60 days after the StartDate
		EndDateValue.setTime(StartDateValue.getTime()   TwoMonths);
		// Set the value of the field
		EndDate.setValue(EndDateValue);
	}
    else {
    }
}



Any assistance is greatly appreciated.
 

I have the same question (0)
  • Suggested answer
    Sergi Valero Profile Picture
    145 on at

    Hello Dynamics_DR!

    I think the main problem here is that you aren't getting the actual values of the fields. Here is how to get the value of a field using the formContext:

    // Get field value
    let Duration = formContext.getAttribute('new_duration').getValue();
    
    // Set field value
    formContext.getAttribute('new_duration').setValue(newValue);
    

    Try doing it that way and if it still doesn't work, we can take a look at the date logic you are applying!

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    Because Date.getTime() returns a millisecond value, you should use this piece of code:

    function SetEndDate(executionContext) {
        var formContext = executionContext.getFormContext();
        var Duration = formContext.getAttribute('frank123_duration').getValue();
    	alert(Duration);
    	var StartDate = new Date();
        var EndDate = new Date();
        var StartDateValue = new Date();
        var EndDateValue = new Date();
        var TwoWeeks = 14;
    	var OneMonth = 30;
    	var TwoMonths = 60;
        var DateTimeConverter = 24 * 60 * 60 * 1000;
    
    	if(Duration == 100000000) {
    		//Set the EndDate to 14 days after the StartDate
    		EndDateValue.setTime(StartDateValue.getTime()   (TwoWeeks * DateTimeConverter));
    		// Set the value of the field
    		EndDate.setTime(EndDateValue.getTime());
    	}
    	else if(Duration == 100000001) {
    		//Set the EndDate to 30 days after the StartDate
    		EndDateValue.setTime(StartDateValue.getTime()   (OneMonth * DateTimeConverter));
    		// Set the value of the field
    		EndDate.setTime(EndDateValue.getTime());
    	}
    	else if(Duration == 100000002) {
    		//Set the EndDate to 60 days after the StartDate
    		EndDateValue.setTime(StartDateValue.getTime()   (TwoMonths * DateTimeConverter));
    		// Set the value of the field
    		EndDate.setTime(EndDateValue.getTime());
    	}
    	alert(EndDate);
    }

    Regarding the unsuccessful execution, you can check three points:

    1. Whether the event was successfully triggered

    2.To get the value of Duration, the following expression should be used: var Duration = formContext.getAttribute('duration').getValue();

    3. EndDate.setValue is not a function

  • Dynamics_DR Profile Picture
    85 on at

    Thank you Sergi

  • Dynamics_DR Profile Picture
    85 on at

    Thank you Frank

  • Sergi Valero Profile Picture
    145 on at

    No problem Dynamics DR,

    make sure to mark the answer as verified so others can easily find the solution!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans