Find your content:

Search form

You are here

Initial term of field expression must be a concrete SObject: String

 
Share

I'm trying to populate a field in every opportunity that is created that has a value based on the account that it's a part of. Both the account field and the opportunity field are picklists, as there are circumstances where a manager may need to modify one of these fields in the future. My trigger only references the insert, not update, because there are also circumstances where the account field could change but we want the opportunity field to stay the same. So with all of this in mind, I'm getting the error specified in the subject line.

trigger BusinessDivisionOpportunity on Opportunity (before insert) 
{    
    Map<String, String> myPickListMap = Constants.businessDivisionMap;

    for (Opportunity o : Trigger.new)
    {
            o.Business_Division__c = myPickListMap.get(o.AccountId).Business_Division__c;
    } 

}

The map in this instance is calling a map that we have defined in a separate class. The error is being thrown on the final line of code. I'm not really sure how else to write this trigger without this error being thrown, however.

Thanks in advance for any help you all can give!


Attribution to: grimsvotn

Possible Suggestion/Solution #1

The error message indicates that you attempted to use a 'dot' to retrieve a value that doesn't exist.

trigger BusinessDivisionOpportunity on Opportunity (before insert) 
{    
    Map<String, String> myPickListMap = Constants.businessDivisionMap;

    for (Opportunity o : Trigger.new)
    {
            // the .get() from the map will retrieve the value associated with the key
            // which is your accountId. The value returned (based on the way your map 
            // is defined (<string, string>) will be a string value.
            // So, your code is attempting to retrieve a field named Business_Division__c
            // from a string, which can't be done.
            // o.Business_Division__c = myPickListMap.get(o.AccountId).Business_Division__c;

            // try this - it presumes that the value in the map is the "business division"
            o.Business_Division__c = myPickListMap.get(o.AccountId);
    } 

}

Update

It seems that what you're trying to accomplish is to just copy some data from the parent Account onto the Opportunity. This can most easily and simply be accomplished using a workflow rule that executes on new Opportunity records and performs a field update on the record using the relationship to the parent Account.

Unless there's some complicated logic in the Constants class for determination of those business division values, a workflow field update might be a far simpler solution. Edit: It's not simpler in this case. A second answer added with caveats.

Alternatively, if this is the scenario (copy the field data from the parent account) that you are trying to accomplish must be done via trigger - my implementation would begin like this:

trigger BusinessDivisionOpportunity on Opportunity (before insert) {

    // collect the account ID values from these opportunities
    Set<Id> relatedAccountIdSet = new Set<Id>();

    for (Opportunity o : Trigger.new) {
        if (o.AccountId != null) {
            relatedAccountIdSet.add(o.AccountId);
        }
    }

    // query the Account details
    Map<Id, Account> relatedAccountsMap = new Map<Id, Account>([SELECT Id
                                                                    , Business_Division__c
                                                                FROM Account
                                                                WHERE Id IN :relatedAccountIdSet]);

    // set the business division on the opportunity based on what's in the field on the account
    for (Opportunity o : Trigger.new) {

        if (o.AccountId == null) {
            continue; // skip to the next opportunity in the list
        }

        Account parentAccount = relatedAccountsMap.get(o.AccountId);
        o.Business_Division__c = parentAccount.Business_Division__c;
    } 

}

Attribution to: Mark Pond

Possible Suggestion/Solution #2

Your problem is here:

myPickListMap.get(o.AccountId)

this returns a String and it looks like here you want it to be an Account in order to fetch the value of the account's business_division__c

Your map needs to be of type <ID,Account> and you'll need to populate this map with the values of the Oppo's Accounts as in:

Set<ID> aIdSet = new Set<ID> ();
for (Opportunity o : Trigger.new)
  if (o.accountId != null) aIdSet.add(o.accountId);
Map<ID,Account> aIdToAccountMap = 
       new Map<ID,Account> ([select id, business_division__c from Account 
                                where id IN :aIdSet]);

for (Opportunity o : Trigger.new)
   o.Business_Division__c = o.AccountId != null 
         ? aIdToAccountMap.get(o.AccountId).Business_Division__c 
         : null;

Attribution to: cropredy

Possible Suggestion/Solution #3

Change

o.Business_Division__c = myPickListMap.get(o.AccountId).Business_Division__c;

to

o.Business_Division__c = myPickListMap.get(o.AccountId);

Your map is a Map of Strings, and you are treating it as a Map of Objects.


Attribution to: dphil

Possible Suggestion/Solution #4

Creating a separate answer for the idea of using a workflow field update.

Huge caveat: a field update action targeting a picklist field does not allow for the copying of a value from another object into that field. In this example, the Business_Division__c field on the Opportunity is a Text(255) type and the field on the Account object is a single-select picklist. A multi-select picklist would show comma delimited values on the Opportunity.

While implementing this I also added a formula field to the Opportunity to reference the value in the parent Account Business_Division__c field as another possible option : TEXT(Account.Business_Division__c)

My picklist values on the account are [North, South, East, West] - the parent account is an East division.

Workflow Rule:

enter image description here

Field Update:

enter image description here

Before Save:

enter image description here

After Save:

enter image description here


Attribution to: Mark Pond
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33504

My Block Status

My Block Content