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
SFAdmin5SFAdmin5 

need test class

Anyone have a test class for this trigger:

trigger ownerCopy on Lead (before Insert, before Update) {

    for(Lead x : Trigger.New){

        if( ((String)x.OwnerId).substring(0,3) == '005' ){
            x.Owner_Copy__c = x.OwnerId;
        }
        else{
            x.Owner_Copy__c = null;
        }
    }

} 

 

Thanks!

 

 

Shashikant SharmaShashikant Sharma

try this

 

@isTest
private class ownerCopytestClass {

 private static testmethod void testownerCopyTrigger()
     {
     
         Lead l = new Lead();
         l.LastName = 'Test';
         l.Company = 'TestCompany';
         insert l;
         
         Group groupObj = new Group(Name='Queue',Type='Queue');
            insert groupObj;
            
            QueueSobject queuesO = new QueueSobject(QueueId = groupObj.Id, SobjectType = 'Lead');
            System.runAs(new User(Id = UserInfo.getUserId()))
             {
              insert queuesO;
             }
                       
            l.OwnerId = groupObj.Id;
            
             update l;
     
          
     }


}

 Will cover 100% code

SFAdmin5SFAdmin5

Thanks!  That works.  I need a test class for almost the exact same trigger, but instead of the Lead object, the trigger's on the account object.  Any idea what a test class would be for this trigger:

 

trigger AccountOwnerCopy on Account (before Insert, before Update) {

    // handle arbitrary number of opps
    for(Account x : Trigger.New){

        // check that owner is a user (not a queue)
        if( ((String)x.OwnerId).substring(0,3) == '005' ){
            x.Owner_Copy__c = x.OwnerId;
        }
        else{
            // in case of Queue we clear out our copy field
            x.Owner_Copy__c = null;
        }
    }

}

 

Shashikant SharmaShashikant Sharma

You can try this

@isTest
private class ownerCopytestAccountClass {

 private static testmethod void testownerCopyAccountTrigger()
     {
     
         Account a = new Account();
         a.Name = 'Test';
         insert a;
         
         Group groupObj = new Group(Name='Queue',Type='Queue');
            insert groupObj;
            
            QueueSobject queuesO = new QueueSobject(QueueId = groupObj.Id, SobjectType = 'Account');
            System.runAs(new User(Id = UserInfo.getUserId()))
             {
              insert queuesO;
             }
                       
            a.OwnerId = groupObj.Id;
            
             update a;
     
          
     }


}

 let me know if any issues in it.

SFAdmin5SFAdmin5

Thanks.  TYour test class gets me 75% which is sufficient, but I'd really like to see if it's possible to get this to 100%.  There's just one line that is not passing yout test class in the trigger (line 8):

 

1 	  trigger AccountOwnerCopy on Account (before Insert, before Update) {
 2 	  
 3 	   // handle arbitrary number of opps
 4 	   for(Account x : Trigger.New){
 5 	  
 6 	   // check that owner is a user (not a queue)
 7 	   if( ((String)x.OwnerId).substring(0,3) == '005' ){
 8 	   x.Owner_Copy__c = x.OwnerId;
 9 	   }
 10 	   else{
 11 	   // in case of Queue we clear out our copy field
 12 	   x.Owner_Copy__c = null;
 13 	   }
 14 	   }
 15 	  
 16 	  }

 Any idea how to get this trigger to pass 100% coverage?

 

Thanks I really appreciate it

Shashikant SharmaShashikant Sharma

 

Change your trigger to , I have not changed any logic of this just changed the represntation so that one less script statement and full code coverage.

 

trigger AccountOwnerCopy on Account (before Insert, before Update) {

    // handle arbitrary number of opps
    for(Account x : Trigger.New){

        // check that owner is a user (not a queue)
        x.Name = (((String)x.OwnerId).substring(0,3) == '005' ) ? x.OwnerId : null;
       
    }

}

 And Test class to

 

@isTest
private class ownerCopytestAccountClass {

 private static testmethod void testownerCopyAccountTrigger()
     {
     
         Account a = new Account();
         a.Name = 'Test';
         insert a;
         try
         {
            Group groupObj = new Group(Name='Queue',Type='Queue');
            insert groupObj;
            
            QueueSobject queuesO = new QueueSobject(QueueId = groupObj.Id, SobjectType = 'Chkbox__c');
            System.runAs(new User(Id = UserInfo.getUserId()))
             {
              insert queuesO;
             }
                       
            a.OwnerId = groupObj.Id;
            
             update a;
         }
         catch(exception e)
         {
         }
          
     }


}

 It should give you 100% code coverage.

Let me know if any issues in it.

RyadavRyadav

public class ART_CheckAvailableUsers
{
    public static Set<string> getAvailableUsers(Set<string> userIdSet)
    {
        List<Event> userEventList = new List<Event>();
        
        //Check if the user has an Out of Office status
        userEventList = [SELECT Id, OwnerId FROM Event WHERE EndDateTime >= :System.Now() AND StartDateTime <= :System.Now() AND ShowAs Like 'Out of Office' AND OwnerId in :userIdSet];
        
        //If s/he has OOO, then remove that user from the result set.
        for(Event e : userEventList)
        {
            userIdSet.remove(e.OwnerId);
        }
        return userIdSet;
    }
}