2
trigger quotepotential on Quote_line_item__c (after insert,after update)  {
    list<Quote__c> qu=new list<Quote__c>();
    for(Quote_Line_Item__c Qli:trigger.new){

        Quote__c quotepotential=new Quote__c();
        quotepotential.id=qli.Quote1__c; AggregateResult[]
        groupedResults=[Select Quote1__c, SUM(max_batch__c)ba From Quote_Line_Item__c
                       Where Quote1__c =: quotepotential.id group by Quote1__c ]; 
        for (AggregateResult ar : groupedResults)  {
            quotepotential.potential__c= (Decimal)ar.get('ba');
            Qu.add(quotepotential); 
        } 
     }   
     update Qu; 
}

This is the trigger which I wrote. It is showing errors while deploying in production. I have attached the image of the errors page. It is throwing from old test cases which was already in production.

Please help me in optimizing the code to deploy in productionenter image description here

1
  • Suggest you would get more interest if you formatted your trigger code more cleanly; use the {} button and tweak.
    – Keith C
    Nov 13, 2013 at 13:16

2 Answers 2

3

Presumably there is a 1 to many relationship between Quote and QuoteLineItem. I can see these problems:

  • Sometimes there are multiple instances of the same Quote in the Quote update - “Duplicate id in list". This will happen when there is more than one QuoteLineItem in a quote.
  • Other times the Quote id appears not to be set in the Quote update presumably because Quote1__c is not always being set in the tests (or in logic executed in other triggers).
  • Though not present in your screen shot, executing a query inside the loop is a governor limit error waiting to happen and this should also be eliminated.

A common pattern for this sort of logic is two loops. The first gathers the set of ids for Quote from the QuoteLineItems an then the second loop driven by a single query prepares the Quote objects and is followed by the single update of the Quotes.

Hopefully none of your tests have SeeAllData(true) set because if they do results may vary from org to org. But other than that, I would expect the same issues in a development org if the same code is present.

2
  • I am getting error that id is not specified in an update call
    – Teja
    Nov 14, 2013 at 6:52
  • Assuming you are using the code posted by @sfdcfox, the bodge fix would be to add "AND Quote1__c != null" to the aggregate query. But a better fix would be to clean up the design by making the relationship a master-detail one.
    – Keith C
    Nov 14, 2013 at 9:31
3

You're querying the quotes inside a loop per line item, which will fail when more than one line item appears in a transaction. Also, presumably, you meant to sum all items together. This trigger illustrates how to achieve your goal.

trigger quotepotential on Quote_Line_Item__c (after insert, after update) {
    Set<Id> quoteIds = new Set<Id>();
    List<Quote__c> quotes = new List<Quote__c>();
    for(Quote_Line_Item__c record: Trigger.new) {
        quoteIds.add(record.Quote1__c);
    }
    for(AggregateResult ar:[SELECT Quote1__c Id, SUM(Max_Batch__c) sumMax FROM Quote_Line_Item__c WHERE Quote__c IN :quoteIds GROUP BY Quote1__c]) {
        quotes.add(new Quote__c(Id=(Id)ar.get('Id'), Potential__c=(Decimal)ar.get('sumMax')));
    }
    update quotes;
}

For "Id not specified in update" errors, as the other answer states, check your test methods-- this field should be required, and not populating that field will result in the error.

4
  • Thank you for your reply .but i am not getting the value for
    – Teja
    Nov 13, 2013 at 17:23
  • potential__c ..
    – Teja
    Nov 13, 2013 at 17:26
  • i am getting error that id not specified in an update call
    – Teja
    Nov 14, 2013 at 6:53
  • Make sure that Quote1__c is never null-- you probably want to validate that.
    – sfdcfox
    Nov 14, 2013 at 14:27

You must log in to answer this question.

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