I am new to Salesforce . I am having a bit of problem wriiting a trigger, any help would be appriciated.
I want to Write a trigger that will set the name of an Opportunity record when it is created and when the fields below are updated. The opportunity name should contain the following, each separated by a space: Account Name,Owner Last Name, Opportunity Close Date.
trigger SetOpportunityName on Opportunity (before insert, before update) {
for (Opportunity op: Trigger.new) {
op.Name = op.Account.Name+' '+Op.Owner.LastName+' '+op.ClosedDate;
system.debug('*****************************' + op.Name);
}
But I am getting op.Account.Name and Op.Owner.LastName values as null.
I know there is a mistake, Please guide me.
Best Answer
You need to query for those values since they don't come down natively in the trigger. Anything that is related will be
null
(likeop.Account.Name
)So I would gather up all the account and OwnerIds and then do separate queries outside of the for loop (otherwise you will have other issues), and then loop again to set the values... Also the owner could be a group so this may be tricky :-P