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
sfdc dev 2264sfdc dev 2264 

Trigger for DATE overlapping help needed

Hi,

I need help on the following requirement below as follows,

1) I have a account object where contract is another object related to account

2) Contract object is having contract start date and end date which populates to account (start date__c and end date __c)

My scenario is 

3)If there are 2 active contract associcated to account and the dates contain

1st Active contracts dates are

start date :1.feb 2017
end date : 31st march 2017

2nd active contract dates are

start date : 1st march 2017
end date : 17th sep 2017

then in this scenatio the first active contract end date should be automatically backdated to the previous month below as follows to avoid overlapping between 2 contracts

1st active contract end date should automatically be changed to 

end date :28th feb 2017
start date should remain the same as 1 feb 2017.

basically both contracts end dates and start dates shouldnt fall in same range to avoid overlapping

Help me how to achieve this 

please help me with the trigger code

Thanks in advance
Amol Salve 13Amol Salve 13
Hello,

Use below code for your requirement,
 
trigger overlapDate on contract (insert insert, after update){

	overlapDateHandler handlerObj = new overlapDateHandler();
	handlerObj.overlapDateMethod(trigger.new);
}

public class overlapDateHandler{

	public void overlapDateMethod(list<contract> contractList){
	
		set<Id> accountIdSet = new set<Id>();
		list<Account> accList = new List<Account>();
		map<integer, integer> dateMap = new map<integer, integer>{
		1 => 31, 
		3 => 31, 
		4 =>30, 
		5 =>31, 
		6 => 30, 
		7 => 31, 
		8 => 31, 
		9 =>30, 
		10 =>31, 
		11 =>30, 
		12, 31, };
		map<id, list<Contract>> idVsContractmap = new map<id, list<contract>>();
		list<Contract> updateContractList = new list<Contract>();
		for(Contract conObj : contractList){
			
			if(conObj.AccountId  != null){
				
				accountIdSet.add(conObj.AccountId);
			}
		}
		accList = [Select Id, (Select startdate__c, enddate__c, AccountId From Contracts) From Account Where Id IN :accountIdSet]
		for(Account accObj : accList){
			
			idVsContractmap.put(accObj.id, accObj.Contracts);
		}
		for(Contract conObj : contractList){
			
			if(conObj.AccountId != null){
			
				if(idVsContractmap.containsKey(conObj.AccountId)){
					
					for(Contract contractObj : idVsContractmap.get(conObj.AccountId)){
						
						if(contractObj.enddate__c.month() == conObj.startdate__c.month()){
							
							if(contractObj.enddate__c.month() - 1 == 2){
							
								if(MOD( contractObj.enddate__c.year()) + 1, 4) = 0){
									
									string newDate = 29 +'//'+contractObj.enddate__c.month() - 1+'//'+contractObj.enddate__c.year();
									contractObj.enddate__c =  date.parse(newDate);
								}
								else{
									
									string newDate = 28 +'//'+contractObj.enddate__c.month() - 1+'//'+contractObj.enddate__c.year();
									contractObj.enddate__c =  date.parse(newDate);
								}
							}
							else if(contractObj.enddate__c.month() - 1 == 0){
								
								string newDate = 31 +'//'+12+'//'+contractObj.enddate__c.year() -1;
								contractObj.enddate__c =  date.parse(newDate);
							}
							else{
								
								string newDate = 31 +'//'+dateMap.get(contractObj.enddate__c.month() - 1)+'//'+contractObj.enddate__c.year() -1;
								contractObj.enddate__c =  date.parse(newDate);
							}
						}
						updateContractList.add(contractObj);
					}
				}
			}
		}
		update updateContractList;
	}
}

Thank yo,
Amol Salve
Salesforce Developer