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
Synthia B.Synthia B. 

Apex Trigger: Insert Record in a custom object (related list)

I am not sure what is incorrect in this trigger. I am getting Error: Compile Error: unexpected token: 'for' at line 4 column 1

Scenario:
- After creating a new Opportunity
- Object Implementation should populate a list of 12 Records when save is clicked. 
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());
 }
}
Best Answer chosen by Synthia B.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
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 (implementation__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());
 }
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
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());
 }
}

Please let us know if this will help you.

Thanks,
Amit Chaudhary
 
Synthia B.Synthia B.
Hello Amit, 

I am getting the following error with the corrected code. 

Error: Compile Error: Invalid field Opportunity__c for SObject Implementation__c at line 6 column 68


 
Synthia B.Synthia B.
I am not sure what you mean Amit. Am I writing the trigger in the wrong place? User-added imageUser-added image
Synthia B.Synthia B.
User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
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 (implementation__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());
 }
}

 
This was selected as the best answer
Synthia B.Synthia B.
Hi Amit, 

Your last code worked however, after creating a new opportunity, the implementation object pulls one record type and it is an id. 
The records types are listed in the implementation object. 



User-added imageUser-added image
yvk431yvk431
The issue is with the api name you have created for opportunity on Impementation object, ideally it should Opportunity__c which you are using in the code, but you have named it as Implementation__c and hence getting the compiler error. What AMit mentioned is correct.

--yvk 
Synthia B.Synthia B.
Thank you Amit