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 

Help to figure out why my code is not creating related opportunity if account is updated

Here is my method that should create another missing related opportunity if the account is updated and have only one opportunity. 
public static void missingOpp(List<Account> acctList3) {

        List<Opportunity> oppList3 = new List<Opportunity>();
        List <Account> acctsTypeProspect = new List<Account>(
        [
                SELECT Id, Name, OwnerId, Type
                FROM Account
                WHERE Type = 'Prospect' AND Id IN (SELECT AccountId FROM Opportunity) AND Id IN :acctList3
        ]);
        for (Account acct : acctsTypeProspect) {
            for (Opportunity oppt : acct.Opportunities) {
                if (oppt.Name == 'Default Residential Opp') {
                }
                Opportunity opp1 = new Opportunity();
                opp1.Name = 'Default Commercial Opp';
                opp1.AccountId = acct.Id;
                opp1.CloseDate = System.today().addMonths(1);
                opp1.StageName = 'Prospecting';
                oppList3.add(opp1);

                if (oppt.Name == 'Default Commercial Opp') {
                }
                Opportunity opp2 = new Opportunity();
                opp2.Name = 'Default Residential Opp';
                opp2.AccountId = acct.Id;
                opp2.CloseDate = System.today().addMonths(1);
                opp2.StageName = 'Prospecting';
                oppList3.add(opp2);
            }
            insert oppList3;
        }
    }

 
Danish HodaDanish Hoda
Hi Asel,
Please refer below code:
public static void missingOpp(List<Account> acctList3) {

        List<Opportunity> oppList3 = new List<Opportunity>();
        List <Account> acctsTypeProspect = new List<Account>(
        [
                SELECT Id, Name, OwnerId, Type
                FROM Account
                WHERE Type = 'Prospect' AND Id IN (SELECT AccountId FROM Opportunities) AND Id IN :acctList3 
        ]); //Opportunities is the child-relationship name, not Opportunity
        for (Account acct : acctsTypeProspect) {
            for (Opportunity oppt : acct.Opportunities) {
                if (oppt.Name == 'Default Residential Opp') {
                }
                Opportunity opp1 = new Opportunity();
                opp1.Name = 'Default Commercial Opp';
                opp1.AccountId = acct.Id;
                opp1.CloseDate = System.today().addMonths(1);
                opp1.StageName = 'Prospecting';
                oppList3.add(opp1);

                if (oppt.Name == 'Default Commercial Opp') {
                }
                Opportunity opp2 = new Opportunity();
                opp2.Name = 'Default Residential Opp';
                opp2.AccountId = acct.Id;
                opp2.CloseDate = System.today().addMonths(1);
                opp2.StageName = 'Prospecting';
                oppList3.add(opp2);
            }
        }
		if(!oppList3.isEmpty())	insert oppList3;  //Try to have DML statements outside the for-loop and do null-check 
    }