You need to sign in to do that
Don't have an account?

SELF-REFERENCE Error on Trigger
I'm trying to create a trigger on Opportunities that will create a new account when a box on the opportunity is checked, then update a lookup field on that same opportunity with the newly created account. I'm getting a self-reference error.
Here is the trigger:
trigger
CreateHybridAcct onOpportunity (beforeupdate) {
for
(Opportunity p : [SELECT ID, ACCOUNT.Name, ACCOUNT.ID, ACCOUNT.Client_Name__c, Create_Hybrid__c, Ownerid
FROM OPPORTUNITY WHERE ID in :Trigger.new]){
if(p.create_hybrid__c == True) {
Account h = newAccount(
name =
'testhybrid',
agency_id__c =
'300',
cmr_number__c =
'997',
client_name__c = p.account.name,
hybrid_account__c = p.account.id,
//owner = p.owner,
lead_converted_date__c = date.
today()
//recordtypeid = '01270000000UHZcAAO',//parentid = '0017000000PgOvrAAF'
);
inserth;
p.hybrid_account__c = h.id;
p.create_hybrid__c =
false;
updatep;
}
}
}
Here's the error message:
Review all error messages below to correct your data.
Apex trigger CreateHybridAcct caused an unexpected exception, contact your administrator: CreateHybridAcct: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0067000000LJK3FAAX; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0067000000LJK3F) is currently in trigger CreateHybridAcct, therefore it cannot recursively update itself: []: Trigger.CreateHybridAcct: line 25, column 5
1) You don't need to perform an update (update p; ) on the Opportunity. The trigger is running just before the opportunity is about to be written to the database. Any changes to make will be saved.
2) You don't need the SELECT statement. All the fields you need are already in the Trigger.new array.
3) You need to create a list of Accounts that you are creating. You don't want to insert each one individually. Build them in code, add them to the list, then insert the whole list in one statement. Finally loop through and add the IDs to the Opportunity.
All Answers
1) You don't need to perform an update (update p; ) on the Opportunity. The trigger is running just before the opportunity is about to be written to the database. Any changes to make will be saved.
2) You don't need the SELECT statement. All the fields you need are already in the Trigger.new array.
3) You need to create a list of Accounts that you are creating. You don't want to insert each one individually. Build them in code, add them to the list, then insert the whole list in one statement. Finally loop through and add the IDs to the Opportunity.
Thanks a lot!! That actually solved my self-reference problem.
Now I have one more issue - I'm trying to set the owner of the newly created account to the owner of the opportunity and I'm getting a new error. I know why I'm getting the error, just not sure how to solve it.
Trigger:
trigger
CreateHybridAcct onOpportunity (beforeupdate) {
for
(Opportunity p : Trigger.new){
if(p.create_hybrid__c == True) {
Account h = newAccount(
name =
'testhybrid',
agency_id__c =
'300',
cmr_number__c =
'997',
client_name__c = p.account.name,
hybrid_account__c = p.account.id,
owner = P.OwnerId,
lead_converted_date__c = date.
today()
//recordtypeid = '01270000000UHZcAAO',//parentid = '0017000000PgOvrAAF'
);
inserth;
p.hybrid_account__c = h.id;
p.create_hybrid__c =
false;
}
}
}
Error:
Save error: Invalid initial expression type for field Owner, expecting: SOBJECT:User (or single row query result of that type)
Any ideas how to assign the owner??
Thanks again!
Try:
OwnerId = P.OwnerId,
instead of :
owner = P.OwnerId,
Oh wow, that was too easy! I kept trying owner or owner.id.
That worked great!!! Thank you!!