+ Start a Discussion
RajnisfRajnisf 

test class to cover .adderror...need help...urgent

  for(Task a : Trigger.old){
            
              if(a.OwnerId != UserInfo.getUserId()  && ProfileId  != '00e00000006qrDIAAY' )
               
               {

                      a.addError('You are not authorised to delete this task!');
              }

        }
        
              
   
 }

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

In your test code, 

 

1. query a user who does not have this Id and ProfileId.

2. use system.runAs to run that code through his login.

 

// CODE HERE //

@isTest

private static void testingTaskDelete(){

     Task t = new Task();

     // give all required fields

     insert t;

 

    User someOtherUser = [Select Id From User Where Id != UserInfo.getUserId() AND ProfileId != '00e00000006qrDIAAY' And isActive = TRUE LIMIT 1];

    system.runAs(someOtherUser){

          delete t;

     }

}

All Answers

Devendra@SFDCDevendra@SFDC
Hi,

Create a test record in a such way that it should call an error. Put the DML operation inside try/catch black. By doing this test methods wont go fail and you can cover error message.

Hops this helps :)

Thanks,
Devendra
RajnisfRajnisf
did the same...bt not working...
Devendra@SFDCDevendra@SFDC
Hi,

Can you please paste your trigger and test method code here?

Thanks,
Devendra
vishal@forcevishal@force

In your test code, 

 

1. query a user who does not have this Id and ProfileId.

2. use system.runAs to run that code through his login.

 

// CODE HERE //

@isTest

private static void testingTaskDelete(){

     Task t = new Task();

     // give all required fields

     insert t;

 

    User someOtherUser = [Select Id From User Where Id != UserInfo.getUserId() AND ProfileId != '00e00000006qrDIAAY' And isActive = TRUE LIMIT 1];

    system.runAs(someOtherUser){

          delete t;

     }

}

This was selected as the best answer
RajnisfRajnisf

thanx a lot....

it is covered now....100 %