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
niharnihar 

trigger on opportunities records to change the closed dates of every opportunities records under account object

hi all,
My Task is to change the due date or closed date of opportunity records under account object 
1st opportunity record to 75 days == created date + 75 days
2nd and 3rd or more to 180 days == created date +180 days



thanks in advance...
Best Answer chosen by nihar
Raj2019ssRaj2019ss
Hi nihar,

Try the below code. It's working me.
//Description : To update Close Date Automatically
    public static void updateCloseDate(List<Opportunity> lstOpp){
        Set<Id> setAccId 		= new Set<Id>();
        Integer countOpp;
        for(Opportunity oppForVar : lstOpp){
            if(oppForVar.AccountId != null){
            	setAccId.add(oppForVar.AccountId);
            }
        }
        countOpp = [SELECT count() FROM Opportunity WHERE AccountId =: setAccId];
            for(Opportunity oppForvar : lstOpp){
                 Date createdDate = date.newinstance(oppForvar.CloseDate.year(), oppForvar.CloseDate.month(), oppForvar.CloseDate.day());
                String sdate = String.valueOf(createdDate);
                if(countOpp == 1){
                oppForvar.CloseDate = Date.valueOf(sdate) + 75;
            }
                else{
                oppForvar.CloseDate = Date.valueOf(sdate) + 180;
            }
        }
    }
Trigger:
trigger OpportunityTrigger on Opportunity (before insert, before update, after insert, after update) {
 if(trigger.isBefore && trigger.isInsert){
        OpportunityTriggerHandler.updateCloseDate(trigger.new);
}
}
please try and let me know. if it helps you, please mark it as solved and the best answer

 

All Answers

Raj2019ssRaj2019ss
Hi nihar,

Try the below code. It's working me.
//Description : To update Close Date Automatically
    public static void updateCloseDate(List<Opportunity> lstOpp){
        Set<Id> setAccId 		= new Set<Id>();
        Integer countOpp;
        for(Opportunity oppForVar : lstOpp){
            if(oppForVar.AccountId != null){
            	setAccId.add(oppForVar.AccountId);
            }
        }
        countOpp = [SELECT count() FROM Opportunity WHERE AccountId =: setAccId];
            for(Opportunity oppForvar : lstOpp){
                 Date createdDate = date.newinstance(oppForvar.CloseDate.year(), oppForvar.CloseDate.month(), oppForvar.CloseDate.day());
                String sdate = String.valueOf(createdDate);
                if(countOpp == 1){
                oppForvar.CloseDate = Date.valueOf(sdate) + 75;
            }
                else{
                oppForvar.CloseDate = Date.valueOf(sdate) + 180;
            }
        }
    }
Trigger:
trigger OpportunityTrigger on Opportunity (before insert, before update, after insert, after update) {
 if(trigger.isBefore && trigger.isInsert){
        OpportunityTriggerHandler.updateCloseDate(trigger.new);
}
}
please try and let me know. if it helps you, please mark it as solved and the best answer

 
This was selected as the best answer
Raj2019ssRaj2019ss
Hi nihar,

Thank you very much.
If you any doubts please let me know
niharnihar
hi rajeshkumar,
thank you for your code but i am getting :(wrong)
1st and 3rd or more opportunity record to 180 days 
2nd opportunity record  to 75 days

But i must get :
1st opportunity record to 75 days 
2nd and 3rd or more to 180 days 
 
Raj2019ssRaj2019ss
Hi,
I have modifed. Before i used close date. Sorry for that. Now i have changed to System.today(). Try the below one and let me know.
I need one more information. When we need to trigger that below code. I meant on Insert or update or both.
//Description : To update Close Date Automatically
    public static void updateCloseDate(List<Opportunity> lstOpp){
        Set<Id> setAccId 		= new Set<Id>();
        Integer countOpp;
        for(Opportunity oppForVar : lstOpp){
            if(oppForVar.AccountId != null){
            	setAccId.add(oppForVar.AccountId);
            }
        }
        countOpp = [SELECT count() FROM Opportunity WHERE AccountId =: setAccId];
            for(Opportunity oppForvar : lstOpp){
                if(countOpp == 1){
                    oppForvar.CloseDate = System.today() + 75;
            }
                else{
                    oppForvar.CloseDate = System.today() + 180;
            }
        }
    }

 
niharnihar
hi rajeshkumar,
once again i am facing same error i am getting :(wrong)
1st and 3rd or more opportunity record to 180 days 
2nd opportunity record  to 75 days

But i must get :
1st opportunity record to 75 days 
2nd and 3rd or more to 180 days
Raj2019ssRaj2019ss
Hi

What output are you getting? 

I need one more information. When we need to trigger that below code. I meant on Insert or update or both.
niharnihar
hi, we want both insert and update
i am getting output 1st opportunity and 3 rd opportunity and more opportunitys = created date + 180 days
2nd opportunity =create date + 75 days

But i must get :
1st opportunity record to 75 days 
2nd and 3rd or more to 180 days

change 1st opp to = create date + 75 days
2nd and 3rd or more = create date +180 days
Raj2019ssRaj2019ss
Hi
try the below one let me know
    //Description : To update Close Date Automatically
    public static void updateCloseDate(List<Opportunity> lstOpp){
        Set<Id> setAccId 		= new Set<Id>();
        Integer countOpp;
         String sdate;
        for(Opportunity oppForVar : lstOpp){
            if(oppForVar.AccountId != null){
            	setAccId.add(oppForVar.AccountId);
            }
        }
        countOpp = [SELECT count() FROM Opportunity WHERE AccountId =: setAccId];
            for(Opportunity oppForvar : lstOpp){
                if(countOpp == 0){
                    oppForvar.CloseDate = System.today() + 75;
            }
                else{
                    oppForvar.CloseDate = System.today() + 180;
            }
        }
    }
}

 
niharnihar
hi
the code is working now
niharnihar
hi rajesh,
thank you for your valubale time i posted a new question in my org or profile so please go through it and try to solve..........................