trigger quotepotential on Quote_Line_Item__c (after insert,after update) {
Set<id> uniqueNames=new Set<id>();
for(Quote_Line_Item__c a :Trigger.new) {
if(a.IsFutureContext__c) {
a.IsFutureContext__c = false;
} else {
uniqueNames.add(a.id);
}
}
if(!uniqueNames.isEmpty())
asyncApex.processAccounts(uniqueNames);
}
Future class
global class asyncApex {
@future
public static void processAccounts(set<id> id) {
Set<Id> quoteIds = new Set<Id>();
List<Quote__c> quotes = new List<Quote__c>();
for (Quote_line_item__c a : [Select Id, Name, IsFutureContext__c From Quote_line_item__c where ID IN :ID]) {
a.IsFutureContext__c = true;
if (a.Quote1__c != null){
quoteIds.add(a.Quote1__c);
}
Map<Id, Quote__c> mapQuote = new Map<Id, Quote__c>([Select Id, Potential__c from Quote__c where Id in :quoteIds]);
for(AggregateResult ar:[SELECT Quote1__c , SUM(Max_Batch__c)sumMax FROM Quote_Line_Item__c WHERE Quote1__c IN :quoteIds GROUP BY Quote1__c]) {
if(mapQuote.containsKey((ID)ar.get('Quote1__c'))){
Quote__C qu = mapQuote.get((ID)ar.get('Quote1__c'));
qu.Potential__c=(Decimal)ar.get('sumMax');
quotes.add(qu);
}
}
if(quotes.isempty() == false)
{
update quotes;
}
}
}
}