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
Abc234Abc234 

test Class

Class :

public with sharing class ApexButtonGetDist{
    public static Opportunity theOpportunity = new Opportunity();
    public static Opportunity theOpportunity2 = new Opportunity();
    public static Account getDistributor = new Account();
   
    public ApexButtonGetDist(ApexPages.StandardController stdController){
        theOpportunity = (Opportunity)stdController.getRecord();  
    }
   
    public PageReference dologic() {
        theOpportunity2 = [SELECT ID, Name, customfiled__c from Opportunity where Id = :theOpportunity.Id];

        getDistributor = [SELECT ID, Name, BillingStreet,BillingState,BillingCity,BillingPostalCode from Account where Id = :theOpportunity2.customfiled__c];
        theOpportunity.customfiled1__c = getDistributor.BillingStreet;
        theOpportunity.customfiled2__c = getDistributor.BillingCity;
        theOpportunity.customfiled3__c= getDistributor.BillingState;
        theOpportunity.customfiled4__c = getDistributor.BillingPostalCode;       
        update theOpportunity;
        return new PageReference('/' + theOpportunity.ID);
    }
}

Test Class:

@isTest

private class ApexButtonGetDistTest {
      
        static testmethod void testApexButtonGetDist(){
        
        opportunity opps = new opportunity();
            
            Opportunity opp = new Opportunity();
            opp.Name = 'Test opp';
            opp.StageName = 'Test Stage';
            opp.CloseDate = date.today();
            insert opp;
            
            PageReference pr = new PageReference(opp.ID);
            Test.setCurrentPage(pr);
            
            ApexPages.StandardController controller = new ApexPages.StandardController(opps); 
            
            Test.startTest();
            
            ApexButtonGetDist ABG = new ApexButtonGetDist(controller);
            
            Test.stopTest();
 
           
            
 
 
        
        }           
}

bob_buzzardbob_buzzard

You haven't posted a question - were you just showing us your code or is there something you would like help with ?

Abc234Abc234

I am having trouble to get the test coverage for the doMyApexLogic()

 

Not sure, how to test the  

 

public PageReference doMyApexLogic() {
        theOpportunity2 = [SELECT ID, Name, Distributor_Name__c from Opportunity where Id = :theOpportunity.Id];

        getDistributor = [SELECT ID, Name, BillingStreet,BillingState,BillingCity,BillingPostalCode from Account where Id = :theOpportunity2.Distributor_Name__c];
        theOpportunity.Dist_street__c = getDistributor.BillingStreet;
      theOpportunity.Dist_City__c = getDistributor.BillingCity;
      theOpportunity.Dist_State__c = getDistributor.BillingState;
      theOpportunity.Dist_PostalCode__c = getDistributor.BillingPostalCode;        
        update theOpportunity;
        return new PageReference('/' + theOpportunity.ID);
    }
}

 

When use ABG.doMyApexLogic(); it's throughing error "LIST has no records" but not sure how to pass the values to the this method.

 

Can you point me to where I am doing wrong?

 

Thanks,

bob_buzzardbob_buzzard

You are passing a new instance of an opportunity to the controller, so this won't have an id for your query in the constructor.