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
AlecSAlecS 

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!

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

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.

 

Trigger triggername on opportunity(after update){

    List<Opportunity> oppList = new List<Opportunity>();
    
    // Add filter in query instead of using additional if statement. It is a better way of filtering records.
    // Add all the fields of Opportunity or its parent which you want to use.
    for(Opportunity o : [Select Name, Id, Account.Name, AccountId, closedate from Opportunity where Id IN: trigger.new 
                         and StageName = 'Closed Won']){ 
        Opportunity opp = new Opportunity();
    
        // Fetching Account name field which is retrieved from query..
        opp.Name = o.Account.Name;
        opp.AccountId = o.AccountId;
        opp.closedate = o.closedate;
        opp.stageName = 'Renewal - Neutral';
        oppList.add(opp);
    }
    if(oppList.size()>0){
        insert oppList;
    }
}

 

All Answers

goabhigogoabhigo

If you can paste your code snippet, it would be great help for me to help you ;)

AlecSAlecS
Here's the code. I want "opp.Name = Account Name". Greatly appreciate your help!

Trigger triggername on opportunity(after update){ List<Opportunity> oppList = new List<Opportunity>(); for(Opportunity o : trigger.new){ if(o.stageName=='Closed Won'){ Opportunity opp = new Opportunity(); //this is where I am trying to get the name of the account and not the 15 digit id //opp.Name = o.Name; opp.Name = String.valueof(o.AccountId); opp.AccountId = o.AccountId; opp.closedate = o.closedate; opp.stageName = 'Renewal - Neutral'; oppList.add(opp); } } if(oppList.size()>0) insert oppList; }

 

goabhigogoabhigo

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>

AlecSAlecS

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.

goabhigogoabhigo

Ohh I completely misread the post. Apologies. 

 

Are Contract dates present in Opportunity object?

goabhigogoabhigo

Try,

 

opp.Name = o.Account.Name;

 

Does this work?

Rahul SharmaRahul Sharma

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.

 

Trigger triggername on opportunity(after update){

    List<Opportunity> oppList = new List<Opportunity>();
    
    // Add filter in query instead of using additional if statement. It is a better way of filtering records.
    // Add all the fields of Opportunity or its parent which you want to use.
    for(Opportunity o : [Select Name, Id, Account.Name, AccountId, closedate from Opportunity where Id IN: trigger.new 
                         and StageName = 'Closed Won']){ 
        Opportunity opp = new Opportunity();
    
        // Fetching Account name field which is retrieved from query..
        opp.Name = o.Account.Name;
        opp.AccountId = o.AccountId;
        opp.closedate = o.closedate;
        opp.stageName = 'Renewal - Neutral';
        oppList.add(opp);
    }
    if(oppList.size()>0){
        insert oppList;
    }
}

 

This was selected as the best answer
goabhigogoabhigo
Rahul, you are bang on target mate. I did not realize it when I suggested. But there is some problem with the trigger code you written - it will insert new Opportunities every time a Closed Won opportunity is edited (not necessarily updating any field, but simply clicking on Edit and Save). Hence we will have to make sure that new Opportunity should be inserted ONLY when Opportunity Stage is changed. AlecS, try this: trigger YourTriggerName on Opportunity(after update){ List oppList = new List(); for(Opportunity o : [Select Name, Id, Account.Name, AccountId, Closedate,StageName from Opportunity where Id IN: trigger.new AND StageName = 'Closed Won']){ if(o.StageName != Trigger.oldMap.get(o.Id).StageName) { // testing old value of stage vs current value which is = ISCHANGED() Opportunity opp = new Opportunity(); opp.Name = o.Account.Name + Straing.valueOf(YourDateFiledsHere); opp.AccountId = o.AccountId; opp.closedate = o.closedate; opp.stageName = 'Renewal - Neutral'; oppList.add(opp); } } if(oppList.size()>0) insert oppList; } I do not have option to paste the code in a code block, looks like there is an issue with Discussion board.
goabhigogoabhigo
There is some serious problema with editing post. I will modify as soon as I have options..
Rahul SharmaRahul Sharma

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!!

AlecSAlecS

Thanks, Rahul. This works!!!

AlecSAlecS

You are right, Abhi. The previous code will create new opp without checking for old value. Thanks for your help too!

AlecSAlecS

forgot to add. You just need to put "And" after the first Where clause.

 

elect Name, Id, Account.Name, AccountId, closedate from Opportunity where Id IN: trigger.new 
                         AND StageName = 'Closed Won'