+ Start a Discussion
salesforce instancesalesforce instance 

Not able to cover the test class

Trigger RecordPSOwnerAndMainContact on Case (after Insert) 
{
 
  Profile CurrentUserprofile = [select Name from Profile where Id = :UserInfo.getProfileId() LIMIT 1 ];
  if(CurrentUserprofile.Name ==System.Label.U4_PS_Consultant)
     {     
   List<Case> CasesToInsert = new List<Case>();

   for (case CaseX: trigger.New){ //** Only for new created cases.
  
       Case NewCase  = new Case(Id = CaseX.Id);  //** Store unique Case ID
       
       //** Build list with details of current user (PS or not .. could by any user) ..... and list of main contact selected on case (x)
       List<User> usrList = new List<User>([SELECT id, Formula_Profiel_name__c from user where        Id = :UserInfo.getuserId() LIMIT 1]);
       List<User> CntList = new List<user>([SELECT id, Name, Email, UserName   from User where ContactId = :CaseX.ContactID LIMIT 1]);
 
       for (User usr : usrList){
           if(Usr.Formula_Profiel_name__c == System.Label.U4_PS_Consultant) { // ** Check Profile name with actual profile.
          
              //** Is case created by PS Staff member
              NewCase.PS_Owner__c = UserInfo.getuserId(); //** PS Owner value on case get ID of current user
              Update NewCase; //** Write PS Owner to case
              
              //** Build list for Custom Case team PS Owner transaction
              List<CaseTeam__c> CaseTeamMember = new List<CaseTeam__c>();
              CaseTeam__c CaseTeam = new CaseTeam__c (); //** New Member
             
              //** Populate custom case team values
              CaseTeam.case__c = CaseX.id;                                                  // Case#
              CaseTeam.Type_of_User__c = 'PS User';                                        
              CaseTeam.MemberRole__c = 'Unit4 PS Consultant';                                      // Role
              CaseTeam.User__c = UserInfo.getuserId();                                  // User (PS)
              CaseTeamMember.add(CaseTeam); 
              insert CaseTeamMember; //** Write PS Case team member to Custom Case team
             
              // Now add contact
              For (User Cntxt : CntList){
                //** Build list for custom case team for Main contact transaction
                List<CaseTeam__c> CaseContactTeamMember = new List<CaseTeam__c>();
                CaseTeam__c ContactMember = new CaseTeam__c (); //** New member
             
                //** Populate custom case team values
                ContactMember.case__c = CaseX.id;
                ContactMember.Type_of_User__c = 'Customer User';
                ContactMember.MemberRole__c = 'Main Contact';
                ContactMember.Contact__C = CaseX.ContactID;
                ContactMember.User__c = Cntxt.id;
                CaseContactTeamMember .add(ContactMember); 
                insert CaseContactTeamMember; //** Write Main contact case team member to custom case team
              }
                  
           }
           Else{   //== Not Created by PS Staff member os make sure field is empty
          
              // Now as the case was not created by PS Consultant, main contact registration is done anyway to make
              //  case registration in general much better as this will save support consultants many time and efforts.
              For (User Cntxt : CntList){
                //** Build list for custom case team for Main contact transaction
                List<CaseTeam__c> CaseContactTeamMember = new List<CaseTeam__c>();
                CaseTeam__c ContactMember = new CaseTeam__c ();  //** New member
               
                //** Populate custom case team values   
                ContactMember.case__c = CaseX.id;
                ContactMember.Type_of_User__c = 'Customer User';
                ContactMember.MemberRole__c = 'Main Contact';
                ContactMember.Contact__C = CaseX.ContactID;
                ContactMember.User__c = Cntxt.id;
                CaseContactTeamMember .add(ContactMember); 
                insert CaseContactTeamMember; //** Write Main contact case team member to custom case team
              }
          
              NewCase.PS_Owner__c = NULL; //** No PS Owner (NULL)
              Update NewCase; //** Write empty value to PS Owner field to case
              
           }
       }
   }
}
}
 
Deepak Pandey 13Deepak Pandey 13
Hi 
Firstly insert user with profile which is used in custom label.
Atter that insert Account, contact and case. 
After that CaseTeam__c object and lookup should be populated od case.