function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Neelam BasakNeelam Basak 

I am trying to do field dependency for editable fields not formula fields.

Hi Experts,

Please Help!
I have 3 fields
a) Project Start Date
b) Project End Date
c) Term(in months)
Whenever we create a new agreement/project, the user will fill only start date and terms. The end date will be automatically calculated.
However, after the user edits either terms or end date the respective alternate fields are not updating automatically.
For example, if the user edits the terms the end date should automatically update or vice versa.
Both the fields are editable for the users.
Please suggest how to make field dependency on each other. If the user edit terms the end date should be calculated automatically. 
Or if the user edits end date the term should be automatically updated.

Thanks,
Neelam
Best Answer chosen by Neelam Basak
Khan AnasKhan Anas (Salesforce Developers) 
Hi Neelam,

I trust you are doing very well!

Instead of a trigger, you can use formula field and a text field (as you want to make it editable).
  • First create formula field
  • Then create a text field
  • Update text field using workflow/process builder
  • Update it using the formula field reference
  • Hide formula field from page layout

Formula for creating end date using number of months and start date:
https://help.salesforce.com/articleView?id=000004519&language=en_US&type=1
 
DATE (
YEAR ( StartDate__c ) + FLOOR ( (MONTH ( StartDate__c ) -1 + Number_of_Months__c)/12),
CASE ( MOD ( MONTH ( StartDate__c )+Number_of_Months__c, 12 ),0,12,MOD ( MONTH ( StartDate__c )+Number_of_Months__c, 12 )),
MIN ( DAY ( StartDate__c ),
CASE ( MOD ( MONTH ( StartDate__c )+Number_of_Months__c,12 ) ,9,30,4,30,6,30,11,30,2,28,31 ) )
)

Formula for calculating number of months from start and end date:
IF( 
CEILING((testdate2__c - testdate1__c) / 365 * 12) < 1, 1, CEILING((testdate2__c - testdate1__c) / 365 * 12)
)

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Neelam,

I trust you are doing very well!

Instead of a trigger, you can use formula field and a text field (as you want to make it editable).
  • First create formula field
  • Then create a text field
  • Update text field using workflow/process builder
  • Update it using the formula field reference
  • Hide formula field from page layout

Formula for creating end date using number of months and start date:
https://help.salesforce.com/articleView?id=000004519&language=en_US&type=1
 
DATE (
YEAR ( StartDate__c ) + FLOOR ( (MONTH ( StartDate__c ) -1 + Number_of_Months__c)/12),
CASE ( MOD ( MONTH ( StartDate__c )+Number_of_Months__c, 12 ),0,12,MOD ( MONTH ( StartDate__c )+Number_of_Months__c, 12 )),
MIN ( DAY ( StartDate__c ),
CASE ( MOD ( MONTH ( StartDate__c )+Number_of_Months__c,12 ) ,9,30,4,30,6,30,11,30,2,28,31 ) )
)

Formula for calculating number of months from start and end date:
IF( 
CEILING((testdate2__c - testdate1__c) / 365 * 12) < 1, 1, CEILING((testdate2__c - testdate1__c) / 365 * 12)
)

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Neelam BasakNeelam Basak
It worked.

Thanks,
Neelam