You need to sign in to do that
Don't have an account?
Trigger to set Opportunity Name is not working, need help please!
I trying to create an Opportunity trigger that sets the name of a new Opportunity to "AccountName - AccountCity, AccountState" for example ("Test Corp. - San Franciso, CA").
What's difficult is that I have a custom checkbox field on the Opportunity named "Same as Mailing Address?". When this checkbox is checked, it sets the custom Opportunity address fields "Project Address", "Project City", "Project State" and "Project Zip Code" to the Account's custom fields "Mailing Address", "Mailing City", "Mailing State", "Mailing Zip Code" via process builder. But the Opportunity name doesn't update right away.
The goal is to have the user create an Opportunity, check the box, hit Save, and then The opportunity name is automatically displayed correctly.
However, what happens is that only the Account name shows up as the Opportunity name while the Project Address fields are correctly filled out. Then after one edit of any kind, the name changes to the correct format.
I want it to show the correct naming convention after the user hits Save for the first time.
Screenshots of Opportunity create screen:
After Save, this is what displays in the Opportunity record:
Then after any update, it changes to the correct name:
Here's my trigger I'm working on that doesn't work.
trigger NameOpportunity on Opportunity (before insert) { Set<Id> AccountIds = new Set<Id>(); // Initialize a list of Ids String accName; // Initialize account name variable String newOppName; // Initialize final opportunity name variable // Add an account ID to the list for every opportunity caught by trigger for (Opportunity op : Trigger.new) { AccountIds.add(op.AccountId); } // Create a map of Ids and account, querying the name from account records whose Id is in the AccountIds list generated earlier Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :AccountIds]); System.debug(accountMap); // Update the Opportunity name for every opportunity entering the trigger that has and Account, and "Same as Mailing Address" checked for (Opportunity myOpp : Trigger.new) { if (myOpp.AccountId != null && myOpp.Same_as_Mailing_Address__c == true) { accName = accountMap.get(myOpp.AccountId).Name; // set naming conventions newOppName = accountMap.get(myOpp.AccountId).Name + ' - ' + myOpp.Project_City__c + ', ' + myOpp.Project_State__c; myOpp.Name = newOppName; } } }
Any help is greatly appreciated thank you!
I decided to remove the process from process builder and add all of the logic to a trigger. Everything works now. Thee issue with the trigger and test class was the following section, I just removed "op.Same_as_Mailing_Address__c = true" from the criteria because the process in process builder is deleted:
This is what it is now:
All Answers
Please read Order of execution in salesforce that trigger executes first and then process builder.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
So for update the name of Opportunity follow this code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Thank you Ajay, I used your code revision and my test class assertion still doesn't pass. Do you think I should remove the process from process builder and instead add it to this trigger?
Also, here's my test class:
I also made one adjustment to your revision. The address fields from the account are actually called "Mailing City" and "Mailing State" instead of "Project City" and "Project State". So my trigger now looks like this:
So, I've decided to remove the naming process from Process Builder and instead have the trigger do all of the naming. My second test class method assertion is failing, but the first one is passing. Does anyone know why it's failing?
Here's my trigger:
And here's my test class:
I decided to remove the process from process builder and add all of the logic to a trigger. Everything works now. Thee issue with the trigger and test class was the following section, I just removed "op.Same_as_Mailing_Address__c = true" from the criteria because the process in process builder is deleted:
This is what it is now: