You are trying to using a BEFORE
trigger, and in your trigger, you are trying to do a DML call on records that are in the trigger. In BEFORE
triggers, any manipulation you do to the data happens before the records undergo their DML call, so you don't need to perform a DML call on the trigger records -- it's already gonna happen. However, you would need to do a DML call on AFTER
triggers. They happen after the trigger, so anything you do AFTER
the trigger needs to be "saved" via a DML call.
So, for your case, just remove the upsert rows;
and it should work.
To note, I usually use BEFORE
triggers if I need alter data that is being fed into the trigger while I use AFTER
triggers if I need to alter data that is related to the data being fed into the trigger.
BEFORE
Example:
trigger MyCustomObj_beforeTrigger on MyCustomObj__c (before update){
for(MyCustomObj__c m:trigger.new){
//do something to the trigger data
}
}
AFTER
Example:
trigger MyCustomObj_afterTrigger on MyCustomObj__c (afterupdate){
list<myChildObj__c> newChildrenList = new list<myChildObj__c>();
for(MyCustomObj__c m:trigger.new){
newChildrenList.add(new myChildObj__c());
}
insert newChildrenList;
}
Also, one bit of advice on triggers: write all the code you need for your trigger in its own class, and then just call the class in the trigger (and make sure you use all the trigger goodies when you can -- trigger.new
, trigger.old
, trigger.newMap
, trigger.oldMap
, trigger.isInsert
, trigger.isUpdate
, trigger.isDelete
).
It's good to do this to keep your triggers as short and simple as possible. Then, I also recommend that you have only 1 trigger per custom object (so you can easily tell what exactly is being triggered in any situation). And if you need to do separate things in your trigger, just call the classes as needed.
Example:
Class code:
public class trigger_CreateNewChildren(){
public static void CreateNewChildren(map<id,MyCustomObj__c>newMap){
list<MyChildrenObj__c> newChildren = new list<MyChildrenObj__c>();
for(MyCustomObj__c m:newMap.values()){
newChildren.add(new MyChilerenObj__c());
}
insert newChildren;
}//END CreateNewChildren
public static void fieldChangeCheck(map<id,MyCustomObj__c> newMap,map<id,MyCustomObj__c>oldMap){
for(id m:newMap.keyset()){
if(newMap.get(m).field_1__c!=oldMap.get(m).field_1__c)
//do something
}
}//ENDfieldChangeCheck
Trigger code:
Trigger MyCustomObj on MyCustomObj__c(AFTER INSERT,BEFORE UPDATE){
if(trigger.isInsert && trigger.isAfter){
trigger_CreateNewChildren.CreateNewChildren(trigger.newMap);
}
if(trigger.isUpdate && trigger.isBefore){
trigger_CreateNewChildren.fieldChangeCheck(trigger.newMap,trigger.oldMap);
}
}
You could write you trigger logic like this:
trigger UpdateStageOnOpportunity on Account (after update) {
set<Id>AccId = new set<Id>();
for(Account Acc : trigger.new){
if(Acc.Id != null && Acc.MyLightningP__Payment_Info_on_File__c == true){
AccId.add(Acc.Id);
}
}
map<Id,Opportunity>OppMap = new map<Id,Opportunity>([select Id, name, StageName
from Opportunity
where AccountId in: AccId]);
for(Account Acc : trigger.new){
if(OppMap.Containskey(Acc.Id)){
OppMap.get(Acc.Id).StageName='Closed Won';
}
}
update OppMap.values();
}
Best Answer
Building on Eric's answer, the code can be cut down to this: