1

I'm blocked in creating a Record for PartnerNetworkConnection.I'm getting an error like

QueryException: List has no rows for assignment to SObject. Stack Trace: Class.caseForward.forward: line 30, column 1 Class.caseForwardTest.testMethod1: line 26, column 1

In apex class:

I'm getting query exception in below line

PartnerNetworkConnection con = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionName='Penson Worldwide' and ConnectionStatus='Accepted' limit 1];

Not sure how to cover this in the test class.Can someone help me on this asap.

Thanks in advance....

Apex class:

public class caseForward{

    public Case caseObj{get; set;}
    public Case currentCase{get;set;}

    public caseForward(ApexPages.StandardController controller) {
        Id caseId = ApexPages.currentPage().getParameters().get('id');
        currentCase = [SELECT Initial_Request_to_Penson__c FROM case WHERE id = :caseId];
        caseObj=(Case) controller.getRecord();
    }

    public PageReference forward(){ 

        if (currentCase.Initial_Request_to_Penson__c == null || currentCase.Initial_Request_to_Penson__c.trim().length() == 0)
        {
            system.debug('Initial_Request_to_Penson__c::::::'+currentCase.Initial_Request_to_Penson__c);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Initial Request to Penson" field is required!'));
            return null;
        }

        //Case currentCase;

        Savepoint sp = Database.setSavepoint();

        try
        {           
            PartnerNetworkConnection con = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionName='Penson Worldwide' and ConnectionStatus='Accepted' limit 1];
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
            newrecord.ConnectionId = con.Id;
            newrecord.LocalRecordId = caseObj.Id;  
            newrecord.RelatedRecords = 'CaseComment,Attachment';
            newrecord.SendClosedTasks = true;
            newrecord.SendOpenTasks = true;
            newrecord.SendEmails = true;   
            System.Debug('@@@@@@ 1'+newrecord.id);
            insert newrecord;


            System.Debug('@@@@@@@ 2'+newrecord.id);        
            caseObj.Status ='Sent To Penson';
            caseObj.Sent_To_Penson__c = true;
            update caseObj;
            System.Debug(+newrecord.id);
            Pagereference casePage=new Pagereference ('/'+caseObj.id+'?error=1');
            return casePage;
        }
        Catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Unable to forward case.  Error: ' + e.getMessage()));
            throw e;    
            Database.rollback(sp);
            return null;
        }
  }
}

Test Class:

@isTest (seealldata = true)
private class caseForwardTest {

    private static testMethod void testMethod1() {

        //Case
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        insert cas;

        Test.startTest();

        PageReference pageRef = Page.CaseForwordPage;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('Id',cas.Id);

        caseForward objcaseForward = new caseForward(new ApexPages.StandardController(cas));        
        objcaseForward.forward();

        cas.Initial_Request_to_Penson__c = 'This is a test class request to pension'; 
        update cas;

        objcaseForward = new caseForward(new ApexPages.StandardController(cas));        
        objcaseForward.forward();       

        Test.stopTest();
    }
}
  • are you getting error during deployment or in normal execution? – Santanu Boral Jan 10 '17 at 22:28
  • 1
    have a look into this ans salesforce.stackexchange.com/questions/98885/… – Santanu Boral Jan 10 '17 at 22:35
  • 1
    PartnerNetworkConnection can't be mocked by DML, it is part of org data (and hence seeAlldata=true is unnecessary). This means in every environment there has to be a valid PNC given the way you coded this. The answer that @SantanuBoral pointed to is one way of addressing this – cropredy Jan 10 '17 at 22:51
  • @SantanuBoral When I'm executing my testclass I'm getting this error and my test's are failing with the Queryexception when I'm retrieving PartnerNetworkConnection – Sammy Jan 10 '17 at 23:00
0

You are getting QueryException as the query doesn't return a record and you are trying to assign directly to the object.

To eliminate this issue do this changes before using PartnerNetworkConnection object:

PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
            PartnerNetworkConnection con = new PartnerNetworkConnection();

            if(!Test.isRunningTest())
            {
                List<PartnerNetworkConnection>  lstCon = [select Id, ConnectionStatus, ConnectionName 
                    from PartnerNetworkConnection 
                    where ConnectionName='Penson Worldwide' 
                    and ConnectionStatus='Accepted' 
                    limit 1]; 
                    if(!lstCon.isEmpty())
                    {
                        con = lstCon[0];
                        newrecord.ConnectionId = con.Id;
                        newrecord.LocalRecordId = caseObj.Id;  
                        newrecord.RelatedRecords = 'CaseComment,Attachment';
                        newrecord.SendClosedTasks = true;
                        newrecord.SendOpenTasks = true;
                        newrecord.SendEmails = true;   
                        System.Debug('@@@@@@ 1'+newrecord.id);
                        insert newrecord;
                        System.Debug('@@@@@@@ 2'+newrecord.id);   
                    }

            }

            caseObj.Status ='Sent To Penson';
            caseObj.Sent_To_Penson__c = true;
            update caseObj;
            System.Debug(+newrecord.id);
            Pagereference casePage=new Pagereference ('/'+caseObj.id+'?error=1');
            return casePage;

Secondly, DML operation is not allowed on PartnerNetworkConnection object. So we cannot mock PartnerNetworkConnection record.

Also using Test.isRunningTest() in apex class will help you which piece of code to be executed during testing or in normal execution.

I have tried to show an example, you can change at your own purpose.

  • You can first close this question and ask a new one – Santanu Boral Jan 11 '17 at 2:37
  • I have updated with above lines but now I have less code coverage below the threshold so how to overcome with this issue? – Sammy Jan 11 '17 at 2:43
  • You just create the record at your org to satisfy the SOQL query – Santanu Boral Jan 11 '17 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.