ShowAll Questionssorted byDate Posted
Pankaj 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 Tripathi
Hi Pankaj,
```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){
}
}

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;
commulativeAmount = 0;
}
}
if(OpportunityUpdate.size()>0){
update OpportunityUpdate;
}

}
}```

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

Regards,
Suraj

Suraj Tripathi
Hi Pankaj,
```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){
}
}

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;
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 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.