+ Start a Discussion
Khalid mnKhalid mn 

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;

 
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
Best Answer chosen by Khalid mn
AnkaiahAnkaiah (Salesforce Developers) 
Hi Khalid,

try with below code.
public static void renewCount(List<Account> accList){
	// Set<Id> setAccIds = new Set<Id>();
   map<id,integer> mapaccwithyear = new map<id,integer>();
	List<ServiceContract> newServiceContactList = new List<ServiceContract>();
	for(Account acc : accList){
	if(acc.Onboarding_Date__C != null){
        integer year = acc.Onboarding_Date__C.year();
		mapaccwithyear.put(acc.Id, year);
	}
	}
	List<ServiceContract> serviceContractList = [SELECT Renew_Count1__c,AccountId FROM ServiceContract WHERE AccountId IN : mapaccwithyear.keySet()];
	for(ServiceContract serviceContr : serviceContractList){
        
		if(serviceContr.Renew_Count1__c == null && mapaccwithyear.containsKey(serviceContr.accountId) ){
           integer currentyear = Date.Today().YEAR(); 
		serviceContr.Renew_Count1__c = currentyear - mapaccwithyear.get(serviceContr.accountId);
		newServiceContactList.add(serviceContr);
		
		}
	}
	update newServiceContactList;

}

If this helps, please mark it as best answer.

Thanks!!​​​​​​​

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Khalid,

try with below code.
public static void renewCount(List<Account> accList){
	// Set<Id> setAccIds = new Set<Id>();
   map<id,integer> mapaccwithyear = new map<id,integer>();
	List<ServiceContract> newServiceContactList = new List<ServiceContract>();
	for(Account acc : accList){
	if(acc.Onboarding_Date__C != null){
        integer year = acc.Onboarding_Date__C.year();
		mapaccwithyear.put(acc.Id, year);
	}
	}
	List<ServiceContract> serviceContractList = [SELECT Renew_Count1__c,AccountId FROM ServiceContract WHERE AccountId IN : mapaccwithyear.keySet()];
	for(ServiceContract serviceContr : serviceContractList){
        
		if(serviceContr.Renew_Count1__c == null && mapaccwithyear.containsKey(serviceContr.accountId) ){
           integer currentyear = Date.Today().YEAR(); 
		serviceContr.Renew_Count1__c = currentyear - mapaccwithyear.get(serviceContr.accountId);
		newServiceContactList.add(serviceContr);
		
		}
	}
	update newServiceContactList;

}

If this helps, please mark it as best answer.

Thanks!!​​​​​​​
This was selected as the best answer
Khalid mnKhalid mn
Hi Ankaiah,
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)