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 

Opportunity Product Trigger

Hi friends, My Task is to write a Trigger

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>();
   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(Order o : [SELECT Id FROM Order WHERE id in :accIds]){
         //======== Strucked Here =============
        }   
    }
    
}

How to Proceed further....! (Or) any other way to do this...!
Atul GuptaAtul Gupta
The scenario you have described here will work when 1 product is being added to an opportunity.
What if multiple products are being added? In that case, how would this work?

for eg.
1. You have mentioned that if orders exist then check for the lates order price and compare it with the product price being added. What if I'm adding 3-4 products to the opportunity?
Which product's price should be compared with the opportunity's latest order price?
DevSFDevSF
Hi Atul Gupta,
Thanks for your response.

Yes, you are right..!
But, we just will add 1 Product at a time and check if any orders exists if exists compare with order price and the product price.

I need this trigger when 1 Product added to opportunity. (My Scenario)
Can u Help me completing this....!