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 

Test class to add all opp amount

I have written a test class to add all amount on quota which lies between start and end date of quota

I am getting below error

User-added image

Test class
@isTest
public class Test_listopponquota {
    static testMethod void listopponquota(){
        
        Quota__c a = new  Quota__c(Name = 'Test');
        a.Assign_to_User__c = 'Shruthi MN';
        a.Start_Date__c = date.today();
         a.Start_Date__c = date.today() + 12;
        insert a;
        
        
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
    }
   
}
apex
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 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.enqueueJob(q);
                }
            }
        }
        system.debug('oppList-->'+oppList.size());
        return null;
    }
    
    public void doCallout(){
        system.debug('qutoaList-->'+qutoaList.size());
        if(qutoaList.size()>0){
            update qutoaList;
        }
        
    }
}

Vf

<apex:page controller="listopponquota" lightningStylesheets="true" action="{!doCallout}">
    <apex:form > 
        <apex:pageBlock rendered="true" >
            <apex:pageBlockTable value="{!oppList}" var="v">
                <apex:column value="{!v.OwnerId}"/>
                
                <apex:column headerValue="Opportunity Name">
                    <apex:outputlink value="/{!v.Id}">{!v.Name}</apex:outputlink>
                </apex:column>
                <apex:column value="{!v.CloseDate}"/>
                <apex:column value="{!v.StageName}"/>
                <apex:column value="{!v.Amount}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form> 
</apex:page
Uttpal_ChandraUttpal_Chandra
Hi Shruthi,

Kindly update your test class with this code
 
@isTest
public class Test_listopponquota {
    static testMethod void listopponquota(){

Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
         
        User usr = new User(LastName = 'LIVESTON',
                           FirstName='JASON',
                           Alias = 'jliv',
                           Email = 'abc@gmail.com',
                           Username = 'abc@asdf.com',
                           ProfileId = profileId.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                          	LocaleSidKey = 'en_US'
                           );

        
        Quota__c a = new  Quota__c(Name = 'Test');
        a.Assign_to_User__c = usr.id;
        a.Start_Date__c = date.today();
         a.Start_Date__c = date.today() + 12;
        insert a;
        
        
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
    }
   
}

 
Shruthi MN 19Shruthi MN 19
Hi Uttpal

The code is working fine but code coverage is 0
Shruthi MN 19Shruthi MN 19
Hi Uttpal

The code is working fine but code coverage is 0