0

A bit lost on how to finish this trigger.

Scenario: - After creating a new opportunity
- Object Implementation should populate a list of 12 Records
- Each record then has it’s own set of task - Task’s may range from 10 to 30 tasks

  trigger IMP on Implementation__c (after insert) {  
        List <Implementation__c> ImpToInsert = new List <Implementatiom__c> 
    for (Implementation : Trigger.new) {

Am I headed in the right direction? I tried following the example in this link but I got a little lost as my scenario is a little different. https://help.salesforce.com/apex/HTViewSolution?id=000176618&language=en_US enter image description here

Thanks!

14
  • 4
    This is like asking: "I need a trigger to be able to xxx, Here is what I have so far trigger my trigger on Account(before insert){} now I am lost, please help....This site deals with specific issues not writing code for you. Where are you stuck. To Directly answer your question yes you are on the right track but there is so much to consider....
    – Eric
    Jul 17 '15 at 14:32
  • You're going to need to provide more than three lines of code for us to be able to guide you. You also need to pay more attention to the code you're writing; you've misspelt Implementation__c as Implementatiom__c and you've got the new List<Implementation__c>(); syntax wrong.
    – Rob
    Jul 17 '15 at 14:33
  • 1
    You will also need to pay attention to your requirements which state "After creating a new opportunity", which would suggest your trigger is on opportunity not Implementation
    – BarCotter
    Jul 17 '15 at 14:35
  • 1
    I do agree with Eric and SB...your requirements are also a little difficult to understand. When you say "Object Implementation should populate a list of 12 Records" what exactly do you mean? It also sounds like you want the trigger on Opportunity (so that it's fired when an Opportunity is created), not on Implementation__c Jul 17 '15 at 14:35
  • 3
    SMH: Nowhere in your code you posted in the text Opportunity__c - Look at all these people wanting to help you and you fail to take any direction to be specific. How can we debug code you do not even post??? Mindreader's.. And could it need to be Opportunity and Not Opportunity__c? Only custom objects end with __c
    – Eric
    Jul 17 '15 at 14:44
2

Along with all the comments on you OP, a basic trigger to create multiple contacts for Account

trigger createContacts on Account(After Insert){

Contact[] c = New Contact[]{};

for(Account a : trigger.new){
 for(integer x=0;x<10;x++){
    c.add(New Contact(AccountID = a.id, LastName="LastName ' + x));
 }
}

insert c;

}

Per your comment:

  1. You forgot the () after instantiating the List
  2. _c should be __c
  3. DO NOT put Inserts inside a for loop. Populate the list then Insert
  4. You have a catch but no try
  5. No need to use a try catch for dml (unless use case requires it, use allornone = false and evaluate

    trigger IMP on Opportunity (after insert) { 
     List <implementation__c> ImpToInsert = new List <implementation__c>(); 
    
     for (Opportunity o : Trigger.new) {
      implementation__c rec = new implementation__c (Opportunity__c = o.id); 
      ImpToInsert.add(rec);
    }
    
    database.saveResult[] sr = database.insert(impToInsert,false);  
    
    for(integer x=0;x<sr.size();x++){
      if(sr[x].isSuccess() == false)
          system.debug(logginglevel.error,sr[x].getErrors()[0].getMessage());
     }
    }
    
9
  • Error: Compile Error: unexpected token: 'for' at line 3 column 0 trigger IMP on Opportunity (after insert) { List <implementation_c> ImpToInsert = new List <implementation__c> for (Opportunity o : Trigger.new) { implementation_c = new implementation_c (); insert impToInsert; } catch (system.Dmlexception e) { system.debug (e); } }
    – user22024
    Jul 17 '15 at 15:46
  • @SBeauvais34 - See updated answer.
    – Eric
    Jul 17 '15 at 15:52
  • Thank you. I am getting the following error with the updated code. Error: Compile Error: unexpected token: 'for' at line 4 column 1
    – user22024
    Jul 17 '15 at 15:59
  • @SBeauvais34 - Sorry I forgot to correct my point #1 in your code
    – Eric
    Jul 17 '15 at 16:00
  • Once you get it, you should consider compartmentalizing the code into a class and have the trigger simple call the code with the records list. Doing so may allow you to reuse the code in other classes that may need to do the same thing. But that is another topic
    – Eric
    Jul 17 '15 at 16:01