+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

Date Range restriction 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 

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 

Thanks in advance
Mustafa JhabuawalaMustafa Jhabuawala
Hi,

You need to write a trigger to achieve the said functionality.

I am not sharing code as of now, but I can guide you on how you can achieve this.

You need to follow below mentioned steps - 
1. Need to write an after insert & update trigger
2. In trigger while insert or update you need to check that is there any contract with in the same period -
For ex -  you are inserting ABC Contract with Start Date - 1st Feb 2017 & End Date - 31st March. So you need to check that is there any contracts which fall into the same date range using SOQL
3. Depending on the results you will get by performing operation as mentioned in step 2 you need to change the dates of the contracts

Note - It will be difficult to manage this scenario via code, because some or the other use case will always fail in this requirement as far as I can imagine this. I would suggest you that it should be handled by user itself, code should be responsible only to inform the users that there are contracts with some oevrlapping dates, so that user can navigate through those manually and can change those. I feel this option will be better.

Hope this helps you to solve your problem.

Thanks,
Mustafa Jhabuawala
Technical Lead at Zen4orce (http://www.zen4orce.com)
sfdc dev 2264sfdc dev 2264
Can you please help me with the code please bro

​Thanks
Amol Salve 13Amol Salve 13
Hello,
use below code,
 
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 you,
Amol Salve
Salesforce Developer 
sfdc dev 2264sfdc dev 2264
Thanks for the code bro ,i will check and let you know if any issues
sfdc dev 2264sfdc dev 2264
Hi ,

I tried the above code , I am getting the following error on save


Error: Compile Error: unexpected syntax: 'mismatched input ',' expecting MAPPED_TO' at line 18 column 11
Amol Salve 14Amol Salve 14
Hello.
actually I had done small mistake..please use this map
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, };
 insted of this
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, };

there is syntax error at last line

thank you
Amol Salve
Salesforce Developer
sfdc dev 2264sfdc dev 2264
i tried the same , getting the below error,

Error: Compile Error: unexpected token: '}' at line 18 column 18
sfdc dev 2264sfdc dev 2264
MY CLASS USED :

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;
    }
}

Line 18 is

12 =>31, };
sfdc dev 2264sfdc dev 2264
i tried the same , getting the below error,

Error: Compile Error: unexpected token: '}' at line 18 column 18

Please help me bro
 
Amol Salve 14Amol Salve 14
Hello,

Please remove comma from after 31 from this line12 =>31, };