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
mcareymcarey 

Getting error "List has more than 1 row for assignment to SObject"

Trying to deploy a change set with a trigger and test class.  I successfuly deployed one earlier this week but now with a new trigger I get:

 

TestSalesRegionUpdate.testSalesRegionUpdate() Class 13 15 Failure Message: "System.QueryException: List has more than 1 row for assignment to SObject", Failure Stack Trace: "Class.TestSalesRegionUpdate.testSalesRegionUpdate: line 13, column 15 External entry point"

 

It is referencing the existing test class, not the new one I'm trying upload.  Is there a conflict with multiple test classes?  Any info on what this erro usually means?

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

I would try this:

 

}

@isTest
private class TestSalesRegionUpdate{

static testMethod void testSalesRegionUpdate() {

Opportunity opportunity = new Opportunity(Name = 'Test',OwnerID = '005E0000000d0DU',StageName = 'Pipeline',CloseDate=System.today());
insert opportunity;

Test.startTest();
update opportunity;
Test.stopTest();

opportunity = [Select ID, Sales_Region__c FROM Opportunity Where Name = 'Test' LIMIT 1];

user u = new user[select  Sales_Region__c from user where id = o.ownerid LIMIT 1]);
system.assert(opportunity.Sales_Region__c == u.Sales_Region__c);
}

 

That should be a more dynamic test since it querys for the data from the user object that is being set on the opportunity, because the only reason that I can see at this point for the test to fail is if the Sales region on the Owner of the Opportunity is not 'NA - Great Lakes & Canada'.

All Answers

Jake GmerekJake Gmerek

Usually this would imply that you may have a line like this:

 

my_obj__c o = new my_obj__c([select some_fields from my_obj__c where some condition]);

 

and it is returning more than one record as a result, but apex can only store one result.

NiketNiket

Hi ,

 

Usually it happens when your query is returning more than one record and you are getting it into a single instance of Sobject.

 

If you could paste your code here, I will be able to pin point the exact problem.

 

Cheers !

Niket

Certified Administrator | Certified Developer

MyBlog



mcareymcarey

Code is below.  Since I'm only searching where the name = "Test" and is assigned to one user I wouldn't think it would return more than one record.  Thanks for the help.

 

@isTest
private class TestAcctSalesRegionUpdate{

static testMethod void TestAcctSalesRegionUpdate() {

Account account = new account(Name = 'Test',OwnerID = '005E0000000d0DU');
insert account;

Test.startTest();
update account;
Test.stopTest();

account = [Select ID, Sales_Region__c FROM Account Where Name = 'Test'];
system.assert(account.Sales_Region__c == 'NA - Great Lakes & Canada');
}
}

Jake GmerekJake Gmerek

In order to return only one record you have to explicitly limit the return.  

 

account = [Select ID, Sales_Region__c FROM Account Where Name = 'Test'];

 

becomes

 

account = [Select ID, Sales_Region__c FROM Account Where Name = 'Test' LIMIT 1];

 

and your code should work.

mcareymcarey

Thanks for the reply.  I added it to the class as you suggested but the error was apparently for a test class I already previously uploaded and deployed successfully.  I tried to add the same Limit 1 code to the other class and deploy and got the following error on deployment to prod.

 

Failure Message: "System.AssertException: Assertion Failed", Failure Stack Trace: "Class.TestSalesRegionUpdate.testSalesRegionUpdate: line 14, column 1 External entry point"

 

Below is the code for the other test class.

 

@isTest
private class TestSalesRegionUpdate{

static testMethod void testSalesRegionUpdate() {

Opportunity opportunity = new Opportunity(Name = 'Test',OwnerID = '005E0000000d0DU',StageName = 'Pipeline',CloseDate=System.today());
insert opportunity;

Test.startTest();
update opportunity;
Test.stopTest();

opportunity = [Select ID, Sales_Region__c FROM Opportunity Where Name = 'Test' LIMIT 1];
system.assert(opportunity.Sales_Region__c == 'NA - Great Lakes & Canada');
}
}

Jake GmerekJake Gmerek

Ok we are getting closer.  This error appears to be telling you that this line:

 

system.assert(opportunity.Sales_Region__c == 'NA - Great Lakes & Canada');

 

failed.  Essentially the sales region of the opportunity named test is not getting set to 'NA - Great Lakes & Canada' when you are updating the opportunity.  I am assuming that that is the point of the trigger? that you are testing.  At this point it appears that your test code is fine, but the original code that you are trying to test is failing to do what it was designed to do.  

If you need help troubleshooting that, you can post the relavant code and I will take a look at it.

mcareymcarey

Here is the code.  I just tested this in the sandbox successfully and it also works in prod so I am definitely confused.  What it does is set the custom field "Sales Region" in Opportunity to the owner's custom field for the User object.  Thanks again, this is the first class I have implemented.  I am using the same concept to update the same custom field on the Account object.

 

trigger myOppTrigger on Opportunity (before insert, before update) { 

Set<id> uID = New Set<id>();
Map<ID,User> mUserRegion;

for(Opportunity o : trigger.new){

uID.add(o.OwnerID);

}

mUserRegion = New Map<ID,User>([Select ID, Sales_Region__c FROM User Where ID IN :uID]);

if (Trigger.isBefore && !Trigger.isDelete) {
        for(Opportunity o: Trigger.new){
           If(mUserRegion.get(o.OwnerID) != Null)               
               o.Sales_Region__c = mUserRegion.get(o.OwnerID).Sales_Region__c;
      }

 }
 
 }

Jake GmerekJake Gmerek

I would try this:

 

}

@isTest
private class TestSalesRegionUpdate{

static testMethod void testSalesRegionUpdate() {

Opportunity opportunity = new Opportunity(Name = 'Test',OwnerID = '005E0000000d0DU',StageName = 'Pipeline',CloseDate=System.today());
insert opportunity;

Test.startTest();
update opportunity;
Test.stopTest();

opportunity = [Select ID, Sales_Region__c FROM Opportunity Where Name = 'Test' LIMIT 1];

user u = new user[select  Sales_Region__c from user where id = o.ownerid LIMIT 1]);
system.assert(opportunity.Sales_Region__c == u.Sales_Region__c);
}

 

That should be a more dynamic test since it querys for the data from the user object that is being set on the opportunity, because the only reason that I can see at this point for the test to fail is if the Sales region on the Owner of the Opportunity is not 'NA - Great Lakes & Canada'.

This was selected as the best answer
mcareymcarey

That worked and I think it might be due to the fact that Sale Region is set as a picklist field in production and a text field in the sandbox.  Perhaps that was throwing my code out of whack.  I will try to convert the field to a text field to be consistent with the Opportunity trigger.  Thanks for the help.

Jake GmerekJake Gmerek

Yes that could cause some problems, I am glad that you got that working.  Talk to you later.

 

Jake