-1

Hi I tired creating below trigger but it is giving me this error:

Loop variable must be of type CSM_Milestone__c

trigger CSMMilestone on CSM_Milestone__c (After insert, After update) {

     List<Opportunity> oppList = [select Id from Opportunity where StageName = 'Closed: Won']; 

     list<CSM_Milestone__c> csmList = new list<CSM_Milestone__c>();
     for(Opportunity o : trigger.new)
     {
        CSM_Milestone__c csm = new CSM_Milestone__c ();
       // quote.Opportunity = o.id;
        csm.Name = 'CSM Milestone1'; 
        csmList.add(csm);
        if (o.StageName == 'Closed: Won'){
           for(Opportunity act: oppList){
              csm = new CSM_Milestone__c(Name='testName');
             // quote.Opportunity__c = o.Id;
              csmList.add(csm);
           }
        }     
     }
     insert csmList;    
}

1 Answer 1

0

This is sample code to create object in opportunity. In for loop you can give your logic to create your records same like opportunity

trigger CSMMilestone on Opportunity(After insert, After update) {

list<Opportunity> csmList = new list<Opportunity>();
 for(Opportunity o : trigger.new)
 {
    // if you want to create record in opportunity
    if (o.StageName == 'Closed: Won'){        
    Opportunity csm = new Opportunity();
     csm.Name = 'CSM Milestone1'; 
    csmList.add(csm);
    }     
 }
 insert csmList;    
 }
5
  • Will this work for if I have Parent object as Opportunity and child Object as CSM Whenever Opportunity stage is closed won , It will auto create multiple csm child records like name = a1, b2,c3,d4 etc
    – Sai
    Commented Jan 27, 2017 at 18:23
  • I am getting same error Loop variable must be of type CSM_Milestone__c
    – Sai
    Commented Jan 27, 2017 at 18:25
  • Trigger cmsMilesrone on csm_milestone__c(after ...) And in for loop for (csm_mileatone m : trigger.new) ... try ths..That should work Commented Jan 27, 2017 at 18:52
  • Sorry for formatting i am using mobile right now Commented Jan 27, 2017 at 18:52
  • Thank you for a quick response. I really appreciate it. But still I am getting same error. Please help
    – Sai
    Commented Jan 27, 2017 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .