You need to sign in to do that
Don't have an account?
if List has no rows for assign and runas needs to be used
HI All,
Anyone please help me with the trigger, basically it fires on insert and works if a user with username 'abc@abc.com'' assigns a new task to anyone other than the owner of the task it will be automatically assigned to the owner of the task.
THe issue is i don't have any unit test failure but when i validate the code it throws following error."Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.Task_AssignOwnerTest.test: line 51, column 1"
i could put a if(User.size()==0) then return;
but to do this i need to do below change:
User[] user = [
select Id, ProfileID
from User
where IsActive = true
and username = 'abc@abc.com'
limit 1
];
and if i do above changes then i am not able to runAs (User[0].id) .
as the error is "runAs requires single argument of type 'user'"
Below is the test class
@IsTest
private class Task_AssignOwnerTest {
public static Program__c program1 = [select Id from Program__c limit 1];
@IsTest
public static void test() {
User[] userList = [
select UserRoleId
from User
where Profile.Name = 'System Administrator'
and UserRoleId != null
and IsActive = true
limit 3
];
Account account = new Account();
account.name = 'Testing account';
account.ownerId = userList[0].Id;
insert account;
System.debug(' ::Task_AssignOwnerTest:: step 1' +account.Id);
Contact contact = new Contact();
contact.FirstName = 'FirstName';
contact.Start_Term__c = 'Jan 2011';
contact.LastName = 'LastName';
contact.Program_Primary__c = program1.Id;
contact.OwnerId = userList[0].Id;
contact.AccountId = account.Id;
insert contact;
System.debug(' ::Task_AssignOwnerTest:: step 2' +contact.Id);
Opportunity opportunity = new Opportunity();
opportunity.Contact__c = contact.Id;
opportunity.Name = 'OpportunityName';
opportunity.StageName = 'StageName';
opportunity.CloseDate = Date.today();
opportunity.Program__c = program1.Id;
opportunity.OwnerId = userList[0].Id;
insert opportunity;
System.debug(' ::Task_AssignOwnerTest:: step 3' +opportunity.Id);
(Line 51 is here)User user = [
select Id, ProfileID
from User
where IsActive = true
and username = 'abc@abc.com'
limit 1
];
System.debug(' ::Task_AssignOwnerTest:: step 4' +user[0].id);
try {
System.RunAs(user){
Task[] taskList = new Task[] {
new Task (WhoId = contact.Id, OwnerId = userList[1].Id),
new Task (WhoId = null),
new Task (WhoId = opportunity.Id, OwnerId = userList[1].Id)
};
insert taskList;
}
} catch (System.dmlException e) {
}
}
}
You can simply use the first user in the list as long as you get a match.
E.g.
All Answers
You can simply use the first user in the list as long as you get a match.
E.g.
Change your test class to version 23 or
give
@isTest(SeeAllData=true)
Thanks a lot Bob & Steve