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
Abilash.SAbilash.S 

How to write Trigger to create opportunity, if existing opportunity has 90% probability for that account.

Hi Everyone
I have wrote code to create opportunity, if existing opp has 90% probability. So I have used parent-child query to queru opportunities related accounts.
I have used FOR - IN - FOR loops. The records are creating in huge number. Please provide proper code with single for loop and use handler class. I came to know that should not use tripple for loops. Then how to achieve without multiple loops.


public static void createOpp(List<Account> accList) {
        
       List<Opportunity> oppList = new List<Opportunity>();
        Map<Id, Account> acctMap = new Map<Id, Account>([Select Id, (Select Id, probability from Opportunities) from Account WHERE ID IN :accList]);
        
        for(Account a : accList) {
        
            for(Account ao: acList) {
            
                    for(Opportunity opp : ao.opportunities) {

                        if( opp.probability == 90){
                            Opportunity opptny = new Opportunity();
                            opptny.AccountId = a.Id;
                            opptny.Name      = a.Name + 'Opportunity with 90% prob new';
                            opptny.StageName = 'Prospecting';
                            opptny.probability = 90;
                            opptny.CloseDate = System.today().addMonths(3);
                            oppList.add(opptny);
                        }
                   }
               }
            }
        
        try{
        if(oppList.size() > 0){
            insert oppList;
            }
        } catch (DmlException e) {
            system.debug('Exception caused-->'+e.getMessage());
       } 
   }
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

Do you want create new opportunity for the existing opportunity has 90% propability and the new opp should link to the same account?

Thanks!!
Abilash.SAbilash.S
Yes. I need to create new opp for the account, if existing opp has 90% probability for same account.
AnkaiahAnkaiah (Salesforce Developers) 
try with below code.
trigger CreateOpportunity on Opportunity (after insert,after update) {


	List<Opportunity> createnewopp = new List<Opportunity>();
	
	for(opportunity opp:trigger.new){
	if(opp.probability ==90 ){
	
		Opportunity opptny = new Opportunity();
		opptny.AccountId = opp.Id;
		opptny.Name      = opp.Name + 'Opportunity with 90% prob new';
		opptny.StageName = 'Prospecting';
		opptny.probability = 90;
		opptny.CloseDate = System.today().addMonths(3);
       createnewopp.add(opptny);
	
	}
	}

	insert createnewopp;
	
}

If this helps, Please mark it as best answer.

Thanks!!
Abilash.SAbilash.S
Thanks ankaiah. But I need to create new opportunity for same account if account opportunity has 90% probability. Before create new opp, we need to check existing opp for account has 90% prob
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abikash,

My code will work for same as you mentioned.

Thanks!!