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();
}
}
PartnerNetworkConnection
can't be mocked by DML, it is part of org data (and henceseeAlldata=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