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
DevSFDevSF 

Compare Opportunity Product with Order Product


when a Product is added to Opportunity, Check for previous Orders related to Opportunity account and
and if no orders exists then update a picklist field on Opportunity as (New).

if any Orders exists then check for the latest orders price and compare it with our new product price that we are adding.

if it is greater than new product's price then update  picklist on opportunity as Loss
if less then update as WOn.

I have Written a trigger to achieve this and was stuck at this point. Needed help to proceed further..!



trigger triggeronopportunity on Opportunity (before insert,before update) {
    List<opportunity> oppo = new List<opportunity>();
    List<Account> acc = new List<Account>();
    List<OpportunityLineItem> oppl = new List<OpportunityLineItem>();
    List<PricebookEntry> pb = new List<PricebookEntry>();
   Set<ID> OppIds = new Set<ID>();
    Set<ID> accIds = new Set<ID>();
    for(Opportunity opp : trigger.new){
        if(oppl.size() == 0){
            opp.Revenue_Type__c = 'New ';
            }
        for(Account a : [Select Id, Order_Product__c FROM Account Where Id in :OppIds ]){
            
========== Struck Here ==============
       
        }   
    }
}



 
Tushar Tyagi 45Tushar Tyagi 45
trigger UpdateAccountDetail on OpportunityLineItem(after insert,after update)
{
    list<OpportunityLineItem> oppLineId=[select OpportunityId from OpportunityLineItem where id in:trigger.new];
    list<Opportunity> UpdateOpp=new list<opportunity>();
    set<id> oppId=new set<id>();
    set<id> accid=new set<id>();
    for(OpportunityLineItem opp:oppLineId)
    {
        oppId.add(opp.OpportunityId);
    }   
    
    for(opportunity o:[select accountid from opportunity where id IN:oppId])
    {
        accid.add(o.accountid);
    }
   
    list<order> orderlist=[select id from order where accountId IN:accid];
    list<opportunity>Opplist=[select id,name from opportunity where id IN:oppId];
   Map<id,Opportunity> OpportunityMap=new Map<id,Opportunity>([select id,(select id from OpportunityLineItems) from opportunity where id IN:oppId]);

    if(orderlist.size()<0 || orderlist.size()==0 )
    {
        for(Opportunity opp:Opplist)
        {
           opp.picklist*='NEW';
            UpdateOpp.add(opp);
        }
    }
    else
    {
        for(opportunity opp:Opplist)
        {
          for(OpportunityLineItem newol:trigger.new)
            {
                if(newol.price*>OpportunityMap.get(opp.id).OpportunityLineItems.price*)
                {
                    opp.list*='Loss'
                    UpdateOpp.add(opp);  
                }
                else
                {
                    opp.list*='Profit'
                    UpdateOpp.add(opp);
                }
            }
        }
        
    }
    
    update UpdateOpp;
}

The above trigger has the solution. All you need to do is just replace the name of fields with correct field names which are marked with '*'.