You need to sign in to do that
Don't have an account?
Staci
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();
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
Try using List<Change__c> caseList = [Select... Then, Change__c currCase = caseList[0];
Hope this helps!
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)
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 .
Here's all of the code if that helps:
I revised your code:
Try that...
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?
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
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
}