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
shakila Gshakila G 

Rollup summary trigger formula field?

Hi All,
I have the Qline Item With Cross object Formula Field (Stock_Not_Available__c)     

I want to rollup this field in quote object( No_of_Products_Stock_Not_Available__c)



My trigger Working working for add. Sum() Exam- 5
But if the count is  reduced in Quote line item it's not revert backing in Quote  Emaple 2

Result remain same in Quote as Exam; 5

I wrote  a trigger   :

trigger RollupProjectCountTrigger on QuoteLineItem  (Before Insert, after insert) {

    List<Id> VQuoteID = new List<Id>();
    
    for(QuoteLineItem p: Trigger.new) {
        VQuoteID.add(p.QuoteiD);
    }
    
    AggregateResult[] groupedResults = [SELECT Sum(Stock_Not_Available__c) lineItemCOunt, QuoteiD FROM QuoteLineItem 
    WHERE QuoteiD in :VQuoteID  GROUP BY QuoteiD];
    
    Map<id,Quote> QuoteMap = new Map<Id,Quote>([SELECT id, No_of_Products_Stock_Not_Available__c 
    FROM Quote WHERE Id in :VQuoteID]); 
    
    for(AggregateResult ar: groupedResults) {
    
    QuoteMap.get((Id)ar.get('QuoteiD')).No_of_Products_Stock_Not_Available__c = (decimal) ar.get('lineItemCOunt');

       
    }
    
    try {
        update QuoteMap.values();
    }   catch(DmlException e) {
        System.debug(e.getMessage());
    }

}


Kindly let me know where am doing mistake

                  
Best Answer chosen by shakila G
Akshay_DhimanAkshay_Dhiman
Hi Shakila,


 In your Trigger, you used the same event it is not possible.
 you used (Before insert, after insert) it wrong change it by (Before insert, before update )
 
 and try this code to rollup  field in quote object( No_of_Products_Stock_Not_Available__c)
trigger RollupProjectCountTrigger on QuoteLineItem  (after insert,after update,after delete) {
    
    List<Id> VQuoteID = new List<Id>();
    
    if(Trigger.isInsert){
        for(QuoteLineItem p: Trigger.new) {
            VQuoteID.add(p.QuoteiD);
        }
    }
    if(Trigger.isUpdate || Trigger.isDelete){
        for(QuoteLineItem p: Trigger.old) {
            VQuoteID.add(p.QuoteiD);
        }
        
    }
    
    List<Quote> quoteList = new List<Quote>();
    List<Quote> toUpdatequoteList = new List<Quote>();
    
    if(VQuoteId.size() > 0 ){
        quoteList =[ SELECT No_of_Products_Stock_Not_Available__c,(SELECT Stock_Not_Available__c FROM QuoteLineItems where Stock_Not_Available__c!=null ) FROM Quote WHERE Id IN :VQuoteID]
            
            }
    if(quoteList.size() > 0){
        for(Quote quot:quoteList){
            if(quot.QuoteLineItems.size() > 0){
                Integer Total =0;
                for(QuoteLineItem qlt:quot.QuoteLineItems){
                    Total+=qlt.Stock_Not_Available__c;
                }
                quot.No_of_Products_Stock_Not_Available__c=Total;
                toUpdatequoteList.add(quot);
            }
        }
    }
    
    try {
        update toUpdatequoteList;
    }catch(DmlException e)
    {
        System.debug(e.getMessage());
    } 
}

if you found this answer helpful then please mark it as best answer so it can help others.

Thanks 
Akshay Dhiman

All Answers

Waqar Hussain SFWaqar Hussain SF
try below code
trigger RollupProjectCountTrigger on QuoteLineItem  (after insert) {

    List<Id> VQuoteID = new List<Id>();
    
    for(QuoteLineItem p: Trigger.new) {
        VQuoteID.add(p.QuoteiD);
    }
    
    AggregateResult[] groupedResults = [SELECT Sum(Stock_Not_Available__c) lineItemCOunt, QuoteiD FROM QuoteLineItem 
    WHERE QuoteiD in :VQuoteID  GROUP BY QuoteiD];
    
    Map<id,Quote> QuoteMap = new Map<Id,Quote>([SELECT id, No_of_Products_Stock_Not_Available__c 
    FROM Quote WHERE Id in :VQuoteID]); 
    
    for(AggregateResult ar: groupedResults) {
    
    QuoteMap.get((Id)ar.get('QuoteiD')).No_of_Products_Stock_Not_Available__c = (decimal) ar.get('lineItemCOunt');

       
    }
    
    //try {
        update QuoteMap.values();
    //}   catch(DmlException e) {
        //System.debug(e.getMessage());
    //}

}

Let me know If any error occur.
Waqar Hussain SFWaqar Hussain SF
trigger RollupProjectCountTrigger on QuoteLineItem  (after insert, after update) {

    List<Id> VQuoteID = new List<Id>();
    
    for(QuoteLineItem p: Trigger.new) {
        VQuoteID.add(p.QuoteiD);
    }
    
    AggregateResult[] groupedResults = [SELECT Sum(Stock_Not_Available__c) lineItemCOunt, QuoteiD FROM QuoteLineItem 
    WHERE QuoteiD in :VQuoteID  GROUP BY QuoteiD];
    
    Map<id,Quote> QuoteMap = new Map<Id,Quote>([SELECT id, No_of_Products_Stock_Not_Available__c 
    FROM Quote WHERE Id in :VQuoteID]); 
    
    for(AggregateResult ar: groupedResults) {
    
    QuoteMap.get((Id)ar.get('QuoteiD')).No_of_Products_Stock_Not_Available__c = (decimal) ar.get('lineItemCOunt');

       
    }
    
    //try {
        update QuoteMap.values();
    //}   catch(DmlException e) {
        //System.debug(e.getMessage());
    //}

}

 
Akshay_DhimanAkshay_Dhiman
Hi Shakila,


 In your Trigger, you used the same event it is not possible.
 you used (Before insert, after insert) it wrong change it by (Before insert, before update )
 
 and try this code to rollup  field in quote object( No_of_Products_Stock_Not_Available__c)
trigger RollupProjectCountTrigger on QuoteLineItem  (after insert,after update,after delete) {
    
    List<Id> VQuoteID = new List<Id>();
    
    if(Trigger.isInsert){
        for(QuoteLineItem p: Trigger.new) {
            VQuoteID.add(p.QuoteiD);
        }
    }
    if(Trigger.isUpdate || Trigger.isDelete){
        for(QuoteLineItem p: Trigger.old) {
            VQuoteID.add(p.QuoteiD);
        }
        
    }
    
    List<Quote> quoteList = new List<Quote>();
    List<Quote> toUpdatequoteList = new List<Quote>();
    
    if(VQuoteId.size() > 0 ){
        quoteList =[ SELECT No_of_Products_Stock_Not_Available__c,(SELECT Stock_Not_Available__c FROM QuoteLineItems where Stock_Not_Available__c!=null ) FROM Quote WHERE Id IN :VQuoteID]
            
            }
    if(quoteList.size() > 0){
        for(Quote quot:quoteList){
            if(quot.QuoteLineItems.size() > 0){
                Integer Total =0;
                for(QuoteLineItem qlt:quot.QuoteLineItems){
                    Total+=qlt.Stock_Not_Available__c;
                }
                quot.No_of_Products_Stock_Not_Available__c=Total;
                toUpdatequoteList.add(quot);
            }
        }
    }
    
    try {
        update toUpdatequoteList;
    }catch(DmlException e)
    {
        System.debug(e.getMessage());
    } 
}

if you found this answer helpful then please mark it as best answer so it can help others.

Thanks 
Akshay Dhiman
This was selected as the best answer
shakila Gshakila G
Thanks its working fine