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
Smurfet15Smurfet15 

Get the list of all Opportunitylineitem and update the Opportunity field

Hi Guru,

I have a requrement to update the checkbox field in the opportunity everytime product is added/update in the opportunitylineitem.
Checkbox should be check if ANYONE of those materials has an 'FRAME' category. Below is working only when material is added with FRAME category. I need to list all product and loop to all lineitem.  I need a list code or SOQL for my lineitem...

Thanks in Advance!

----- here is logic and let me know if this correct-----
loop in list line item
{  if  (itemcategory == 'FRAME')
  { var vframe = true }
}
if (vframe)
{
  oppy.Includes_Frame__c = true ;
  update opportunity;
}
--------------------------------------------------------------
Here is my current code:
----------------------------------------------------------------
trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
//Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
    String OppID;
    String Olipc;    

    List<OpportunityLineItem> oppLineItems =
    [Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c
     from OpportunityLineItem where Id IN: trigger.new ];
    
        List<Opportunity> opprt = new List<Opportunity>(); //Get the Opportunity
    for(OpportunityLineItem Oli: oppLineItems){ //loop to the line items
      
     OppID = Oli.OpportunityID;
     Olipc = Oli.PricebookEntry.Product2.product_Category__c  ;
     
    if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
     {
       Opportunity oppy = new Opportunity(Id = OppID);
       oppy.Includes_Frame__c = true ;
       update oppy;  
     }
   }
Best Answer chosen by Smurfet15
Neetu_BansalNeetu_Bansal
Hi,

Few Considerations:
1. I guess you have restricted, that no one will manually update the Includes Frame field on Opportunity.
2. As Product cannot be change on Opportunity Line Item, so there is no use to write after update trigger.
3. I know this is silly, but it is also restricted that no one will update Category on Product.

Used this code:
trigger OpportunityLineItemTrg on OpportunityLineItem( after insert, before delete )
{
    List<OpportunityLineItem> oppLineItemslist;
    
    if( trigger.isInsert )
    {
        oppLineItemslist = trigger.new;
    }
    else if( trigger.isDelete )
    {
        oppLineItemslist = trigger.old;
    }
    
    List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.Product_Category__c
                                                from OpportunityLineItem where Id IN: oppLineItemslist
                                                AND PricebookEntry.Product2.Product_Category__c = 'Frame' ];
    
    Set<Id> oppIds = new Set<Id>();
    for( OpportunityLineItem Oli: oppLineItems )
    {
       oppIds.add( Oli.OpportunityID );
    }
    
    Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>([ Select Id, Includes_Frame__c, (Select Id, PricebookEntry.Product2.Product_Category__c,
                                                                        OpportunityId From OpportunityLineItems where PricebookEntry.Product2.Product_Category__c = 'Frame')
                                                                        From Opportunity where Id IN: oppIds ]);
    
    Map<Id, Opportunity> opportunitiesToBeUpdated = new Map<Id, Opportunity>();
    for( OpportunityLineItem Oli: oppLineItems )
    {
        Opportunity oppy = new Opportunity( Id = Oli.OpportunityID );
        
        if( trigger.isInsert )
        {
            if( opportunityMap.containsKey( Oli.OpportunityID )
                && !opportunityMap.get( Oli.OpportunityID ).Includes_Frame__c )
            {
                oppy.Includes_Frame__c = true;
                opportunitiesToBeUpdated.put( oppy.Id, oppy ); 
            }
        }
        else if( trigger.isDelete )
        {
            if( opportunityMap.containsKey( Oli.OpportunityID ) && opportunityMap.get( Oli.OpportunityID ).OpportunityLineItems.size() == 1
                && opportunityMap.get( Oli.OpportunityID ).Includes_Frame__c )
            {
                oppy.Includes_Frame__c = false;
                opportunitiesToBeUpdated.put( oppy.Id, oppy ); 
            }
        }
    }
    
    if( opportunitiesToBeUpdated.values().size() > 0 )
        update opportunitiesToBeUpdated.values();
}

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi,

Please use the below code:

trigger OpportunityUpdateFromOppProduct on OpportunityLineItem(after insert, after update)
{
    List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c
                                                from OpportunityLineItem where Id IN: trigger.new ];
    
    List<Opportunity> opprt = new List<Opportunity>(); //Get the Opportunity
    for(OpportunityLineItem Oli: oppLineItems)
    {
        if( Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
        {
            Opportunity oppy = new Opportunity(Id = Oli.OpportunityID);
            oppy.Includes_Frame__c = true ;
            opprt.add( oppy );  
        }
    }
    
    if( opprt.size() > 0 )
        update opprt;
}

Thanks,
Neetu
Smurfet15Smurfet15
Hi Neetu,

Thanks again for the response.  But i did try and it working only if you adding product with "FRAME" category, but what if we deleted that prduct then it should be uncheck as well.  This is reason I need to LIST for all productlineitem for the said opportunity, so we will check all line item if any of those product line is ''FRAME' then checkbox should be true else (if all item have "FRAME"  then checkbox should be uncheck.

Cheers,
Smurfet15
Neetu_BansalNeetu_Bansal
Hi,

Ok, try this:
trigger OpportunityUpdateFromOppProduct on OpportunityLineItem( after insert, after update, before delete )
{
    List<OpportunityLineItem> oppLineItemslist;
    
    if( trigger.isInsert || trigger.isUpdate )
    {
        oppLineItemslist = trigger.new;
    }
    else if( trigger.isDelete )
    {
        oppLineItemslist = trigger.old;
    }
    
    List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c
                                                from OpportunityLineItem where Id IN: oppLineItemslist ];
    
    List<Opportunity> opprt = new List<Opportunity>(); //Get the Opportunity
    for(OpportunityLineItem Oli: oppLineItems)
    {
        if( Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
        {
            Opportunity oppy = new Opportunity(Id = Oli.OpportunityID);
            
            if( trigger.isInsert || trigger.isUpdate )
            {
                oppy.Includes_Frame__c = true ;
            }
            else if( trigger.isDelete )
            {
                oppy.Includes_Frame__c = false ;
            }
            opprt.add( oppy );  
        }
    }
    
    if( opprt.size() > 0 )
        update opprt;
}

Thanks,
Neetu
Smurfet15Smurfet15
Hi Neetu,

I have tried it and it worked but there is one issue for this logic, If i have multiple product in the opportunity with "Frame" category and I deleted one then the checkbox become false which should remain to be checked there is one or more product that have still "Frame" category.

thanks in Advance.

 
Neetu_BansalNeetu_Bansal
Hi,

trigger OpportunityUpdateFromOppProduct on OpportunityLineItem( after insert, after update, before delete )
{
    List<OpportunityLineItem> oppLineItemslist;
    
    if( trigger.isInsert || trigger.isUpdate )
    {
        oppLineItemslist = trigger.new;
    }
    else if( trigger.isDelete )
    {
        oppLineItemslist = trigger.old;
    }
    
    List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c
                                                from OpportunityLineItem where Id IN: oppLineItemslist ];
    
    Set<Id> oppIds = new Set<Id>();
    for(OpportunityLineItem Oli: oppLineItems)
    {
        if( Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
        {
            oppIds.add( Oli.OpportunityID );
        }
    }
    
    Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>([ Select Id, (Select Id, OpportunityId From OpportunityLineItems)
                                                                        From Opportunity where Id IN: oppIds ]);
    List<Opportunity> opprt = new List<Opportunity>(); //Get the Opportunity
    for(OpportunityLineItem Oli: oppLineItems)
    {
        if( Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
        {
            Opportunity oppy = new Opportunity(Id = Oli.OpportunityID);
            
            if( trigger.isInsert || trigger.isUpdate )
            {
                oppy.Includes_Frame__c = true ;
                opprt.add( oppy ); 
            }
            else if( trigger.isDelete )
            {
                if( opportunityMap.containsKey( Oli.OpportunityID ) && opportunityMap.get( Oli.OpportunityID ).OpportunityLineItems.size() == 1)
                {
                    oppy.Includes_Frame__c = false;
                    opprt.add( oppy ); 
                }
            }
        }
    }
    
    if( opprt.size() > 0 )
        update opprt;
}

Thanks,
Neetu
Smurfet15Smurfet15
Hi Neetu,

Thanks for bearing with me on this. But sad to sa that recent is not working now.  I am still trying to figure out. Let me know if you have other solution on this.
Thanks again.

 
Smurfet15Smurfet15

H Neetu,

The issue now is once the Frame checkbox is not uncheck eventhough we have deleted produclineitem with "Frame" as product category.
Is it alright to text you on your skype just in case?  I am newbie here...just learning in process.

Thanks again.
Mary

 



 

Smurfet15Smurfet15
Hi Neetu,
I think this is where the issue.  Everytime I add "Frame" product the check box get updated correct but when you are deleting any Frame product it is updating anymore.
Can you please tell me the second condition? Not clear to me why only when lineitemsize ==1 then it should be false.
if( opportunityMap.containsKey( Oli.OpportunityID ) && opportunityMap.get( Oli.OpportunityID ).OpportunityLineItems.size() == 1)
{
           oppy.Includes_Frame__c = false ;
            opprt.add( oppy );
   }
Neetu_BansalNeetu_Bansal
Hi,

Few Considerations:
1. I guess you have restricted, that no one will manually update the Includes Frame field on Opportunity.
2. As Product cannot be change on Opportunity Line Item, so there is no use to write after update trigger.
3. I know this is silly, but it is also restricted that no one will update Category on Product.

Used this code:
trigger OpportunityLineItemTrg on OpportunityLineItem( after insert, before delete )
{
    List<OpportunityLineItem> oppLineItemslist;
    
    if( trigger.isInsert )
    {
        oppLineItemslist = trigger.new;
    }
    else if( trigger.isDelete )
    {
        oppLineItemslist = trigger.old;
    }
    
    List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.Product_Category__c
                                                from OpportunityLineItem where Id IN: oppLineItemslist
                                                AND PricebookEntry.Product2.Product_Category__c = 'Frame' ];
    
    Set<Id> oppIds = new Set<Id>();
    for( OpportunityLineItem Oli: oppLineItems )
    {
       oppIds.add( Oli.OpportunityID );
    }
    
    Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>([ Select Id, Includes_Frame__c, (Select Id, PricebookEntry.Product2.Product_Category__c,
                                                                        OpportunityId From OpportunityLineItems where PricebookEntry.Product2.Product_Category__c = 'Frame')
                                                                        From Opportunity where Id IN: oppIds ]);
    
    Map<Id, Opportunity> opportunitiesToBeUpdated = new Map<Id, Opportunity>();
    for( OpportunityLineItem Oli: oppLineItems )
    {
        Opportunity oppy = new Opportunity( Id = Oli.OpportunityID );
        
        if( trigger.isInsert )
        {
            if( opportunityMap.containsKey( Oli.OpportunityID )
                && !opportunityMap.get( Oli.OpportunityID ).Includes_Frame__c )
            {
                oppy.Includes_Frame__c = true;
                opportunitiesToBeUpdated.put( oppy.Id, oppy ); 
            }
        }
        else if( trigger.isDelete )
        {
            if( opportunityMap.containsKey( Oli.OpportunityID ) && opportunityMap.get( Oli.OpportunityID ).OpportunityLineItems.size() == 1
                && opportunityMap.get( Oli.OpportunityID ).Includes_Frame__c )
            {
                oppy.Includes_Frame__c = false;
                opportunitiesToBeUpdated.put( oppy.Id, oppy ); 
            }
        }
    }
    
    if( opportunitiesToBeUpdated.values().size() > 0 )
        update opportunitiesToBeUpdated.values();
}

Thanks,
Neetu
This was selected as the best answer
Smurfet15Smurfet15
Wow Neetu, this works. On the #3 item.  I have tried to go to the product and change the category and it work as well, we just need to clear and add again the product.  This is awesome!!!  I am going to study your code again. and hopefully create @test class for this.  Can I bother you just in case test class is not getting to 75%.?

Thank you very much,
Mary