function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tommy GeorgiouTommy Georgiou 

Mass update quote line items error

Hi all,

I have created  triggers for Quote Line Items where it checks a checkbox as soon as the Product Family of the item equals the value that I've set for each trigger.
One of the triggers looks like : 
trigger UpdateQuoteCheckBoxCake on QuoteLineItem (after insert, after update)
{
    List<Quote> quoteList = new List<Quote>();
    
    for(QuoteLineItem currQli : Trigger.New)
    {
        if(currQli.Product_Family__c == 'Cakes')
        {
            quoteList.add(new Quote(Id = currQli.QuoteId, Cakes__c = true));
        }
    }
    
    if(!quoteList.isEmpty())
    {
      update quoteList;
    }
}

Now I am trying to update all the records in order to get teh boxes checked and I am getting an error like 
Apex trigger UpdateQuoteCheckBoxReception caused an unexpected exception, contact your administrator: UpdateQuoteCheckBoxReception: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0Q0w0000000o68RCAQ: Trigger.UpdateQuoteCheckBoxReception: line 15, column 1

Any Ideas how can I resolve this?
Best Answer chosen by Tommy Georgiou
sandeep sankhlasandeep sankhla
Hi Tommy,

Plese use map insetad of list

map<Id, Quote> mapQuoteIdtoQuote = new map<Id, Quote>();

mapQuoteIdtoQuote.put(currQli.QuoteId, new Quote(Id = currQli.QuoteId, Cakes__c = true));

update mapQuoteIdtoQuote.values();

replace your list with maps..

Please check with this and let me know if this solve your issue..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

Rahul BorgaonkarRahul Borgaonkar
Hi,

You don't need to use update DML in trigger for same object. If you change any field value it should change it permanently.  Using DML is trigger may cause runtime Error.

Suggested changes.
trigger UpdateQuoteCheckBoxCake on QuoteLineItem (after insert, after update)
{
	    //List<Quote> quoteList = new List<Quote>();
	     
	    for(QuoteLineItem currQli : Trigger.New)
	    {
	        if(currQli.Product_Family__c == 'Cakes')
	        {
	            currQli.Cakes__c = true;
	        }
	    }
/*	     
	    if(!quoteList.isEmpty())
	    {
	      update quoteList;
	    }
*/
}
Regards

 
sandeep sankhlasandeep sankhla
Hi Tommy,

You want to update the Quote or Quote line items based on your req ??

Please let me know your req so I can help you out on this..

Based on your above code it is throwing error because list where you are storuing the IDs having duplicate IDs..

Many QuoteLineItems can have same Quote as parent so it will throw an error..you can use map here or check if list already contains the id then dont add them....

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
Tommy GeorgiouTommy Georgiou
Hi Rahul,

Will try and see.

Hi Sandeep,

My checkboxes are on the Quote so I want them to become checked based on the triggers that are structed for Quote Line Items. So I figured out that by updating the Items my boxes will become checked .
Rahul BorgaonkarRahul Borgaonkar
Hi Tommy,

Sandeep is right. You can use Set collection instead of List to solve this issue. You need to convert set to list again while using in update though.

Regards,
Tommy GeorgiouTommy Georgiou
Hi Rahul.

Tried your solution but it's not working. The checkboxes are designed as Quote fields and not as Quote Line Items ones. 
sandeep sankhlasandeep sankhla
Hi Tommy,

Plese use map insetad of list

map<Id, Quote> mapQuoteIdtoQuote = new map<Id, Quote>();

mapQuoteIdtoQuote.put(currQli.QuoteId, new Quote(Id = currQli.QuoteId, Cakes__c = true));

update mapQuoteIdtoQuote.values();

replace your list with maps..

Please check with this and let me know if this solve your issue..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
Tommy GeorgiouTommy Georgiou
Hi Sandeep

Now the trigger is 
trigger UpdateQuoteCheckBoxCake on QuoteLineItem (after insert, after update)
{
    
map<Id, Quote> mapQuoteIdtoQuote = new map<Id, Quote>();

mapQuoteIdtoQuote.put(currQli.QuoteId, new Quote(Id = currQli.QuoteId, Cakes__c = true));

update mapQuoteIdtoQuote.values();
    }

based on this I get the following error :Error: Compile Error: Variable does not exist: currQli.QuoteId at line 6 column 23

 
sandeep sankhlasandeep sankhla
Hi Tommy,

Please replace your code with this

trigger UpdateQuoteCheckBoxCake on QuoteLineItem (after insert, after update)
{

    map<Id, Quote> mapQuoteIdtoQuote = new map<Id, Quote>();

    for(QuoteLineItem currQli : Trigger.New)

    {
        if(currQli.Product_Family__c == 'Cakes')

        {

            mapQuoteIdtoQuote.put(currQli.QuoteId, new Quote(Id = currQli.QuoteId, Cakes__c = true));
        }

    }
     

    if(!mapQuoteIdtoQuote.isEmpty())

    {
      update mapQuoteIdtoQuote.values();

    }

}


Please replace this and let me know if it solves your problem

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
sandeep sankhlasandeep sankhla
Hi Tommy,

Please try the ablove code and let me know if that solves your issue.

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
Tommy GeorgiouTommy Georgiou
Hi Sandeep,


I am trying now to deploy this onto production and try and update the quote line items to see if it works. Will let you know in a bit
Tommy GeorgiouTommy Georgiou
Hi Sandeep,

It seems that with the new trigger I need to change the test class as well because I am covering the code up to 71% only. 
My test class is :
 
@isTest
private class QuoteperProductFamTransport {

    static testMethod void UpdateQuoteCheckBox_Test1() {
        // TO DO: implement unit test
        
         Account a = new Account(Name = 'Test');
        insert a; 
        
        Id pricebookId = Test.getStandardPricebookId();                         

        Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1', isActive = true, Family= 'Transport');
        insert prd1;
        system.assert(prd1.Id != NULL);
        
        PricebookEntry pe=new PricebookEntry(UnitPrice = 1,Product2Id=prd1.id,Pricebook2Id=pricebookId,isActive=true);
        insert pe;
        system.assert(pe.Id != NULL);
            

        
        Opportunity objOpportunity = new Opportunity(Name='Test-Opportunty', AccountId=a.id, CloseDate=Date.Today(),
                                                     StageName='Closed Won');
        insert objOpportunity;
        system.assert(objOpportunity.Id != NULL);
        
        
        Quote objQuote = new Quote(Name='Test-Quote', OpportunityId=objOpportunity.Id, Pricebook2Id = pricebookId);
        insert objQuote;
        system.assert(objQuote.Id != NULL);
        
        
        
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>{
                                                            new QuoteLineItem(QuoteId=objQuote.Id, PricebookEntryId=pe.Id, Quantity=1, UnitPrice=1.1)
                                                            };      
        insert lstQLI;
        
        
        List<QuoteLineItem> lstClonedQLI = new List<QuoteLineItem>([select Id from QuoteLineItem where QuoteId =: objQuote.Id]);
        
                
    }
}

Any Ideas why it won't cover up to 100%?
 
sandeep sankhlasandeep sankhla
Hi Tommy,

QuoteLineItem which you are inserting should have 
Product_Family__c == 'Cakes'

Please add this and check..
Tommy GeorgiouTommy Georgiou
Hi Sandeep,


I am trying to get that on the 
QuoteLineItem(QuoteId=objQuote.Id, PricebookEntryId=pe.Id, Quantity=1, UnitPrice=1.1,
Product_Family__c == 'Cakes'
)
                                                            };
but it wont save Compile Error: expecting an equals sign, found '==' at line 35 column 167

then I remove one of the = and the error is 
Error: Compile Error: Field is not writeable: QuoteLineItem.Product_Family__c at line 35 column 170

 
sandeep sankhlasandeep sankhla
Hi Tommy,

You dont have write permision  for this field, please check the setting for this as it is custom field..give the eprmission and then check again...chck your profile also..
Tommy GeorgiouTommy Georgiou
Hi Sandeep,

Found the problem. It was on the trigger where I mispelled the Product Family
sandeep sankhlasandeep sankhla
Hi Tommy,

great! now try and let me know if you are able to deploy.
Tommy GeorgiouTommy Georgiou
Hi Sandeep,


Works like a charm
sandeep sankhlasandeep sankhla
Thanks Tommy..

Feel free to reach out for all your queries..