+ Start a Discussion
Robert Lange 6Robert Lange 6 

apex helper class to create two opportunities

I am trying to write a trigger and helper class for an Account that has the Type as "Prospect".  If the Account is updated and has no related Opportunities, two new Opportunities are added to the Account.  I have not had any luck getting the code to work.

Here is my Trigger:

trigger ResComOpportunityTrigger on Account (after update) {
    if(Trigger.isAfter && Trigger.isUpdate){
        ResComOpportunityHelper.UpdateOpps(Trigger.new);
    }
}

Here is my helper class:

public with sharing class ResComOpportunityHelper {

    static public void UpdateOpps(List<Account> acctList) {

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

        acctList = [
                SELECT Id, Name, OwnerId, (SELECT Name, Id FROM Opportunities)
                FROM Account
                WHERE Type = 'Prospect'
        ];

        for (Account accUp : acctList) {
            for (Opportunity opp : accUp.Opportunities) {
                if (opp.Name == null) {
                    Opportunity opp1 = new Opportunity();
                    opp1.Name = 'Default Residential Opp';
                    opp1.AccountId = accUp.Id;
                    opp1.CloseDate = System.today().addMonths(3);
                    opp1.StageName = 'Prospecting';
                    updateOppsList.add(opp1);

                    Opportunity opp2 = new Opportunity();
                    opp2.Name = 'Default Commercial Opp';
                    opp2.AccountId = accUp.Id;
                    opp2.CloseDate = System.today().addMonths(3);
                    opp2.StageName = 'Prospecting';
                    updateOppsList.add(opp2);
                }
            }
        }
        insert updateOppsList;
    }
}

Any assistance would be appreciated!
Best Answer chosen by Robert Lange 6
Prangya JenaPrangya Jena
Hi ,

OR you can filter the accounts which do not have opportunities like:

SELECT Id, Name,OwnerId FROM Account  WHERE Type = 'Prospect' AND Id NOT IN (SELECT AccountId FROM Opportunity) AND Id IN 
acctMap.Keyset();

So that you dont have check if(acc.Opportunities)

All Answers

sfdcBahusfdcBahu
Here you go. Keep me posted on how it goes.
since the child list is null, the code was not passing on your Optty Creation block

trigger ResComOpportunityTrigger on Account (after update)
{

    if(Trigger.isAfter && Trigger.isUpdate)
    {
        ResComOpportunityHelper.UpdateOpps(Trigger.newMap);
    }

}

public with sharing class ResComOpportunityHelper {

    static public void UpdateOpps(Map<Id,Account> acctMap) {

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

        List<Account> acctList = 
        [
                SELECT Id, Name, OwnerId, (SELECT Name, Id FROM Opportunities)
                FROM Account
                WHERE Type = 'Prospect' and id in: acctMap.Keyset();
        ];

        for (Account accUp : acctList) 
        {
          if(accUp.Opportunities == null)
           {
                    Opportunity opp1 = new Opportunity();
                    opp1.Name = 'Default Residential Opp';
                    opp1.AccountId = accUp.Id;
                    opp1.CloseDate = System.today().addMonths(3);
                    opp1.StageName = 'Prospecting';
                    updateOppsList.add(opp1);

                    Opportunity opp2 = new Opportunity();
                    opp2.Name = 'Default Commercial Opp';
                    opp2.AccountId = accUp.Id;
                    opp2.CloseDate = System.today().addMonths(3);
                    opp2.StageName = 'Prospecting';
                    updateOppsList.add(opp2);
            }
        }
        
      if(updateOppsList.size() > 0)
        insert updateOppsList;
    }
}
Prangya JenaPrangya Jena
Hi ,

OR you can filter the accounts which do not have opportunities like:

SELECT Id, Name,OwnerId FROM Account  WHERE Type = 'Prospect' AND Id NOT IN (SELECT AccountId FROM Opportunity) AND Id IN 
acctMap.Keyset();

So that you dont have check if(acc.Opportunities)
This was selected as the best answer