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
Carrie SchoenvogelCarrie Schoenvogel 

Help with Test class for Trigger

Can someone help me with a test class for the following.  Would like to put together a "public WITHOUT SHARING" test class if possible.  Thank you.

trigger caseToAM on Case (before insert) {

   Set<ID> accountIDs = new Set<ID>();

  

   for (Case c : trigger.new) {

      //build a set of unique account ids associated to the case(s) being created

      if (c.AccountId != null && c.RecordTypeId == '012R00000004tyC') {

         accountIDs.add(c.AccountId);

      }

     

   }

   //get the account manager id for all accounts affected

   List<Account> lAccounts = [SELECT Id, Name, Account_Manager__r.Id FROM Account WHERE Id in :accountIDs];

  

   //loop through the cases again and assign the account manager to the case

   for (Case c : trigger.new) {

      //change the owner to the account manager

          for (Account a : lAccounts) {

            if (c.AccountId == a.Id) {

        if(a.Account_Manager__r.Id != NULL)

                 c.OwnerId = a.Account_Manager__r.Id;

        else

                 c.OwnerId = UserInfo.getUserId();

            break;

          }

      }

     

   }  

  

}

hitesh90hitesh90

Hi,

 

Below is the Test class as per your requirement.

 

Test Class:

@istest
public class TestcaseToAM{
    Private Static testmethod void TestcaseToAM(){
        Account objAcc = new Account();
        objAcc.Name = 'Test LastName';
        
        insert objAcc;
     
        List<RecordType> lstRecordType = [select id from Recordtype where id = '012R00000004tyC'];
        Case Objcase = new case();
        if(lstRecordType.size() > 0){
            Objcase.RecordTypeId = lstRecordType[0].id;
        }
        Objcase.Status = 'New';
        Objcase.AccountId = objAcc.id;
        Objcase.Origin= 'Phone';
        insert Objcase;
    }
}

 
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/