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
Shruthi MN 19Shruthi MN 19 

Code ato all opp amount

I have written the below code to update bulk records to add all opportunity amount on quota object. Since it is a bulk record I have used asynchronous bulk update records.


public with sharing class listopponquota {
     public list<Opportunity> oppList{get;set;}
    public list<quota__c> qutoaList{get;set;}
    public set<ID> oppIds = new set<ID>(); 
 
public void listopponquota()
    {
        String selectedVal ='';
        oppList = new list<Opportunity>();
        qutoaList = new list<quota__c>();
        oppList();
    }
    
    
    
    public pageReference oppList() {
        Id qId = (Id) ApexPages.currentPage().getParameters().get('id');
        system.debug('Id-->'+qId);    
        List<quota__c> quotaList1 = new List<quota__c>();
        List<Opportunity> oppList1 = new List<Opportunity>();
        
        quotaList1 =[select Id, Name,Start_Date__c,End_Date__c,Assign_to_User__c,Actual_Amount__c, OwnerId from quota__c where Id   =: qId Limit 1];
        oppList1 = [Select Id, Name, CloseDate, StageName,OwnerId,Amount From Opportunity where StageName = 'Closed Won'];
        
        system.debug('quotaList1-->'+quotaList1);
        if(quotaList1.size() > 0){
            for(quota__c q : quotaList1){
                
                for(Opportunity opp : oppList1){
                    system.debug('closedDate-->'+opp.CloseDate);
                    if(opp.CloseDate  >=  q.Start_Date__c  && opp.CloseDate <= q.End_Date__c && q.Assign_to_User__c == q.OwnerId){
                        oppList.add(opp);
                        oppIds.add(opp.id);
                    } 
                }                
            }
        }
        for(aggregateresult ag : [SELECT SUM(Amount) sum FROM Opportunity where ID =:oppIds]){
            system.debug('oppList-->'+double.valueof(ag.get('SUM')));
            for(quota__c q : quotaList1){
                if(q.Actual_Amount__c != double.valueof(ag.get('SUM'))){
                    q.Actual_Amount__c = double.valueof(ag.get('SUM'));
                    qutoaList.add(q);
                   
                }
            }
        }
        system.debug('oppList-->'+oppList.size());
        return null;
    }
    
    public class FutureClassLimitsTest {

    @future(callout=true)
    public static void docallouts(){
  
        system.debug('qutoaList-->'+qutoaList.size());
        if(qutoaList.size()>0){
            
            update qutoaList;
        }
        
    }
}
User-added image
Best Answer chosen by Shruthi MN 19
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
You are getting this error because qutoaList is not a static variable.Static method can’t access the instance member variable values of its class.
Change variable qutoaList in line 3 to static variable
public static list<quota__c> qutoaList{get;set;}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi shruti,
Static methods are not allowed inside inner classes.tTry refactoring your code by moving your method docallouts() inside listopponquota class. 

Please refer below link which might help you in this
https://salesforce.stackexchange.com/questions/135865/only-top-level-class-methods-can-be-declared-static

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Shruthi MN 19Shruthi MN 19
can you edit the same in the code
Shruthi MN 19Shruthi MN 19
I am nit able to understand
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Try this way
public with sharing class listopponquota {
     public list<Opportunity> oppList{get;set;}
    public list<quota__c> qutoaList{get;set;}
    public set<ID> oppIds = new set<ID>(); 
 
public void listopponquota()
    {
        String selectedVal ='';
        oppList = new list<Opportunity>();
        qutoaList = new list<quota__c>();
        oppList();
    }
    
    
    
    public pageReference oppList() {
        Id qId = (Id) ApexPages.currentPage().getParameters().get('id');
        system.debug('Id-->'+qId);    
        List<quota__c> quotaList1 = new List<quota__c>();
        List<Opportunity> oppList1 = new List<Opportunity>();
        
        quotaList1 =[select Id, Name,Start_Date__c,End_Date__c,Assign_to_User__c,Actual_Amount__c, OwnerId from quota__c where Id   =: qId Limit 1];
        oppList1 = [Select Id, Name, CloseDate, StageName,OwnerId,Amount From Opportunity where StageName = 'Closed Won'];
        
        system.debug('quotaList1-->'+quotaList1);
        if(quotaList1.size() > 0){
            for(quota__c q : quotaList1){
                
                for(Opportunity opp : oppList1){
                    system.debug('closedDate-->'+opp.CloseDate);
                    if(opp.CloseDate  >=  q.Start_Date__c  && opp.CloseDate <= q.End_Date__c && q.Assign_to_User__c == q.OwnerId){
                        oppList.add(opp);
                        oppIds.add(opp.id);
                    } 
                }                
            }
        }
        for(aggregateresult ag : [SELECT SUM(Amount) sum FROM Opportunity where ID =:oppIds]){
            system.debug('oppList-->'+double.valueof(ag.get('SUM')));
            for(quota__c q : quotaList1){
                if(q.Actual_Amount__c != double.valueof(ag.get('SUM'))){
                    q.Actual_Amount__c = double.valueof(ag.get('SUM'));
                    qutoaList.add(q);
                   
                }
            }
        }
        system.debug('oppList-->'+oppList.size());
        return null;
    }
     @future(callout=true)
    public static void docallouts(){
  
        system.debug('qutoaList-->'+qutoaList.size());
        if(qutoaList.size()>0){
            
            update qutoaList;
        }
        
    }

   
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Shruthi MN 19Shruthi MN 19
User-added image
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
You are getting this error because qutoaList is not a static variable.Static method can’t access the instance member variable values of its class.
Change variable qutoaList in line 3 to static variable
public static list<quota__c> qutoaList{get;set;}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Shruthi MN 19Shruthi MN 19
User-added image
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi shruti,
Please verify or check your code before posting your error here.
Your method name is docallouts but your are using docallout in vfpage.That is why you are getting this error.Change method name in vf page.

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Shruthi MN 19Shruthi MN 19


Thank you so muck devi code is working fine but inspite of using future meyhord why ma I getting below error





User-added image

 
Shruthi MN 19Shruthi MN 19
I have used future to avoid the above error but still i am getting this error can you explain the reason
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
This error might be because you are trying to update the same record which is being modified by your class.
Shruthi MN 19Shruthi MN 19
but unitl I edit the page or refresh the page the amount is not getting updated is there any other way