2

I created a test using the following apex code:

static testMethod void testProfile() {

    Test.startTest();

    Task task = [select id, OwnerId from task where OwnerId = '00104'];

    Profile p = [select id from profile where name='System Administrator'];

    User u = new User(alias = 'salesf', email='iamasalesforce@noemail.com', timezonesidkey='America/Los_Angeles', localesidkey='en_US', emailencodingkey='UTF-8', ProfileId = p.Id, Id = task.OwnerId, languagelocalekey='en_US', lastname='Testing', Firstname='Testing', CompanyName= 'xyz', username='iamasalesforce@noemail.com');
    insert u;

    Test.stopTest();
}

but I recevied the following error message:

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

How can I execute the code in my tets method without receiving such error message?

  • Profile cannot be inserted from code. It is read-only. – mohammed azarudeen Jul 21 '17 at 10:11
1

Do something like this. It might help you:

public User createUser(){ 
     Profile p = [select id from profile where name='System Administrator'];
     //userRole r=[SELECT ID FROM userRole WHERE name =: 'CEO']; // Use it if you need Role

     User u = new User(alias = 'salesf', email='iamasalesforce@noemail.com',
                       emailencodingkey='UTF-8',profileid = p.Id,lastname='Testing', Firstname='Testing', languagelocalekey='en_US',
                       localesidkey='en_US',country='United States Of America', CompanyName= 'xyz',Phone='(123) 456-7890',title='Dev',PostalCode='S1A 0E5',
                       timezonesidkey='America/Los_Angeles', username='iamasalesforce@noemail.com'',city='Phoenix',State='Alabama');
     insert u;
     return u;   
}
  • Thank you for your valuable workaround. I've adde one more line Task task = [select id, OwnerId from task where OwnerId = '0050Y000001EwVyQAK']; because I need my test to be executed for Task object. But when I run the test I get: System.QueryException: List has no rows for assignment to SObject. Is there any workaround and how to avoid such message? – sfdev Jul 21 '17 at 13:09
  • Please explain what you changed. – Adrian Larson Jul 21 '17 at 15:10
  • @sfdev hey, seems like ur query is not returning any value. That OwnerId = '0050Y000001EwVyQAK'...seems like tha owner id is wrong. Pls chek the owner id once. Better, u run that siql query first in workbench and see if it returns any result. If u dont know how to use workbench, plz google once. And also, hardcoding is not a good practice. But i will tell u how to avoid it after u have queried the same soql in workbench. Get back to me once done – Abhishek Jul 23 '17 at 6:18
0

Profile is read-only. you aren't able to do DML on profile object. It is because Profiles are described as metadata, and are stored as metadata in xml format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.