1
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;
            }
        }
    }
}

this is exception i am getting while deploying

2
  • use debug logs and use system.debug to find of the reason which part of code not working. Commented Dec 3, 2013 at 5:28
  • you don't find any error by seeing the code
    – Teja
    Commented Dec 3, 2013 at 5:35

2 Answers 2

2

Your trigger is completely wrong; you shouldn't be using a field for this at all. Not to mention, Trigger.new is read-only in "after" triggers, so this code won't actually stop the recursive future calls. Try this trigger:

trigger quotepotential on Quote_Line_Item__c (after insert, after update) {
    if(System.isFuture()) {
        return;
    }
    asyncApex.processAccounts(Trigger.newMap.keySet());
}

Next, your aggregate query is incorrect-- when you do not alias a field, the expression is named 'exprX', where X is the numeric position of this expression. Change that portion as follows:

global class asyncApex {
    @future public static void processAccounts(Set<Id> quoteIds) {
        Map<Id, Quote__c> quotes = new Map<Id, Quote__c>();
        for(Id quoteId: quoteIds) {
            quotes.put(quoteId, new Quote__c(Id=quoteId, Potential__c=0.0));
        }
        for(AggregateResult ar:[SELECT Quote1__c Id, SUM(Max_Batch__c) sumMax FROM Quote_Line_Item__c WHERE Quote1__c IN :quoteIds GROUP BY Quote1__c]) {
            quotes.get((Id)ar.get('Id')).Potential__c = (Decimal)ar.get('sumMax');
        }
        update quotes.values();
    }
}

Finally, it should be said that nothing in this code should result in a "too many future calls" error, so presumably the problem lies with the test method itself.

Edit

Updated code; I mistakenly used the "standard" quote object instead of the custom one.

8
  • @user4045 It's only 5am in the morning, but there you have it. I forgot you were using a custom object.
    – sfdcfox
    Commented Dec 3, 2013 at 11:14
  • @user4045 Typo. Fixed it.
    – sfdcfox
    Commented Dec 3, 2013 at 11:47
  • @user4045 The function name was misspelled in the class.
    – sfdcfox
    Commented Dec 3, 2013 at 11:50
  • i am not getting value to the potential
    – Teja
    Commented Dec 3, 2013 at 12:03
  • code is running fine
    – Teja
    Commented Dec 3, 2013 at 12:04
2

Below query doesnt have Quote1__c field in it

 // Add Quote1__c  in the below query
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);
            }

I am surprised why you are not getting SOQL field reference without querying error.

9
  • super ,i got this now it is working
    – Teja
    Commented Dec 3, 2013 at 6:11
  • i am getting exception while deploying can you sort it
    – Teja
    Commented Dec 3, 2013 at 7:26
  • What is the exception ? Commented Dec 3, 2013 at 7:39
  • i had a photocopy pasted in the post check it out'
    – Teja
    Commented Dec 3, 2013 at 7:40
  • Can you paste the test class? Commented Dec 3, 2013 at 7:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .