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
Andrew TelfordAndrew Telford 

Reference 'x' record in SOQL result

Hi

I am trying to reference a particular record in a recordset that has been returned from an SOQL statement. I am doing this as part of a test class and I am not concerned which record I return but I do need to return the second record in another section of the script.
User rsUsers = [ SELECT Id, Username, Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey FROM user WHERE name = 'State Account' or name = 'Andrew Telford' ];
I have been trying do this by doing
 
Contact srvContact = new Contact
			( 
				AccountID = acct.Id,
				FirstName = 'John',
				LastName = 'Smith',
				OwnerID = rsUsers[0].Id,
				RecordTypeId = recType.Id,
				Approved_Owner_Change__c = FALSE,
				Approved_Owner_Name__c = NULL,
				CCC_Adviser__c = FALSE,
				Segmentation_Tier__c = 'Tier 4'
			);
			insert srvContact;
Any assistance or direction would be appreciated.

 
Chandra Sekhar CH N VChandra Sekhar CH N V
Hi Andrew,

Your soql query might return more than 1 record. Either make the 'rsUsers' as a list 
list<User>

else you can also limit number of rows by adding a limt at the end , ex- 
[select .... LIMT 1]

Let me know if this was something you were referring above.