1

I want to update 2 fields on a Product record when a field is populated. The values I want to pull in are from another object so I believe the correct way is to get a filtered list of records from the new map. Do a query on the custom object and return records that match.

When I try to save the class I am working in I get an error:

Initial term of field expression must be a concrete SObject: List (Line: 15, Column: 29)

I know my initial list is for Product2 then I want to compare records in the Accounting_Unit__c object to the product2 records. Is there a right way to do this?

private void updatePA(Map<Id, Product2> newMap, Map<Id, Product2> oldMap)
{
    List<Product2> prodsToUpdate = new List<Product2>();
    for(Product2 prod : newMap.values())
    {
        if(prod.Product_LOB__c != null)
        prodsToUpdate.add(prod);
    }

    List<Accounting_Unit__c> aUnits = [SELECT   Id, 
                                                    Name, 
                                                    Practice_Area__c,
                                                    Sector__c
                                           FROM Accounting_Unit__c
                                           WHERE Name = :prodsToUpdate.Product_LOB__c
                                           LIMIT 1];

    for(Product2 prodUpdate : prodsToUpdate())
    {
        prodsToUpdate.Product_BU__c = aUnits.Practice_Area__c;
        prodsToUpdate.Product_Sector__c = aUnits.Sector__c;
    }
}

2 Answers 2

1

The List<Product2> type does not have any property named Product_LOB__c. In fact, it appears you mean to use a List<String>. You have a nearly identical error in your final for loop with your reference to aUnits.

You have quite a few issues, so it will be easier to provide an annotated alternative to your code:

void updatePA(/*same parameters*/)
{
    List<String> prodsToUpdate = new List<String>(); // change type
    for (Product2 product : newMap.values())
    {
        if (prod.Product_LOB__c != null)
        {
            prodsToUpdate.add(product.Product_LOB__c); // add a String instead
        }
    }

    Map<String, Accounting_Unit__c> units = new Map<String, Accounting_Unit__c>();
    for (Accounting_Unit__c unit : [
        SELECT ... FROM Accounting_Unit__c
        WHERE Name IN :prodsToUpdate // remove field reference
        // remove limit
    ]) units.put(unit.Name, unit); // map by name for later retrieval

    for (Product2 product : newMap.values())
    {
        // loop through the actual Product2 records
        // not the field values

        Accounting_Unit__c unit = units.get(product.Product_LOB__c);
        // get the unit with the same name

        if (unit != null)
        {
            // set product fields based on the corresponding unit
        }
    }
}
2
  • The code compiles and saves but it does not update the record. From your example we created a list<string> and then we did a for loop on the units where the name is in the string. But we never added anything to the list. Is that an error? Sep 22, 2016 at 18:15
  • I misread your initial code and thought you could reuse that loop for some reason.
    – Adrian Larson
    Sep 22, 2016 at 18:17
1

prodsToUpdate Is a list you can't refer them direct in query. Change your code store Product_LOB__c in a set and use that in query.

List<Product2> prodsToUpdate = new List<Product2>();
Set<string> nameSet = new Set<string>();
    for(Product2 prod : newMap.values())
    {

        if(prod.Product_LOB__c != null) {
                nameSet.add(prod.Product_LOB__c );
          prodsToUpdate.add(prod);

      }
    }

    List<Accounting_Unit__c> aUnits = [SELECT   Id, 
                                                    Name, 
                                                    Practice_Area__c,
                                                    Sector__c
                                           FROM Accounting_Unit__c
                                           WHERE Name IN :nameSet
                                           LIMIT 1];
2
  • Two answers intentional?
    – Adrian Larson
    Sep 22, 2016 at 17:28
  • 1
    @AdrianLarson I didn't realise I have posted two answer. Thanks :) Sep 22, 2016 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .