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
Asel Vazir 1Asel Vazir 1 

apex class and trigger creating two opportunities when account is inserted or updated

Help me to figure out why when I try to update the Account without Opps it doesn't create two new Opps as per below logic.
Here is my helper class
public with sharing class AccountWithTwoDefaultOpp {
    public static void addTwoDefaultOpptToAcct(List<Account> acctList) {

        List<Opportunity> oppList = new List<Opportunity>();

        for (Account acct : acctList) {

            Opportunity opp = new Opportunity();
            opp.Name = acct.Name + ' Default Residential Opp';
            opp.StageName = 'Prospecting';
            opp.CloseDate = System.today().addMonths(1);
            opp.AccountId = acct.Id;
            oppList.add(opp);

            Opportunity opp2 = new Opportunity();
            opp2.Name = acct.Name + 'Default Commercial Opp';
            opp2.StageName = 'Prospecting';
            opp2.CloseDate = System.today().addMonths(1);
            opp2.AccountId = acct.Id;
            oppList.add(opp2);
        }
        insert oppList;
    }
    public static void updateAccount(List<Account> acctList2) {

        List<Opportunity> oppList2 = new List<Opportunity>();

        Map<Id, Account> acctsTypeProspect = new Map<Id, Account>(
        [
                SELECT Id, Name, Type
                FROM Account
                WHERE Type = 'Prospect'
                AND Id NOT IN (SELECT AccountId FROM Opportunity) AND Id IN :acctList2
        ]);

        for (Account account : acctList2) {
            if(account.Type == 'Prospect'){
        }
            for (Opportunity opp : oppList2) {

                if (acctsTypeProspect.get(account.Id).Opportunities.isEmpty()) {

                    Opportunity opp1 = new Opportunity();
                    opp1.Name = 'Default Residential Opp';
                    opp1.AccountId = account.Id;
                    opp1.CloseDate = System.today().addMonths(1);
                    opp1.StageName = 'Prospecting';
                    oppList2.add(opp1);

                    Opportunity opp2 = new Opportunity();
                    opp2.Name = 'Default Commercial Opp';
                    opp2.AccountId = account.Id;
                    opp2.CloseDate = System.today().addMonths(1);
                    opp2.StageName = 'Prospecting';
                    oppList2.add(opp2);
                }

            }
            if(oppList2.size()>0){
                insert oppList2;

            }
        }

    }
}
And Trigger 
trigger AddTwoOppToNewAccount on Account (after insert, after update) {
    if (Trigger.isInsert && Trigger.isAfter){
        AccountWithTwoDefaultOpp.addTwoDefaultOpptToAcct(Trigger.new);
    }

    if  (Trigger.isUpdate) {
        AccountWithTwoDefaultOpp.updateAccount(Trigger.new);
    }
}

 
Jayni belaniJayni belani
Hi,
Are you getting values in map ?
Prangya JenaPrangya Jena
Hi Asel Vazir,

I have done few changes in second method and its working fine. Please check and mark it as best answer if works.
 
public with sharing class AccountWithTwoDefaultOpp {
    public static void addTwoDefaultOpptToAcct(List<Account> acctList) {
        
        List<Opportunity> oppList = new List<Opportunity>();

        for (Account acct : acctList) {

            Opportunity opp = new Opportunity();
            opp.Name = acct.Name + ' Default Residential Opp';
            opp.StageName = 'Prospecting';
            opp.CloseDate = System.today().addMonths(1);
            opp.AccountId = acct.Id;
            oppList.add(opp);

            Opportunity opp2 = new Opportunity();
            opp2.Name = acct.Name + 'Default Commercial Opp';
            opp2.StageName = 'Prospecting';
            opp2.CloseDate = System.today().addMonths(1);
            opp2.AccountId = acct.Id;
            oppList.add(opp2);
        }
       
        insert oppList;
    }
    public static void updateAccount(List<Account> acctList2) {
        
        List<Opportunity> oppList2 = new List<Opportunity>();

        Map<Id, Account> acctsTypeProspect = new Map<Id, Account>(
        [
                SELECT Id, Name, Type
                FROM Account
                WHERE Type = 'Prospect'
                AND Id NOT IN (SELECT AccountId FROM Opportunity) AND Id IN :acctList2
        ]);
       // no need to check Type field in accList2 as in your query you are already filtering out
     // no need to Loop opportunity list ans the list is blank till now.

        for (Id accountId : acctsTypeProspect.keySet()) {

                if (acctsTypeProspect.get(accountId).Opportunities.isEmpty()) {

                    Opportunity opp1 = new Opportunity();
                    opp1.Name = 'Default Residential Opp';
                    opp1.AccountId = accountId;
                    opp1.CloseDate = System.today().addMonths(1);
                    opp1.StageName = 'Prospecting';
                    oppList2.add(opp1);

                    Opportunity opp2 = new Opportunity();
                    opp2.Name = 'Default Commercial Opp';
                    opp2.AccountId = accountId;
                    opp2.CloseDate = System.today().addMonths(1);
                    opp2.StageName = 'Prospecting';
                    oppList2.add(opp2);

            }
            if(oppList2.size()>0){
                insert oppList2;

            }
        }

    }
}