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
Khalid mnKhalid mn 

How to count year gap between two dates using Apex code

Hi,
I want to calculate the gap between two dates and store in Renew Count Field -
Related Objects and fields
Enitlement =>  Field:(Renewed__C) Checkbox
ServiceContract => field:(Renew_Count__c)
Account => field: (Onboarding_Date__c) Date

For Now, I want to calculate based on the Date (consider (Year, Month, and Day)
the Requirement is now that-

When the Entitlement field is checked in case of active contract.
Calculated the gap between calculate the gap between onborading_date__c and Today date-
(e.g. if the onboarding_date is 2021-06-22, today - onboarding_date is 1 year so Renew_Count__c Become 1; and 
(e.g. if the onboarding_date is 2020-06-22, today - onboarding_date is 1 year so Renew_Count__c Become 2;


Thank You in Advance



 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Khalid,

You need to write after update trigger on Enitlement object.

Refer the trailhead to proceed further on your requirement.

https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro

Thanks!!