1

one quote line item have many batches i have a look up relation ship between quote line item and batch .in batch value one of the field batch value is .i need to get the max batch value to be printed in quote line items .help me this is the scenario i am new to this apex coding working hard to write it.

this the trigger i wrote .this is not showimg the value

trigger batchtoquotelineitem on Batch__c (after insert) {
     list<Quote_line_item__c> quo=new list<Quote_line_item__c>();

     for (batch__c ba:trigger.new) {
           Quote_Line_Item__c quotelineitem=new Quote_Line_Item__c();
           AggregateResult[] groupedResults=[select MAX(batch_value__c)ba from batch__c group by  Quote_Line_Item__c];

           for (AggregateResult ar : groupedResults)  {
                quotelineitem.max_batch__c= (Decimal)ar.get('ba');
                quotelineitem.id=ba.id;
                quo.add(quotelineitem);
                insert quo;

           } 
     }
}

help me to find this logic

3

So from what I understand you want to update the Parent Object being the Quote_Line_Item__c with the MAX() value of the field batch_value_c on its related children (Batch_c), this should occur on insert of each Batch__c record.

I have not tested but something like this should do the trick. I have added comments so that you can understand my reasoning.

trigger batchtoquotelineitem on Batch__c (after insert) 
{
    list<Quote_line_item__c>   quo                  = new list<Quote_line_item__c>();
    set<Id>                    setQuoteLineItemIds  = new set<Id>();

    // Create a set of all the Quote Line Item Ids
    for (batch__c ba:trigger.new) 
    {
        setQuoteLineItemIds.add(Ba.Quote_Line_Item__c);
    }

    // Get the aggregate results for only what we need
    List<aggregateResult> results groupedResults = [SELECT Id, MAX(batch_value__c)ba 
                                        FROM batch__c WHERE Quote_Line_Item__c 
                                        IN : setQuoteLineItemIds
                                        GROUP BY Quote_Line_Item__c];

    // Loop through our aggregate results and create our line items
   for (AggregateResult ar : groupedResults)
   {
        Quote_Line_Item__c quotelineitem = new Quote_Line_Item__c();
        quotelineitem.max_batch__c= (Decimal)ar.get('ba');
        quotelineitem.Id = ar.get('Id');
        quo.add(quotelineitem);
   } 

    // Use an update statement instead of an insert statement and outside of the for loop
    if(quo.isempty() == false)
    {
        update quo;
    }    
}
  • 1
    @[Mitch Hunt] It's no longer necessary to check if a list is empty before performing a DML on it. If the list is empty, it is a no-op that doesn't count against the DML limits. – sfdcfox Oct 11 '13 at 14:22
  • 1
    Well there you go answer a question, learn something new, cheers. – Mitch Hunt Oct 11 '13 at 21:01
  • hi your code is excellent but i am getting the error id is not specified in an update call – Teja Nov 14 '13 at 6:45

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.