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
AndySFAndySF 

First Trigger - Help Please

Hi all,

 

I'm trying to write my first trigger and am running into some errors.  It is probably baby stuff, but we all have to start somewhere.  Can someone please help with the error?

 

I'm trying to pull a field (text) from the Associated Account to a separate field (picklist) on the Opportunity.

 

Thanks for the help/lesson in advance!

 

 

trigger setXCenter on Opportunity (before insert, before update) { for(Opportunity oppObject : trigger.new) { Account acctObject = [Select Default_X_Center__c from Account where id = :oppObj.Account]; oppObject.Cost_Center__c = acctObject.Default_X_Center__c; }

 I'm getting this error:

 

 Error: Compile Error: No such column 'Default_X_Center__c' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 19 column 29

 

Thanks 

 

EtienneCoutantEtienneCoutant

Hi,

 

You might be able to get Default_X_Center__c without querying the Account.

 

 

trigger setXCenter on Opportunity (before insert, before update) { for(Opportunity oppObject : trigger.new) { oppObject.Cost_Center__c = oppObject.Account.Default_X_Center__c; } }

 

 

 

ascuccimarraascuccimarra

Yep. Couple of things, you should never do queries inside a for loop. And in this case, like Ettiene said you don't need the query. The error you were getting is probably because you don't have a custom field called Default_X_Center__c in Account. Check in the field setup if that's the api name.

Hope this helps.