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
crob1212crob1212 

System.QueryException: List has no rows for assignment to SObject Please Help!!

Sorry...here is the code in a more legible format (p.s. I don't recommend using Chrome when posting messages including code.  Somehow the formatting of the code gets lost when the message is posted)

 

 

@isTest private class TestHoldAddressedOnCase { public static testMethod void testHoldAddressed() { //set up user User u = [ SELECT id FROM User WHERE alias='auser' LIMIT 1]; //Run as u System.RunAs(u) { //create a Request Request__c r = new Request__c (MRCProject__c='a0F700000028Eb5', Group__c='00G70000001CnPEEA0',Subject__c='a0270000005GLDl', Custodian_Lookup__c='0017000000YN7Mt', Record_Type__c='Academic', Status__c='10-Request Sent', Case_Priority__c='P4', Method_of_Authorization__c='Blank Auth', Source__c='Client'); insert r; //verify the request was created correctly. r = [ SELECT Id, Hold_Addressed__c FROM Request__c WHERE Id = :r.Id LIMIT 1]; system.assertEquals(FALSE, r.Hold_Addressed__c); //Create the Case (Client Directive) string testCase = 'Test Case'; Case c = new case (RecordTypeId='01270000000LtyP', Status = 'Unassigned', Related_Subject__c='a0270000005GLDl', Request__c=r.Id,Hold_Addressed__c = TRUE); //Cause the trigger to execute. insert c; //Verify that the request was updated. r = [ SELECT Id, Hold_Addressed__c FROM Request__c WHERE Id = :c.Id]; System.assertEquals(c.Hold_Addressed__c, r.Hold_Addressed__c); } } }

 

 

 

 

Hello All,

I am new to Apex and I'm pulling my hair out trying to figure out what is wrong with my code.  In a nutshell, there is a field in the Case object called Hold_Addressed__c, which is a checkbox field.  And there is a field with the same name (Hold_Addressed__C) on a custom object called Request__c.  I've written a trigger that populates the field on the custom object with the value of the field on the case.  So, if case.Hold_Addressed__c = TRUE then Request__r.Hold_Addressed__c = TRUE; same for FALSE.  The trigger is working great.  But the test class is not.  I'm getting an error stating "System.QueryException: List has no rows for assignment to SObject".  Can anyone help with this?  Many Thanks.

Here is the test class:

 

 

@isTestprivate class TestHoldAddressedOnCase{ public static testMethod void testHoldAddressed() { //set up user User u = [ SELECT id FROM User WHERE alias='auser' LIMIT 1]; //Run as u System.RunAs(u) { //create a Request Request__c r = new Request__c (MRCProject__c='a0F700000028Eb5', Group__c='00G70000001CnPEEA0',Subject__c='a0270000005GLDl', Custodian_Lookup__c='0017000000YN7Mt', Record_Type__c='Academic', Status__c='10-Request Sent', Case_Priority__c='P4', Method_of_Authorization__c='Blank Auth', Source__c='Client'); insert r; //verify the request was created correctly. r = [ SELECT Id, Hold_Addressed__c FROM Request__c WHERE Id = :r.Id LIMIT 1]; system.assertEquals(FALSE, r.Hold_Addressed__c); //Create the Case (Client Directive) string testCase = 'Test Case'; Case c = new case (RecordTypeId='01270000000LtyP', Status = 'Unassigned', Related_Subject__c='a0270000005GLDl', Request__c=r.Id,Hold_Addressed__c = TRUE); //Cause the trigger to execute. insert c; //Verify that the request was updated. r = [ SELECT Id, Hold_Addressed__c FROM Request__c WHERE Id = :c.Id]; System.assertEquals(c.Hold_Addressed__c, r.Hold_Addressed__c); } } }

 


 

Message Edited by crob1212 on 03-06-2010 06:12 AM
Message Edited by crob1212 on 03-06-2010 06:25 AM
AlexPHPAlexPHP

First of all, could you format your pasted code so that it's not all jumbled together?

 

Secondly, when you run this test, it should give you the line number where the exception is occurring in the log.  This will help you track down issues with your code. 

Message Edited by AlexPHP on 03-05-2010 04:37 PM
crob1212crob1212

I updated the original post so that the code is not strung together.  Sorry about that.

 

Here is a screen shot of the test log.  The line of code referenced in the log is "User u = [ ".  But I don't understand what it is about that line that is generating the error.  

 

Any help is appreciated.

 

 

Thanks

AlexPHPAlexPHP

It looks like your query:

 

User u = [ SELECT id

FROM User

WHERE alias='auser' LIMIT 1];

 

 is not returning any records.  Try running that query on the APEX Explorer, to make sure you have a user with that alias.

 

To avoid running into these errors, try to assign SOQL queries results to a list of sObjects instead.

  

List<User> users = [ SELECT id

FROM User

WHERE alias='auser' LIMIT 1];

 Doing it this way, you will simply have an empty list if the SOQL query doesn't return any records.  This way, you won't have the QueryException.

 

You can subsequently check for the size of the users list by using the size method:

 

 

users.size()

 

 If the size is 0, then you know you have no records for your query.

 

 

 

Message Edited by AlexPHP on 03-08-2010 10:13 AM
crob1212crob1212
Thanks Alex.  That fixed it.