Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Can anybody help me with this script, this is a simple update when the trigger is fired, (it works in sandbox) What can I use instead of items.values().PO_Details_Ordered__c = ... I am pretty sure this approach is causing this error. I will appreciate any help!

I tried this:

  items.get(result.get('p')).PO_Details_Ordered__c = Double.valueof(result.get('ordered'));

I got this error:

Error: Compile Error: Incompatible key type Object for MAP at line 13 column 8

Trigger on the object:

 trigger PODetailsCountsONProducts on NRProducts__c(before update) {

     PODetailsCountsONProducts PODetailsCountsONProducts = new PODetailsCountsONProducts();

     if (Trigger.isUpdate) {
         PODetailsCountsONProducts.Run(Trigger.newMap);
     }
 }

Class:

System debug for items: USER_DEBUG [6]|DEBUG|{a0Cf0000002WxGMEA0=NRProducts__c:{all the fields for the NRProduct record......}}

public with sharing class PODetailsCountsONProducts {

    public void Run(Map <Id, NRProducts__c> items) {

        for (AggregateResult result: [SELECT NRProducts__c p
                                        , SUM(Qty_Ordered__c) ordered
                                        , SUM(Qty_Received__c) received
                                        , MIN(Unit_Price__c) unitPrice
                                      FROM Purchase_Order_Details__c
                                      WHERE PO_Status__c = 'Placed'
                                        AND Purchase_Order__r.Financial_Ownership_Date__c != NULL
                                        AND NRProducts__c IN :items.keyset()
                                      GROUP BY NRProducts__c
        ]) {


            // here is the problem

            items.values().PO_Details_Ordered__c = Double.valueof(result.get('ordered'));
            items.values().PO_Details_Received__c = Double.valueof(result.get('received'));
            items.values().Min_Price__c = Double.valueof(result.get('unitPrice'));
        }
    }
}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

Your attempt was close. The key to items is an Id, so you need to make sure that you are passing in an Id type to the get method of the Map. Your attempt was passing in an Object.

You had the following:

items.get(result.get('p')).PO_Details_Ordered__c = Double.valueof(result.get('ordered'));

The result.get('p') returns an Object. You need to cast it to an Id:

Id key = (Id) result.get('p');
items.get(key).PO_Details_Ordered__c = Double.valueof(result.get('ordered'))
share|improve this answer
    
Hi Peter, thank you for taking the time to help me. Now i am getting the following error when i try to update a record: Update failed. First exception on row 0 with id a0Cf0000002WxGMEA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PODetailsCountsONProducts: execution of AfterUpdate caused by: System.FinalException: Record is read-only Class.PODetailsCountsONProducts.Run: line 19, column 1 Trigger.PODetailsCountsONProducts: line 7, column 1: []: Trigger.PODetailsRollupTrigger: line 29, column 1 –  Carlos Aug 1 at 23:33
    
Are you trying to update the records in the items Map with an update statement? You do not need to explicitly call a DML statement to do that, since it is a before update trigger just setting the values is in the Trigger.newMap records (items map in your class's function) is enough. The system will then include those values when it saves to the database. Think of the before update as intercepting the records and manipulating them however you want before the actual save to the database. –  Peter Knolle Aug 1 at 23:41
    
Peter, thank you so much. I was trying to update with an update statement. I truly appreciate your help. Have a great weekend! :) –  Carlos Aug 1 at 23:47
    
Thanks. I will!!! You too, Carlos! :) –  Peter Knolle Aug 1 at 23:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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