You need to sign in to do that
Don't have an account?
AlecS
Convert Opp Account Name to a String Value
Using trigger, I am trying to create a renewal opportunity the moment a new opportunity is closed won. However, each Opportunity name is unique. The way I make them unique is by giving the name as follows:
Opportunity Name = [Account Name - <contract start year> to <contract end year>]
Example: Abc Corp - 2012 - 2013
I am having a tough time copying the Account Name to a string value. If I try to read the string value of the Account name, I get the 15 digit ID.
How can I get the Account name, like "Abc Corp".
Thanks!
In triggers, You only get all the values of fields of object in Trigger.new. So for fetching reference field value, you need to perform a query.
// If you try to fetch o.Account.Name with iteration over Trigger.new then you will get it as null. So use a query!!
Modified your trigger as below.
All Answers
If you can paste your code snippet, it would be great help for me to help you ;)
Ok great. If you can tell me where is Contract Start and End date is (is it Opportunity custom field or are you taking it from Contract object), then I can give you code snippet which I think should work.
OR... wait...
Why don't you use workflow field update? No need of trigger for your requirement. Moreover the trigger you provided will not work, as you are trying to update the same opportunity which is locked (after update). Just use workflow rule where you will check whether stage was changed and its value is 'Closed Won' - if yes you use the field update to change the name to Account Name - <Contract Start Date> <Contract End Date>
But workflow won't create a new opportunity. When an opp is closed won, I want to create a duplicate opp with a different opp name. Thanks.
Ohh I completely misread the post. Apologies.
Are Contract dates present in Opportunity object?
Try,
opp.Name = o.Account.Name;
Does this work?
In triggers, You only get all the values of fields of object in Trigger.new. So for fetching reference field value, you need to perform a query.
// If you try to fetch o.Account.Name with iteration over Trigger.new then you will get it as null. So use a query!!
Modified your trigger as below.
Yeah abhi, you are correct.
I didn't concentrate on checking if the field is changed or not, I just modified the code to populate Account Name.
Thanks for the correction!!
Thanks, Rahul. This works!!!
You are right, Abhi. The previous code will create new opp without checking for old value. Thanks for your help too!
forgot to add. You just need to put "And" after the first Where clause.