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 

DATE OVERLAPPING TRIGGER 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


I tried the below apex class
 
public class overlapDateHandler{

	public void overlapDateMethod(list<Contract__c> 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__c>> idVsContractmap = new map<id, list<Contract__c>>();
		list<Contract__c> updateContractList = new list<Contract__c>();
		for(Contract__c conObj : contractList){
			
			if(conObj.AccountId  != null){
				
				accountIdSet.add(conObj.AccountId);
			}
		}
		accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, AccountId From Contract__c) From Account Where Id IN :accountIdSet]
		for(Account accObj : accList){
			
			idVsContractmap.put(accObj.id, accObj.Contract__c);
		}
		for(Contract__c conObj : contractList){
			
			if(conObj.AccountId != null){
			
				if(idVsContractmap.containsKey(conObj.AccountId)){
					
					for(Contract contractObj : idVsContractmap.get(conObj.AccountId)){
						
						if(contractObj.Contract_End_Date__c.month() == conObj.startdate__c.month()){
							
							if(contractObj.Contract_End_Date__c.month() - 1 == 2){
							
								if(MOD( contractObj.Contract_End_Date__c.year()) + 1, 4) = 0){
									
									string newDate = 29 +'//'+contractObj.Contract_End_Date__c.month() - 1+'//'+contractObj.Contract_End_Date__c.year();
									contractObj.Contract_End_Date__c =  date.parse(newDate);
								}
								else{
									
									string newDate = 28 +'//'+contractObj.Contract_End_Date__c.month() - 1+'//'+contractObj.Contract_End_Date__c.year();
									contractObj.Contract_End_Date__c =  date.parse(newDate);
								}
							}
							else if(contractObj.Contract_End_Date__c.month() - 1 == 0){
								
								string newDate = 31 +'//'+12+'//'+contractObj.Contract_End_Date__c.year() -1;
								contractObj.Contract_End_Date__c =  date.parse(newDate);
							}
							else{
								
								string newDate = 31 +'//'+dateMap.get(contractObj.Contract_End_Date__c.month() - 1)+'//'+contractObj.Contract_End_Date__c.year() -1;
								contractObj.Contract_End_Date__c =  date.parse(newDate);
							}
						}
						updateContractList.add(contractObj);
					}
				}
			}
		}
		update updateContractList;
	}
}

I am gettingthe below error

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

12 =>31, };

Help me pls

 
Best Answer chosen by sfdc dev 2264
Amit Singh 1Amit Singh 1
Error is due to the extra comma(,) that you are using after 31 remove that and error will be removed.
 
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 };
thanks
 

All Answers

Amit Singh 1Amit Singh 1
Error is due to the extra comma(,) that you are using after 31 remove that and error will be removed.
 
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 };
thanks
 
This was selected as the best answer
Raj VakatiRaj Vakati
Hi ,
Your line number 18 syntax is wrong. Please use this syntax  . extra ',' is there in your code  
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 };

 
sfdc dev 2264sfdc dev 2264
Thanks

i changed it

but getting the below error

Error: Compile Error: Didn't understand relationship 'Contract__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 28 column 19

accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contract__c where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet]


contracts is a custom object child of account.
sfdc dev 2264sfdc dev 2264
pls let me know whats the issue in this query

 
Amit Singh 1Amit Singh 1
Change your query with below query
 
accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__r where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet]
You need to give relationship name.
If above qury does nit works use below
accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__c where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet]
thanks

 
sfdc dev 2264sfdc dev 2264
I tried the above query it its now showing 


Error: Compile Error: expecting a semi-colon, found 'for' at line 29 column 8

 accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__r where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet]
        for(Account accObj : accList){
            
            idVsContractmap.put(accObj.id, accObj.Contract__c);
        }

if i had semicolon in the query

  accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__r where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet];

it gives

Error: Compile Error: expecting a right parentheses, found ',' at line 45 column 84


    if(MOD( contractObj.Contract_End_Date__c.year()) + 1, 4) = 0){​

Please help me 
sfdc dev 2264sfdc dev 2264
Error: Compile Error: expecting a semi-colon, found 'for' at line 29 column 8

 accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__r where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet]
        for(Account accObj : accList){
            
            idVsContractmap.put(accObj.id, accObj.Contract__c);
        }

if i had semicolon in the query

  accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__r where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet];

it gives

Error: Compile Error: expecting a right parentheses, found ',' at line 45 column 84

   if(contractObj.Contract_End_Date__c.month()- 1 == 2){
    if(MOD( contractObj.Contract_End_Date__c.year()) + 1, 4) = 0){​

Please help me 
Amit Singh 1Amit Singh 1
use below code
 if(contractObj.Contract_End_Date__c.month()- 1 == 2){
    if(MOD(contractObj.Contract_End_Date__c.year()+ 1,  4)) = 0){​
sfdc dev 2264sfdc dev 2264
i tried the above code

getting the below error now

Error: Compile Error: unexpected token: '=' at line 44 column 79

                         if(contractObj.Contract_End_Date__c.month()- 1 == 2){
sfdc dev 2264sfdc dev 2264
Hi bro,

I removed it already 

getting the following error now

Error: Compile Error: unexpected token: '=' at line 44 column 79

                         if(contractObj.Contract_End_Date__c.month()- 1 == 2){
APEX CLASS :


public class overlapDateHandler{

    public void overlapDateMethod(list<Contract__c> 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__c>> idVsContractmap = new map<id, list<Contract__c>>();
        list<Contract__c> updateContractList = new list<Contract__c>();
        for(Contract__c conObj : contractList){
            
            if(conObj.Account__c  != null){
                
                accountIdSet.add(conObj.Account__c);
            }
        }
  accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__r where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet];
        for(Account accObj : accList){
            
            idVsContractmap.put(accObj.id, accObj.Contract__c);
        }
        for(Contract__c conObj : contractList){
            
            if(conObj.Account__c != null){
            
                if(idVsContractmap.containsKey(conObj.Account__c)){
                    
                    for(Contract contractObj : idVsContractmap.get(conObj.Account__c)){
                        
                        if(contractObj.Contract_End_Date__c.month() == conObj.Contract_Start_Date__c.month()){
                        
                         if(contractObj.Contract_End_Date__c.month()- 1 == 2){
                       if(MOD(contractObj.Contract_End_Date__c.year()+ 1,  4)) = 0){​
                            
                                    
                                    string newDate = 29 +'//'+contractObj.Contract_End_Date__c.month() - 1+'//'+contractObj.Contract_End_Date__c.year();
                                    contractObj.Contract_End_Date__c =  date.parse(newDate);
                                }
                                else{
                                    
                                    string newDate = 28 +'//'+contractObj.Contract_End_Date__c.month() - 1+'//'+contractObj.Contract_End_Date__c.year();
                                    contractObj.Contract_End_Date__c =  date.parse(newDate);
                                }
                            }
                            else if(contractObj.Contract_End_Date__c.month() - 1 == 0){
                                
                                string newDate = 31 +'//'+12+'//'+contractObj.Contract_End_Date__c.year() -1;
                                contractObj.Contract_End_Date__c =  date.parse(newDate);
                            }
                            else{
                                
                                string newDate = 31 +'//'+dateMap.get(contractObj.Contract_End_Date__c.month() - 1)+'//'+contractObj.Contract_End_Date__c.year() -1;
                                contractObj.Contract_End_Date__c =  date.parse(newDate);
                            }
                        }
                        updateContractList.add(contractObj);
                    }
                }
            }
        }
        update updateContractList;
    }
}

Please help me bro

Thanks​
Amit Singh 1Amit Singh 1
use below code
if((contractObj.Contract_End_Date__c.month()- 1) == 2){

 
sfdc dev 2264sfdc dev 2264
getting the below error


MY APEX CLASS :


public class overlapDateHandler{

    public void overlapDateMethod(list<Contract__c> 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__c>> idVsContractmap = new map<id, list<Contract__c>>();
        list<Contract__c> updateContractList = new list<Contract__c>();
        for(Contract__c conObj : contractList){
            
            if(conObj.Account__c  != null){
                
                accountIdSet.add(conObj.Account__c);
            }
        }
  accList = [Select Id, (Select Contract_Start_Date__c, Contract_End_Date__c, Account__c,Status__c From Contracts__r where Status__c='Signed by Customer') From Account Where Id IN :accountIdSet];
        for(Account accObj : accList){
            
            idVsContractmap.put(accObj.id, accObj.Contract__c);
        }
        for(Contract__c conObj : contractList){
            
            if(conObj.Account__c != null){
            
                if(idVsContractmap.containsKey(conObj.Account__c)){
                    
                    for(Contract contractObj : idVsContractmap.get(conObj.Account__c)){
                        
                        if(contractObj.Contract_End_Date__c.month() == conObj.Contract_Start_Date__c.month()){
                        
                     if((contractObj.Contract_End_Date__c.month()- 1) = 2){
                      if(MOD(contractObj.Contract_End_Date__c.year()+ 1,  4)) = 0){​
                         // if(MOD( contractObj.Contract_End_Date__c.year())+ 1, 4) = 0){​

                            
                                    
                                    string newDate = 29 +'//'+contractObj.Contract_End_Date__c.month() - 1+'//'+contractObj.Contract_End_Date__c.year();
                                    contractObj.Contract_End_Date__c =  date.parse(newDate);
                                }
                                else{
                                    
                                    string newDate = 28 +'//'+contractObj.Contract_End_Date__c.month() - 1+'//'+contractObj.Contract_End_Date__c.year();
                                    contractObj.Contract_End_Date__c =  date.parse(newDate);
                                }
                            }
                            else if(contractObj.Contract_End_Date__c.month() - 1 == 0){
                                
                                string newDate = 31 +'//'+12+'//'+contractObj.Contract_End_Date__c.year() -1;
                                contractObj.Contract_End_Date__c =  date.parse(newDate);
                            }
                            else{
                                
                                string newDate = 31 +'//'+dateMap.get(contractObj.Contract_End_Date__c.month() - 1)+'//'+contractObj.Contract_End_Date__c.year() -1;
                                contractObj.Contract_End_Date__c =  date.parse(newDate);
                            }
                        }
                        updateContractList.add(contractObj);
                    }
                }
            }
        }
        update updateContractList;
    }
}


 
sfdc dev 2264sfdc dev 2264
I have a controller for which i have written a test class which covers only 50%, I am not able to cover the following lines for which i need help on it

//
   if(firstcontract.Contract_Start_Date__c<contractObj.Contract_Start_Date__c && firstcontract.Contract_End_Date__c<contractObj.Contract_End_Date__c ){
                          if(firstcontract.Contract_End_Date__c>=contractObj.Contract_Start_Date__c){
                          firstcontract.Contract_End_Date__c=contractObj.Contract_Start_Date__c.addDays(-1);
                          update firstcontract;//put the list and update
                          }                       
                        }  
                        else{
                        system.debug('elsesection');
                        if(contractObj.Contract_End_Date__c>=firstcontract.Contract_Start_Date__c){
                          contractObj.Contract_End_Date__c=firstcontract.Contract_Start_Date__c.addDays(-1);
                          update contractObj;// put the list and update
                          }    
                          
                          
                          //
                          
                            if(idVsContractmap.containsKey(controbj.Account__r.Id)) {
                List<Contract__c> contrAccList = idVsContractmap.get(controbj.Account__r.Id);
                contrAccList.add(controbj);
                idVsContractmap.put(controbj.Account__c, contrAccList);
            } else {
                idVsContractmap.put(controbj.Account__c, new List<Contract__c> { controbj });
            }   
            //

Kindly help me pls

Thanks in advance