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
amzyamzy 

hi i have a trigger updating contact photo by mapping with Email id, i need test class for this trigger. Can anyone help on this please? code is below -

trigger contactTrigger on Contact (before insert,before Update) {
  if(RecursiveTrigger.isFirst){
       if(Trigger.isInsert || Trigger.isUpdate){
           List<String>  emailList = new List<String>();
           for(Contact c:Trigger.new){
             emailList.add(c.Email);
           }
           
           System.debug('email====>>>>'+emailList);
           
           Map<String,User> userMap = new Map<String,User>();
           List<User>  objUser = [Select id,email,FullPhotoUrl from User where Email in: emailList];
           for(User u:objUser){
            if(u.FullPhotoUrl != null)
               userMap.put(u.email,u);
           }
           List<Contact> objContactList = new List<Contact>();
           for(Contact c:Trigger.new){
            if(userMap.containskey(c.Email))
            
             c.Photo_Url__c = userMap.get(c.Email).FullPhotoUrl;
              
           }
           
       }
  }
}
Best Answer chosen by amzy
VamsiVamsi

Hi,


I tried to create a new User but FullPhotoUrl is not writeable field. So we dont have option to create a new User with Full URL so the only way is to query the running User to get the Ful photo URL.

Here you can find the bulkified version of code.


@IsTest
public class Test_PhotoMap 
{

    @isTest public static void Photomap1()
    {
         id userId=userinfo.getUserId();
         user u=[Select id,email,FullPhotoUrl from User where id=:userId];

        List<Contact> clist = new List<Contact>();
    
        for(Integer i=1;i<=200;i++)
        {
         contact con = new contact (FirstName='Testcon'+i,LastName='FinalCon',Email=u.Email);
         clist.add(con);
        }
        Test.startTest();
        insert clist;
        Test.stopTest();
    }

}

 

All Answers

VamsiVamsi
Hi,

Create a new User record by speciying a test url to FullPhotoUrl  in Test class and then create a new contact finally verify that contact photo URL field has something copied from User object.

Hope this helps...!!

Please mark as best answer if the above helps..!!!
amzyamzy
@Vamsi  i am sorry i dint understand anything in this. since i am a beginner in coding, i dont have an idea how to write test class for this. if you could show the code format that would be great help.
Narender Singh(Nads)Narender Singh(Nads)
Hi Amrita,

Use this code:
 
@istest
public class TestCls {
  	
     private static testMethod void TestFunction(){
         id userId=userinfo.getUserId();
         user u=[Select id,email,FullPhotoUrl from User where id=:userId];
         
         contact c=new contact();
         c.LastName='Test Con';
         c.Email=u.email;
         
         insert c;
     }
	 
}

Let me know if it helps
Thanks!
 
VamsiVamsi

Hi,


I tried to create a new User but FullPhotoUrl is not writeable field. So we dont have option to create a new User with Full URL so the only way is to query the running User to get the Ful photo URL.

Here you can find the bulkified version of code.


@IsTest
public class Test_PhotoMap 
{

    @isTest public static void Photomap1()
    {
         id userId=userinfo.getUserId();
         user u=[Select id,email,FullPhotoUrl from User where id=:userId];

        List<Contact> clist = new List<Contact>();
    
        for(Integer i=1;i<=200;i++)
        {
         contact con = new contact (FirstName='Testcon'+i,LastName='FinalCon',Email=u.Email);
         clist.add(con);
        }
        Test.startTest();
        insert clist;
        Test.stopTest();
    }

}

 
This was selected as the best answer