I have a trigger whenever a quote is marked as primary then the QuotelineItem would copy the Opportunity line item. for insertion, it's working fine but during the checkbox is checked when I tried to delete the one QuotelineItem from the Quote, the Quotelineitem was deleted in Quote level, but for the Opportunity line item level is not deleted. Help need. Code is below

trigger QuoteLineItem on Quote(after update) {
    Set<Id> quoteIds = new Set<Id>();
    Set<Id> OppIds = new Set<Id>();
    Set<Id> setIdOpp = new Set<Id>();
    boolean QuoteFlag = false;
    boolean QuFlag = false;
    List<OpportunityLineItem> lstQLI = new List<OpportunityLineItem>();
    list<OpportunityLineItem> listOfoli = new list<OpportunityLineItem>();

    for (Quote q : Trigger.new) {
    listOfoli = [Select Id From OpportunityLineItem Where OpportunityId = :OppIds];
    for (QuoteLineItem objOLI : [
            SELECT Id,Converted_ACV_Gross__c,Converted_ACV_VAD__c,Converted_TCV_Gross__c,Converted_TCV_VAD__c,LOB__c,vendor__c,Cost_Year_1__c,Cost_Year_2__c,Cost_Year_3__c,Cost_Year_4__c,Cost_Year_5__c,Component_Type__c,ACV_Gross_Year_1__c,ACV_Gross_Year_2__c,ACV_Gross_Year_3__c,ACV_Gross_Year_4__c,ACV_Gross_Year_5__c,Currency__c, Quote.Primary__c,Quote.OpportunityId,PricebookEntryId,
                    PricebookEntry.Product2ID, Quantity, UnitPrice,Legal_Entity__c,ACV_VAD_Year_2__c,ACV_VAD_Year_3__c,ACV_VAD_Year_4__c,ACV_VAD_Year_5__c,ACV_Gross__c,TCV_Gross__c,TCV_VAD__c
            FROM QuoteLineItem
            WHERE QuoteId = :quoteIds
    ]) {

        if (objOLI.Quote.Primary__c == true) {

            QuoteFlag = true;
            System.debug('entered' + QuoteFlag);
            lstQLI.add(new OpportunityLineItem(
                    OpportunityId = objOLI.Quote.OpportunityId,
                    Product2Id = objOLI.PricebookEntry.Product2ID,
                    Quantity = objOLI.Quantity,
                    UnitPrice = objOLI.UnitPrice,
                    PriceBookEntryId = objOLI.PricebookEntryId));
        } else if (objOLi.Quote.Primary__c != Trigger.oldMap.get(objOLi.Quote.Id).Primary__c && objOLi.Quote.Primary__c == false) {
            System.debug('delete' + QuoteFlag);
            QuFlag = true;
        } else {

            System.debug('else delete' + QuoteFlag);

    System.debug('listOfoli' + listOfoli + QuoteFlag);
    System.debug('lstQLI' + lstQLI);
    if (QuFlag) {
        delete listOfoli;
    if (QuoteFlag && listOfoli.size() > 0) {
        delete listOfoli;

    if (!lstQLI.isEmpty()) {
        System.debug('lstQLI' + lstQLI);
        insert lstQLI;
  • your trigger is after update and your requirementt as written says the issue is when the quoteitem is deleted, you need an after delete trigger or logic within this trigger to also cater for the after delete event
    – cropredy
    Dec 5, 2020 at 16:04
  • upon rereading your trigger, my previous comment is nop. It is not clear what the requirement is and I would suggest you use edit and restate the requirement with an example or two.
    – cropredy
    Dec 7, 2020 at 3:57


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.