You need to sign in to do that
Don't have an account?
![Khalid mn Khalid mn](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
How to write Trigger to get Gap between Onboarding_Date__c and Today Date?
Hi,
Please Help me to do this i have a requirement-
Object and Field- [Account]-- Onboarding_Date__c
[Service Contract] -- Renew_Count__c
I want to count the gap between Onboarding_Date__c and today's date.
(consider (Year, Month, and Day)
(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;
What i made wrong in this trigger Please help me out..
Thank You In Advanvce
Please Help me to do this i have a requirement-
Object and Field- [Account]-- Onboarding_Date__c
[Service Contract] -- Renew_Count__c
I want to count the gap between Onboarding_Date__c and today's date.
(consider (Year, Month, and Day)
(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;
public static void renewCount(List<Account> accList){ Set<Id> setAccIds = new Set<Id>(); List<ServiceContract> newServiceContactList = new List<ServiceContract>(); for(Account acc : accList){ if(acc.Onboarding_Date__C != null){ setAccIds.add(acc.Id); } } List<ServiceContract> serviceContractList = [SELECT Renew_Count1__c FROM ServiceContract WHERE AccountId IN : setAccIds]; for(ServiceContract serviceContr : serviceContractList){ if(serviceContr.Renew_Count1__c == null){ serviceContr.Renew_Count1__c = Date.Today().YEAR() - acc.Onboarding_Date__C.YEAR(); newServiceContactList.add(serviceContr); } } update newServiceContactList; }
What i made wrong in this trigger Please help me out..
Thank You In Advanvce
try with below code.
If this helps, please mark it as best answer.
Thanks!!
All Answers
try with below code.
If this helps, please mark it as best answer.
Thanks!!
Here is one problem can you please help me out.
Problem is if the Onboarding Date is 11 months it also measures the gap as 1.
What I want.
eg1. If the Onboarding Date is (2021-07-03) and the Today Date is (22-07-01) Means Year not been completed yet. so it show in Renew_Count__c Field 0. (It's 11 month 28 days)
eg1. If the Onboarding Date is (2021-06-03) and the Today Date is (22-07-01) Means Year completed so it shows in Renew_Count__c Field 1.(it's somthing 1 Year 11 month so count as 1)