0

I have an apex class that doe snot throw an error but does not create the new record. I am trying to create a program__c record when the opportunity is 'closed won'. I would like to be able to grab the id of the newly created record and then create another record using the new Id

 public void afterUpdate (SObject oldso, SObject newso)  {

    Opportunity oopp = (Opportunity )oldso;
    Opportunity nopp = (Opportunity )newso;

    List<Program__c> ProgramList=new List<Program__c>();
        if (nopp.StageName =='Closed Won') {
            Program__c p = new Program__c();
            system.debug('op ID:'+ nopp.id);
            p.Opportunity__c = nopp.id;
            p.Name = 'Test Name';
            insert p;
        }

}
1

The issue with your code is that you create the opportunity list oppty, but it will always be empty so your for loop will not be hit. Likewise, since the list is empty, you can't possibly have an opportunity with a StageName of 'Closed Won'. I believe what you need to work with is passed into the method as arguments. Is this code called from a trigger? The afterUpdate name makes me think it is. You will want to bulkify your code if that's the case.

6
  • Yes, I am trying to stay with 1 trigger per object so I am passing the opportunity to the class 'OpportunityTriggerHandler.class,'Opportunity'. I will take the insert out of the for loop as previously suggested.
    – Phuc
    Jun 4 '20 at 15:18
  • By bulkifying, I mean you want to pass all of your trigger Opportunities to your afterUpdate method at one time. Otherwise you're needlessly pushing and popping afterUpdate onto your call stack potentially 200 times (that's how many records can enter a trigger at once). What you want to do is pass all of the Opportunities into the method, build a List<Program__c> of Programs to insert, and then make only one insert call. See SFDC FAN's answer for example code.
    – nbrown
    Jun 4 '20 at 15:29
  • I have updated the code based on SFDC fans example. In the debug log the op variable is null as you stated. Where I do see the opportunity id is in the oldso and newso variables. So how do I reference these variables in the for loop.
    – Phuc
    Jun 4 '20 at 15:51
  • If you're only passing in one opportunity there's no reason for a list or a for-loop at all... I believe it's newso you want to check for "Closed Won" and make your Program__c accordingly
    – nbrown
    Jun 4 '20 at 16:22
  • That worked. Thank you. let me know if I need to close this post and address this additional question in another post, I have an object(place) that is a related record on the opportunity. I want to create a new record when a new place record is created. Should I create another class or can I use the same opportunity class and detect the creation of the new place record. Just want to make sure I am approaching this correctly.
    – Phuc
    Jun 4 '20 at 17:44
0
  • Please check in debug Logs: Any error occurs while Inserting
  • Dont write DML Statements inside the for Loop
  • In your code oppty, doesnt have any Value
  • oppty is empty,so for loop isnt iterating

    List<Program__c> ProgramList=new List<Program__c>();
     for (Opportunity op : oppty) {
    Program__c p = new Program__c();
    p.Opportunity__c = op.id;
    p.Name = 'Test Name';
    ProgramList.add(p);
    }
     try{
      insert ProgramList;
    }
      catch(exception ex){
       //catch error
    }
    
9
  • This is on the right track, but the List<Program__c> needs instantiated outside the for-loop, otherwise it gets destroyed and recreated with every iteration.
    – nbrown
    Jun 4 '20 at 15:29
  • yes.I havent seen,mistake happened in hurry..its by mistake updated thanks Jun 4 '20 at 15:31
  • 1
    You should never swallow exceptions with System.debug(). You do not need to check isEmpty() before performing DML.
    – David Reed
    Jun 4 '20 at 15:32
  • okay. if i dont check it allows to insert empty List. if program list is empty. correct me if i am wrong Jun 4 '20 at 15:34
  • Inserting an empty list is a no-op.
    – David Reed
    Jun 4 '20 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.