I am getting the above error when I attempt to preview the page

Page code

<apex:page standardController="Case" extensions="CaseOrg2OrgExtension" sidebar="true" showHeader="true" >
<apex:sectionHeader title="Forward this Case" subtitle="{!caseRec.CaseNumber}"/>

<apex:pageMessages />

<apex:form >
    <apex:pageBlock title="" mode="detail">
        <apex:pageBlockButtons location="top">
            <apex:commandButton action="{!shareCase}" value="Save" rendered="{!canShare}"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>

        <apex:pageBlockSection columns="1" title="Active Shares" rendered="{!hasActiveShares}">
            <apex:pageBlockTable value="{!activeShares}" var="share">
                <apex:column headerValue="Connection">
                    <apex:outputText value="{!share.name}"/>
                <apex:column headerValue="Status">
                    <apex:outputText value="{!share.status}"/>
                <apex:column headerValue="Shared Date">
                    <apex:outputText value="{!share.activeDate}"/>

        <apex:pageBlockSection columns="1" title="New Shares" rendered="{!canShare}">
             <apex:selectCheckboxes value="{!selectedConnections}" layout="pageDirection" legendText="Select the connection(s) that may accept this record.">
                <apex:selectOptions value="{!activeConnections}"/>

             <apex:selectCheckboxes value="{!selectedRelatedRecords}" layout="pageDirection" legendText="Include existing related record(s).">
                <apex:selectOptions value="{!relatedRecords}"/>



public with sharing class CaseOrg2OrgExtension {
// http://stevefouracre.blogspot.com/2011/10/setting-up-salesforce-to-salesforce.html

private ApexPages.StandardController controller;

private static final String RELATED_ATTACHMENTS = 'Attachments';
private static final String SHARE_STATUS_SENT = 'Sent';

public Case caseRec {get;set;}
public Boolean canShare {get;set;}
public Boolean hasActiveShares {get;set;}
public Id[] selectedConnections {get;set;}
public List<ActiveShare> activeShares {get;set;}
public List<SelectOption> activeConnections {get;set;}
public String[] selectedRelatedRecords {get;set;}
public List<SelectOption> relatedRecords {get;set;}

public class ActiveShare {
    public String name {get;set;}
    public String status {get;set;}
    public String activeDate {get;set;}

public CaseOrg2OrgExtension(ApexPages.StandardController stdController) {
    try {
        controller = stdController;
        canShare = true;

        caseRec = [select Id, CaseNumber, Transfer_To_Unit__c from Case];

        // case validations
        if (String.isBlank(caseRec.Transfer_To_Unit__c)) {
            throw new myException('The Transfer To Unit field must be populated before you can forward this Case.');

        // get active connections
        List<PartnerNetworkConnection> activeConnects = [select Id, AccountId, ContactId, PrimaryContactId, ConnectionName, ConnectionStatus, ResponseDate
            from PartnerNetworkConnection where ConnectionStatus = 'Accepted' limit 100];

        // validate there is at least 1 active connection
        if (activeConnects.size() == 0) {
           throw new myException('There are no Active Connections to other Salesforce orgs.');

        Map<Id,String> activeConnectionIdAndName = new Map<Id,String>();
        for (PartnerNetworkConnection c : activeConnects) {
            activeConnectionIdAndName.put(c.Id, c.ConnectionName);

        // look to see if the record is already shared with any connections
        List<PartnerNetworkRecordConnection> activePartnerNetworkRecordConnection = [select Id, ConnectionId, LocalRecordId, StartDate, Status from PartnerNetworkRecordConnection
            where LocalRecordId = :controller.getId() and ConnectionId in :activeConnectionIdAndName.keySet() and Status = :SHARE_STATUS_SENT];

        activeShares = new List<ActiveShare>();

        Set<Id> connectionsAlreadySharedWithCase = new Set<Id>();
        if (activePartnerNetworkRecordConnection.size() > 0) {
            hasActiveShares = true;
            for (PartnerNetworkRecordConnection pnrc : activePartnerNetworkRecordConnection) {
                ActiveShare activeShare = new ActiveShare();
                activeShare.name = activeConnectionIdAndName.get(pnrc.ConnectionId);
                activeShare.status = pnrc.Status;
                activeShare.activeDate = pnrc.StartDate.format();

        // build active Connections - exclude any active Connections if the Case is already shared with it
        activeConnections = new List<SelectOption>();
        for (PartnerNetworkConnection c : activeConnects) {
            System.debug(LoggingLevel.INFO, 'connection = ' + c);
            if (!connectionsAlreadySharedWithCase.contains(c.Id)) {
                activeConnections.add(new SelectOption(c.Id, c.ConnectionName));
        // default to only connection?
        if (activeConnections.size() == 0) {
            canShare = false;
        } else if (activeConnections.size() == 1) {
            selectedConnections = new Id[]{activeConnects[0].Id};
        } else {
            selectedConnections = new Id[]{};

        relatedRecords = new List<SelectOption>();
        // default to have Attachments checked?
        relatedRecords.add(new SelectOption(RELATED_ATTACHMENTS, RELATED_ATTACHMENTS));
        selectedRelatedRecords = new String[]{RELATED_ATTACHMENTS};

    } catch (Exception e) {
        System.debug(LoggingLevel.ERROR, 'e.getStackTraceString() = ' + e.getStackTraceString());
        canShare = false;

public PageReference shareCase() {
    try {
        // validate a connection is selected
        if (selectedConnections.size() == 0) {
            throw new myException('You must select at least 1 connection to forward the Case.');

        Boolean shareAttachments = false;
        for (String srr : selectedRelatedRecords) {
            if (srr == RELATED_ATTACHMENTS) {
                shareAttachments = true;

        List<PartnerNetworkRecordConnection> forwards = new List<PartnerNetworkRecordConnection>();
        for (Id id : selectedConnections) {
            PartnerNetworkRecordConnection forward = new PartnerNetworkRecordConnection();
            forward.ConnectionId = id;
            forward.LocalRecordId = controller.getId();
            forward.SendClosedTasks = true;
            forward.SendOpenTasks = true;
            forward.SendEmails = true;
            if (shareAttachments) {
                forward.RelatedRecords = 'Attachment';
        insert forwards;

        PageReference pr = new PageReference('/' + controller.getId());
        return pr;
    } catch (Exception e) {
        System.debug(LoggingLevel.ERROR, 'e.getStackTraceString() = ' + e.getStackTraceString());
    return null;
  • Would be considered best practice to search for an existing answer before posting the question. If those answers do not help then be specific as to why they did not help. This question is widely answered here, on google, on developer.force.com, etc – Eric Sep 3 '15 at 16:39

You have an unbounded query for Case records which is returning more than one case and you are attempting to assign a list of cases to a single Case variable.

Since you're using a standard controller, you should probably leverage the standard controller's .getRecord() method.

Documentation: StandardController Methods

caseRec = [select Id, CaseNumber, Transfer_To_Unit__c from Case];

would be replaced with

caseRec = (Case)controller.getRecord();


It looks like your query is probably returning several results. It then tries to put these results into a single instance of a Case object. Try running this query on it's own to see exactly what is being returned.

public Case caseRec {get;set;}
caseRec = [select Id, CaseNumber, Transfer_To_Unit__c from Case];

You could a) use a collection to store these query results, or b) filter the search by a specific criteria to only get 1 case record for assignment.

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