1

I am working on a trigger that will overwrite the opportunity name with the account name plus some generic text (e.g. 'Acme, Inc. [New Opportunity]'). This functionality can be handled by a workflow with a field update, but the trigger(s) will grow in complexity with additional logic for opportunity products.

I've borrowed and have applied examples from the posts Map of Accounts indexed by Opportunity Id and How to Map Opportunity Owner to Account Owner. The challenge I am facing is that the output is not what I expected. Instead of 'Acme, Inc. [New Opportunity]' I get the following from debug:

(Account:{Name=Acme, Inc., Id=001F00000174p9pIAA} [New Opportunity], Account:{Name=Zenith, LLC., Id=001F00000174p9oIAA} [New Opportunity])

Here is the code that I've been working (will be converted to trigger):

>     Set<Id> opptyIdSet = new Set<Id>(); 

    opptyIdSet.add('006F000000SdrzX');
    opptyIdSet.add('006F000000TH8vS');

    // Map to pair Oppty Id to Account
    Map<Id,Account> mapOpptyAcct = new Map<Id,Account>();

    // Map to gather opptys in scope
    Map<Id,Opportunity> mapOpptys = new Map<Id,Opportunity>([SELECT Id,AccountId FROM Opportunity WHERE Id IN: opptyIdSet]);

    // Collect set of Account Ids
    Set<Id> accountIdSet = new Set<Id>();
    for(Opportunity op : mapOpptys.values()){
        accountIdSet.add(op.AccountId);
    }

    // Map the Account Ids to Account Names
    Map<Id,Account> mapAccounts = new Map<Id,Account>([SELECT Id,Name FROM Account WHERE Id IN :accountIdSet]);

    // Put them together
    for(Id opId : mapOpptys.keySet()){
        Id acctId = mapOpptys.get(opId).AccountId;
        mapOpptyAcct.put(opId,mapAccounts.get(acctId));
    }

    // variable to hold concatenated Oppty Name
    String AccountName;
    String newInquiry = ' [New Opportunity]';
    String newOpptyName;

    // Map to hold Id and final Oppty Name 
    Map<Id, String> mapFinal = new Map<id, String>();

    // Iterate over Oppty Accoutn map and put in new Oppty Name
    for(Id opId : mapOpptyAcct.keySet()) {
        AccountName = String.valueOf(mapOpptyAcct.get(opId));
        newOpptyName = AccountName + ' [New Opportunity]';
        mapFinal.put(opId,newOpptyName);
    }

    // Test Concatenation
    System.debug('AccountName: ' + mapFinal.values());

How does one get to a clean output of the account name?

2

In this line:

mapOpptyAcct.put(opId,mapAccounts.get(acctId));

You're putting the whole account record into that map, indexed by opportunity ID.

Then in this line:

AccountName = String.valueOf(mapOpptyAcct.get(opId));

You're getting that account back, and converting the whole thing to a String variable which is why you're getting all of the rest. Instead you want to just reference the name field:

AccountName = mapOpptyAcct.get(opId).Name;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.