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
Pankaj PariharPankaj Parihar 

trigger calculation

hello friends 
plz help me with the code.
Based on the probability, calculate the amount for each product and display the amount.
Display the cumulative amount in Opportunity object only if the cumulative probability has crossed 50%.
Best Answer chosen by Pankaj Parihar
Suraj TripathiSuraj Tripathi
Hi Pankaj,
Please try this piece of code. Hope it will help you.
trigger Ca_Qsn_trigger_opp on Opportunity (after Update) {
    if(Trigger.IsUpdate){
        Set<Id> oppId = new Set<Id>();
       List<Opportunity> OppNewList = new List<Opportunity>();
       for(Opportunity op : Trigger.new){
            if(op.Probability > 50){  
                oppId.add(op.Id);
                OppNewList.add(op);
            }
        }
        
        map<id,decimal> mp1 = new map<id,decimal>();
        for(Opportunity op : Trigger.old){
            mp1.put(op.id, op.Probability);
        }
       
        List<OpportunityLineItem> oliList  = new List<OpportunityLineItem>();
        oliList = [select id, OpportunityId, unitprice from OpportunityLineItem where Opportunityid =: oppId];
        
        List<Opportunity> OpportunityUpdate = new List<Opportunity>(); 
        
        decimal commulativeAmount = 0;
        
        for(Opportunity oid : OppNewList){
            if(oid.Probability != mp1.get(oid.id)){
            for(OpportunityLineItem oli : oliList){
                if(oli.OpportunityId == oid.id ){
                    commulativeAmount = commulativeAmount + oli.Unitprice;
                }
            }
            Opportunity opp = new Opportunity();
            opp.id = oid.id;
            opp.ashwani__cumulative_amount__c = commulativeAmount;
            OpportunityUpdate.add(opp);
            commulativeAmount = 0;
        }
        }
        if(OpportunityUpdate.size()>0){
            update OpportunityUpdate;
        }
        
    }
}

If this code helps you. Please mark it as best.

Regards,
Suraj

All Answers

Suraj TripathiSuraj Tripathi
Hi Pankaj,
Please try this piece of code. Hope it will help you.
trigger Ca_Qsn_trigger_opp on Opportunity (after Update) {
    if(Trigger.IsUpdate){
        Set<Id> oppId = new Set<Id>();
       List<Opportunity> OppNewList = new List<Opportunity>();
       for(Opportunity op : Trigger.new){
            if(op.Probability > 50){  
                oppId.add(op.Id);
                OppNewList.add(op);
            }
        }
        
        map<id,decimal> mp1 = new map<id,decimal>();
        for(Opportunity op : Trigger.old){
            mp1.put(op.id, op.Probability);
        }
       
        List<OpportunityLineItem> oliList  = new List<OpportunityLineItem>();
        oliList = [select id, OpportunityId, unitprice from OpportunityLineItem where Opportunityid =: oppId];
        
        List<Opportunity> OpportunityUpdate = new List<Opportunity>(); 
        
        decimal commulativeAmount = 0;
        
        for(Opportunity oid : OppNewList){
            if(oid.Probability != mp1.get(oid.id)){
            for(OpportunityLineItem oli : oliList){
                if(oli.OpportunityId == oid.id ){
                    commulativeAmount = commulativeAmount + oli.Unitprice;
                }
            }
            Opportunity opp = new Opportunity();
            opp.id = oid.id;
            opp.ashwani__cumulative_amount__c = commulativeAmount;
            OpportunityUpdate.add(opp);
            commulativeAmount = 0;
        }
        }
        if(OpportunityUpdate.size()>0){
            update OpportunityUpdate;
        }
        
    }
}

If this code helps you. Please mark it as best.

Regards,
Suraj
This was selected as the best answer
Pankaj PariharPankaj Parihar
hello suraj
tnx it really hepl me a lot ..
can u also help me in batch questions plz...i m in big trubble..i have to complete casestudy..but my codeing skilsis less..there r 20 questions left..plz suraj.
1. Assume there are 10 million Account records in the Org. Create a custom field on Accounts called Import Date (Data type: Date). For all these Accounts, populate the value for the Import Date field (value should be same as the Created date of the record).
2.Merging duplicate records in leads based on first name and last name. Batch should run every day.
3.Assign all the leads created during non-business hours to specified queue and leads created during business hours to specified queue.