0

When i run the test class in Apex Test Execution,i get error for the controller as

Error Message   System.QueryException: List has no rows for assignment to SObject

Stack Trace Class.Test_ApprovalProcessLeadController.sapld1: line 25, column 1

Error line is :user u =[select id,Profile_Name__c from user WHERE Profile_Name__c='Custom Partner Community user' limit 1];

Similarly for the test class2.

Test CLass :

    @isTest
public class Test_ApprovalProcessLeadController {
   Public static testMethod void sapld() {

        user u =[select id,Profile_Name__c from user  limit 1]; 
        lead l = new lead();
        l.lastname='Test';
        l.company='test';
        l.email='[email protected]';       
        l.Approval_flag__c=false; 
        l.Rep_Managing_Partner__c= u.id;
        l.Approval_Status__c='Pending Approval';   
        insert l;
         System.AssertEquals(l.Id!=null,True);
         ApexPages.CurrentPage().getparameters().put('id', l.id);
         ApexPages.StandardController controller = new ApexPages.StandardController(l);         
         ApprovalProcessLeadController a = new ApprovalProcessLeadController(controller);
            test.startTest();
            a.getdetails();
            a.Redirect();
            test.stoptest();
        }

        Public static testMethod void sapld1() 
        {

         Account acc11 = new Account(Name='Testing SubscriptionClone', BillingStreet='Banjara hills', BillingCity='Hyd', BillingState='TS',
                                    BillingPostalCode = '500084', BillingCountry = 'India', Phone = '100', Industry = 'Banking',
                                    Type = 'Paid', Customer_Type__c = 'Customer', Customer_List__c = true);
        insert acc11 ;
        contact cc11 = new contact(FirstName ='Test Contact Subscription', LastName ='Opptyclone', Role__c='Subscription Administrator',AccountId=acc11.Id);
        insert cc11;
        Profile p = [SELECT id ,Name FROM Profile WHERE Name='Custom Partner Community user'];

        User u = new User(
            FirstName = 'test', 
            LastName= 'last_test', 
            Email='[email protected]', 
            Phone='111-111-1111', 
            //True_Market_Unit__c='1111', 
            alias = 'test', 
            EmailEncodingKey='UTF-8',
            ProfileId = p.Id,
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US', 
            TimeZoneSidKey='America/Los_Angeles',
            UserName = '[email protected]'
            );
         u.ContactId=cc11.Id;
        insert u;


       // user u =[select id,Profile_Name__c from user WHERE Profile_Name__c='Custom Partner Community user' limit 1];
        System.AssertEquals(u.Id!=null,True);                 
        lead l = new lead();
        l.lastname='Test';
        l.company='test';
        l.email='[email protected]';
        l.Approval_flag__c=false;
        l.Product_Interest__c='Services';
        l.Rep_Managing_Partner__c = u.id;
        l.Approval_Status__c='Pending Approval';                      
        insert l;
         ApexPages.CurrentPage().getparameters().put('id', l.id);
         ApexPages.StandardController controller = new ApexPages.StandardController(l);          
         ApprovalProcessLeadController a = new ApprovalProcessLeadController(controller);
         test.startTest();
         a.getdetails();
         a.Redirect();
         test.stoptest();
        } 

        Public static testMethod void sapld2() 
        {
        Account acc11 = new Account(Name='Testing SubscriptionClone', BillingStreet='Banjara hills', BillingCity='Hyd', BillingState='TS',
                                    BillingPostalCode = '500084', BillingCountry = 'India', Phone = '100', Industry = 'Banking',
                                    Type = 'Paid', Customer_Type__c = 'Customer', Customer_List__c = true);
        insert acc11 ;
        contact cc11 = new contact(FirstName ='Test Contact Subscription', LastName ='Opptyclone', Role__c='Subscription Administrator',AccountId=acc11.Id);
        insert cc11;
        Profile p = [SELECT id ,Name FROM Profile WHERE Name='Custom Partner Community user'];

        User u = new User(
            FirstName = 'test', 
            LastName= 'last_test', 
            Email='[email protected]', 
            Phone='111-111-1111', 
            //True_Market_Unit__c='1111', 
            alias = 'test', 
            EmailEncodingKey='UTF-8',
            ProfileId = p.Id,
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US', 
            TimeZoneSidKey='America/Los_Angeles',
            UserName = '[email protected]'
            );
         u.ContactId=cc11.Id;
        insert u;

       // user u =[select id,Profile_Name__c from user WHERE Profile_Name__c='Custom Partner Community user' limit 1];
        System.AssertEquals(u.Id!=null,True);       
        lead l = new lead();
        l.lastname='Test';
        l.company='test';
        l.email='[email protected]';
        l.Approval_flag__c=false;
        l.Product_Interest__c='Services';      
        l.Rep_Managing_Partner__c = u.id;                    
        insert l;
         ApexPages.CurrentPage().getparameters().put('id', l.id);
         ApexPages.StandardController controller = new ApexPages.StandardController(l);          
         ApprovalProcessLeadController a = new ApprovalProcessLeadController(controller);
         test.startTest();
         a.getdetails();
         a.Redirect();
         test.stoptest();
        }   

    }

Controller :

public with sharing  class ApprovalProcessLeadController{
    Public id getid;
    Public id userid;
    public boolean flag1{get;set;}
    Public Boolean showvalues{get;set;}
    Public Lead getdata{get;set;}

    list<Lead> getLeaddetails= new list<Lead>();

        public ApprovalProcessLeadController(ApexPages.StandardController stdController){

        getdata = new Lead();
         getid = apexpages.currentpage().getParameters().get('id');
           showvalues =False;
             system.debug('ajitava133'+getid );
        }      
        public void getdetails(){
         getdata= [select id,Product_Interest__c,Total_of_Databases_in_your_Org__c,Rep_Managing_Partner__c,Approval_Status__c,CheckCreatedByProfile1__c,Approval_flag__c from Lead where id =: getid limit 1];
         System.debug('@@@@@getdata'+getdata);
            if( getdata!=null)
            {
                system.debug('@@@@@@getdata.Rep_Managing_Partner__c'+getdata.Rep_Managing_Partner__c);
                if(getdata.Rep_Managing_Partner__c !=null)
                {                                                                        
                    if(getdata.Product_Interest__c== null)
                    {
                        showvalues =True;    
                    }                                       
                  else if(getdata.Approval_Status__c == 'Pending Approval')                   
                   {
                       ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Approval request is already submitted/Approved '));
                   }

                   else if(getdata.Approval_flag__c!=true && getdata.CheckCreatedByProfile1__c==true || (getdata.Approval_Status__c =='Rejected'))
                   {
                       userid = getdata.Rep_Managing_Partner__c;
                       Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                       req1.setComments('Submitting request for approval.');
                       req1.setObjectId(getid);
                       req1.setNextApproverIds(new List<Id>{userid });
                       Approval.ProcessResult result = Approval.process(req1);
                       flag1=true;
                   }
                   else                   
                   {                                   
                   if(getdata.Approval_flag__c==true)
                         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Approval request is already submitted/Approved '));
                         if(getdata.CheckCreatedByProfile1__c==false)

                               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,' You dont have access to submit this lead for apporval'));                  
                   }
                }               
                else
                {               
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,' Need to enter the Rep Managing Partner in order to send approval'));                                
                }
            }

        }
      public pagereference Redirect(){

         String newPageUrl = '/'+getdata.id;
         PageReference newPage = new PageReference(newPageUrl);
            newPage.setRedirect(true);

            return newPage;                  

            }
}

In Developer Console ..the line which are not covered are

  public boolean flag1{get;set;}
if(getdata.Product_Interest__c== null)
                    {
                        showvalues =True;    
                    }                                       
                  else if(getdata.Approval_Status__c == 'Pending Approval')                   
                   {
                       ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Approval request is already submitted/Approved '));
                   }

                   else if(getdata.Approval_flag__c!=true && getdata.CheckCreatedByProfile1__c==true || (getdata.Approval_Status__c =='Rejected'))
                   {
                       userid = getdata.Rep_Managing_Partner__c;
                       Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                       req1.setComments('Submitting request for approval.');
                       req1.setObjectId(getid);
                       req1.setNextApproverIds(new List<Id>{userid });
                       Approval.ProcessResult result = Approval.process(req1);
                       flag1=true;
                   }
                   else                   
                   {                                   
                   if(getdata.Approval_flag__c==true)
                         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Approval request is already submitted/Approved '));
                         if(getdata.CheckCreatedByProfile1__c==false)

                               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,' You dont have access to submit this lead for apporval'));                  
                   }

Any help very much appreciated.

2
  • Your query is not returning any records.. You need create a user with Profile_Name__c='Custom Partner Community user'
    – Ratan Paul
    Mar 1, 2016 at 7:38
  • @Ratan,@Prady,@Oles Malkov:Thanks for the response.The test methods got passed.By doing an insertion of account and contact .But now the code coverage remains constant .Any help very much appreciated. Mar 1, 2016 at 9:53

1 Answer 1

1

That error is because your query is not returning any records. You have to create that user first. I assume that Profile_Name__c is a formula field that displays the Name of assigned profile. If so, you can add this method to your test class:

@testSetup 
static void setupUser() {
    Profile p = [SELECT Id FROM Profile WHERE Name = 'Custom Partner Community user'];

    User u = new User(LastName = 'testUser', Email = '[email protected]', ProfileId = p.Id, 
                  Alias = 'test', Username = '[email protected]', LocaleSidKey = 'en_US',
                  TimeZoneSidKey = 'America/Los_Angeles', EmailEncodingKey = 'UTF-8', 
                  LanguageLocaleKey = 'en_US');
    insert u;
}
2
  • :Thanks for your response.When i added this in test class.it throws an error as System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Cannot create a portal user without contact: [ContactId] Mar 1, 2016 at 9:28
  • Looks like this is a portal user, can you please check this link it is well explained there how to create a portal user salesforce.stackexchange.com/questions/65804/… Mar 1, 2016 at 9:51

You must log in to answer this question.

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