0

The class I'm trying to upload is a copy of another one I made with just some basic differences like Role name. I had no trouble uploading the last class but for some reason this one is not working.

My Apex Class:

global class Extra_Mile implements Schedulable {
global void execute(SchedulableContext SC) {

    UserRole Role = [SELECT Id from UserRole where Name='Success Manager'];
    list <user> us = [SELECT Id, Name from User where UserRoleId=:role.Id];
    List<task> ta  = new list<task>();
    for (User use: us)
    {
        Task t= new task();
        t.Subject       =   'Extra Mile';
        t.ActivityDate  =   Date.today();
        t.Description   =   'Hacer algo extra para llegar mas rapido a tus metas';
        t.Status        =   'Open';
        t.OwnerId       =   use.Id;
        t.Priority      =   'Normal';


        ta.add(t);


    }

    insert ta;
}

}

The error is in the insert line

Test Class:

    @isTest(seealldata=false) private class Extra_Mile_isTest{ public static String CRON_EXP = '0 0 0 15 3 ? 2022'; @testsetup static void createtestdata(){

    Account myaccount = new account();
    myaccount.Name='Scrum Diario The Influence';
    myaccount.Cliente_Activo__c=true;
    myaccount.OwnerId='00536000001DJsZ';
    myaccount.Ninja__c='00536000001DJsZ';
    Insert myaccount;
    UserRole rol = new UserRole();
    rol.Name='Success Manager';
    user us = new user();
    us.Alias='vic';
    us.IsActive=true;
    us.UserRoleId=rol.Id;
    system.assertnotEquals(null,myaccount.id);  
}
 static testmethod void test1(){
    Test.startTest();
        system.schedule('test job', CRON_EXP, new Extra_Mile());
    Test.stopTest();
}
}

I've looked around only to find that other people have the same issuer but no solution is given. What am I missing here?

4
  • Whats the error?
    – Samir
    Apr 28, 2017 at 15:05
  • 2
    Is there a user available with id '00536000001DJsZ'? Never harcode an Ids in your classes.
    – Samir
    Apr 28, 2017 at 15:06
  • System.DmlException: Insert failed. First exception on row 0; first error: INACTIVE_OWNER_OR_USER, operation performed with inactive user [00536000001CFQ8] as owner of task: [] Stack Trace: Class.Extra_Mile.execute: line 23, column 1 This is the error I'm getting and yes there is a user available with that ID. But actually that information is not necessary. Only the User information is necesary. Apr 28, 2017 at 15:12
  • 1
    Hardcoding IDs is a bad practice. Apr 28, 2017 at 15:14

1 Answer 1

3

Users are available to test methods

list <user> us = [SELECT Id, Name from User where UserRoleId=:role.Id];

Will get a list of all users

for (User use: us)

will do the action using those users including the inactive ones

Change the query to

list <user> us = [SELECT Id, Name from User where UserRoleId=:role.Id And IsActive = true];

If you want to have your batch run with a specific user then you can add a private property visible to the test, set that property to the user you created and then have your query use that property to filter for that user. When not in test context have it query all....Something like

global class Extra_Mile implements Schedulable {

  @TestVisible private Id testUserId;

    global void execute(SchedulableContext SC) {

        UserRole Role = [SELECT Id from UserRole where Name='Success Manager'];
        list <user> us = database.query('SELECT Id, Name from User where UserRoleId=:role.Id' + testUserId == null ? '' : ' And Id = :testUserId');

    }
    }

Then in your test

Extra_Mile tmp = new Extra_Mile();
tmp.testUserId = [Desired User Id Here];
system.schedule('test job', CRON_EXP, tmp);
1
  • Perfect! I just added AND IsActive=TRUE and it worked! Apr 28, 2017 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .