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
StaciStaci 

List has more than 1 row for assignment to SObject

i have a button that calls apex to create a change record from a case.  See code snippit below.  I only get this error when AccountId and Dealer__c have the same value, but it is possible that these 2 can be the same.  I need it to not worry about that part.  How do I get around that?

public class SampleCaseButtonController {    Case currCase;    public SampleCaseButtonController(ApexPages.StandardController controller) {        currCase = [Select AccountId, ContactId, Dealer__c, Product__c, Support_Advocate__c From Case Where Id = :controller.getId() LIMIT 1];    }        public PageReference changeCase(){            // Create a new Change Record        Change__c newChange = new Change__c();

 

Best Answer chosen by Admin (Salesforce Developers) 
izayizay

Hi Staci,

 

In the query in line 28 you are getting more than one user record with the same ContactId. You can limit the number of records return by adding LIMIT 1 at the end of the query like this:

 

newChange.Contact_Approver2__c = [Select id From User Where ContactId = :tmpId LIMIT 1].Id;

 

Or you can query to a list and then based in the returned records you do the assignment like this.

 

List<User> approvers = [SELECT Id, ContactId FROM User WHERE ContactId = tmpId];

if(approvers.size() > 0){

    newChange.Contact_Approver2__c = approvers[0].Id;

}else{

    //Handle what happends if no user is returned

}

All Answers

izayizay

Try using List<Change__c> caseList = [Select... Then, Change__c  currCase = caseList[0];

 

Hope this helps!

StaciStaci

Like this?

 

I wasn't sure if I was replacing or adding, I replaced.  It doesn't work, it says currCase.AccountId doesn't exist (the last line here)

public class SampleCaseButtonController {
public SampleCaseButtonController(ApexPages.StandardController controller) {

List<Case> caseList = [Select AccountId, ContactId, Dealer__c, Product__c, Support_Advocate__c From Case Where Id = :controller.getId() LIMIT 1];
Case currCase = caseList[0];
}

public PageReference changeCase(){

// Create a new Change Record
Change__c newChange = new Change__c();



// Fill in the values for the new record



newChange.Account_Lookup__c = currCase.AccountId;

 

 

SurpriseSurprise

So u have lookup relationship between change and case object . If that is correct ,shouldn't you be assigning the id of the case to the field on the change object instead of assigning  accountid to the field on the case object .

 

 

 

 

 

 

StaciStaci

Here's all of the code if that helps:

public class SampleCaseButtonController {
 
    

    public SampleCaseButtonController(ApexPages.StandardController controller) {
       
        List<Case> caseList = [Select AccountId, ContactId, Dealer__c, Product__c, Support_Advocate__c From Case Where Id = :controller.getId() LIMIT 1];
        Case currCase = caseList[0];
    }
    
    public PageReference changeCase(){
    
        // Create a new Change Record
        Change__c newChange = new Change__c();
        
                
              
        // Fill in the values for the new record
      
        
       
        newChange.Account_Lookup__c = currCase.AccountId;
        
        // We put the case contact id field into a string because in the SOQL query,
        // we cannot compare the User ContactId field to an ID field directly. Doing so will
        // throw an "Invalid assignment of Schema.Sobjectfield to Id" error.  Exact reason
        // unknown but likely related to the special construction of the Portal fields in the 
        // User object.
        String tmpId = currCase.ContactId;
        newChange.Contact_Approver2__c  = [Select id From User Where ContactId = :tmpId].Id;
        newChange.Dealer_Account4__c    = currCase.Dealer__c;
        newChange.ProductChange__c      = currCase.Product__c;
        newChange.Support_Advocate1__c  = currCase.Support_Advocate__c;       
              
        
        // Insert the change record 
        Database.insert(newChange);
        
          //create a new Case Change Association
        Case_Change_Association__c newCCA = new Case_Change_Association__c();
         // Fill in the values for the new record
         newCCA.Case__c = currCase.Id;
         newCCA.Change__c = newChange.id;
         
        //Insert the new Case Change Association record
        Database.insert (newCCA);
            
        PageReference retPage = new PageReference('/' + newChange.id + '/e');
        return retPage;
    
    }

}

 

izayizay

I revised your code:

 

public class SampleCaseButtonController {
    //Create a variable to hold the case that is accessible from the rest of the class
    private Case currCase = new Case();

    public SampleCaseButtonController(ApexPages.StandardController controller) {
       
        List<Case> caseList = [Select AccountId, ContactId, Dealer__c, Product__c, Support_Advocate__c From Case Where Id = :controller.getId() LIMIT 1];
        //If cases are returned from the query
        if(caseList.size() > 0){
           currCase = caseList[0];
        }
    }
    
    public PageReference changeCase(){
    
        // Create a new Change Record
        Change__c newChange = new Change__c();
          
        // Fill in the values for the new record
        newChange.Account_Lookup__c = currCase.AccountId;
        
        // We put the case contact id field into a string because in the SOQL query,
        // we cannot compare the User ContactId field to an ID field directly. Doing so will
        // throw an "Invalid assignment of Schema.Sobjectfield to Id" error.  Exact reason
        // unknown but likely related to the special construction of the Portal fields in the 
        // User object.
        String tmpId = currCase.ContactId;
        newChange.Contact_Approver2__c  = [Select id From User Where ContactId = :tmpId].Id;
        newChange.Dealer_Account4__c    = currCase.Dealer__c;
        newChange.ProductChange__c      = currCase.Product__c;
        newChange.Support_Advocate1__c  = currCase.Support_Advocate__c;       
              
        
        // Insert the change record 
        Database.insert(newChange);
        
          //create a new Case Change Association
        Case_Change_Association__c newCCA = new Case_Change_Association__c();
         // Fill in the values for the new record
         newCCA.Case__c = currCase.Id;
         newCCA.Change__c = newChange.id;
         
        //Insert the new Case Change Association record
        Database.insert (newCCA);
            
        PageReference retPage = new PageReference('/' + newChange.id + '/e');
        return retPage;
    
    }

}

 Try that...

StaciStaci

Thanks izay

 

I used your code and now its throwing that error no matter if the account and dealer are the same or different.  Any ideas?

StaciStaci
Actually here's the error from the email:
Sandbox

Apex script unhandled exception by user/organization: 005a0000007VK2P/00Df0000002Fb6V
Source organization: 00D30000000YC8U (null)
Visualforce Page: /apex/CasetoChange



caused by: System.QueryException: List has more than 1 row for assignment to SObject

Class.SampleCaseButtonController.changeCase: line 28, column 1
izayizay

Hi Staci,

 

In the query in line 28 you are getting more than one user record with the same ContactId. You can limit the number of records return by adding LIMIT 1 at the end of the query like this:

 

newChange.Contact_Approver2__c = [Select id From User Where ContactId = :tmpId LIMIT 1].Id;

 

Or you can query to a list and then based in the returned records you do the assignment like this.

 

List<User> approvers = [SELECT Id, ContactId FROM User WHERE ContactId = tmpId];

if(approvers.size() > 0){

    newChange.Contact_Approver2__c = approvers[0].Id;

}else{

    //Handle what happends if no user is returned

}

This was selected as the best answer
StaciStaci
the LIMIT 1 worked! Thank you soooooo much!