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
nilesh walkenilesh walke 

need test class for it

 public static void syncfields(list<contact> con1){
        map<id,contact> contacts=new map<id,contact>();
        list<user> userlist=new list<user>();
        for(contact con:con1)
        {
            if(con.User__c!=null){
                contacts.put(con.User__c,con);//create lookup field inside the contact which look up with user...
            }
        }
        list<user> users=[SELECT mobilePhone,fax,phone from user where id IN:contacts.KeySet()];
        for(user uc:users)
        { 
            if(contacts.get(uc.id).phone!=null){
                uc.phone=contacts.get(uc.id).phone;
            }
            if(contacts.get(uc.id).Fax!=null){
                uc.fax= contacts.get(uc.id).Fax  ;
            }
            if(contacts.get(uc.id).MobilePhone!=null){
                uc.mobilePhone= contacts.get(uc.id).MobilePhone;
            }
            userlist.add(uc);
        }
        if(userlist.size()>0){
            update userlist;
        }   
    }
Best Answer chosen by nilesh walke
CharuDuttCharuDutt
Hii Nilesh Walke
Try Below Code
@istest
public class unitTest {
  @istest
    public static void unitTest(){
        Id userId=UserInfo.getUserId();
        List<contact> lstCon= new List<contact>();
        for(integer i=0;i<5;i++){
            contact Con= new contact();
            Con.lastname='Test Con '+i;
            Con.User__c=userId;
            Con.Phone='1234567';
            Con.MobilePhone='0123456789';
            Con.Fax='7654321';
            Con.Email = 'Test'+i+'@test.com';
            lstCon.add(Con);
        }
        insert lstCon;
        test.startTest();
        testclass.syncfields(lstCon);
        test.stopTest();
    }
}
Please Mark This As The Best Answer. If It Helps
Thank You! 



 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Nilesh,
You can take reference from this below code.
@istest
public class UpdateUser_Test {
  @istest
    public static void createTestData(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser1111@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser1111@testorg.com');
        insert u;
        List<contact> conList= new List<contact>();
        for(integer i=0;i<5;i++){
            contact con= new contact();
            con.lastname='Suraj '+i;
            con.User__c=u.id;
            con.Phone='98765';
            con.MobilePhone='4567898765';
            con.Fax='4598765';
            conList.add(con);
        }
        insert conList;
        test.startTest();
        UpdateUser.syncfields(conList);
        test.stopTest();
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
Suraj Tripathi 47Suraj Tripathi 47
Hi Nilesh,

You can also use this test class:-
@istest
public class UpdateUser_Test {
  @istest
    public static void createTestData(){
        string userid=UserInfo.getUserId();
        List<contact> conList= new List<contact>();
        for(integer i=0;i<5;i++){
            contact con= new contact();
            con.lastname='Suraj '+i;
            con.User__c=userid;
            con.Phone='98765';
            con.MobilePhone='4567898765';
            con.Fax='4598765';
            conList.add(con);
        }
        insert conList;
        test.startTest();
        UpdateUser.syncfields(conList);
        test.stopTest();
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
mukesh guptamukesh gupta
Hi Nilesh.

Please use below code:
 
@isTest 
public class ContTestClass 
{
 static testMethod void testMethod1() 
 {
	 // Setup test data
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com',
			mobilePhone = '888888888',fax = '444444444',phone = '545454465'
			);

 
	System.runAs(u) {
	List<Contact> conList = new List<Contact>();
			  Contact con = new Contact();
			 con.Name='Test Contact';
			 con.User__c = u.id
			 
			 insert con;
			 conList.add(con);
            
        }
 
 

 Test.StartTest(); 
  yourClassName.syncfields(conList)
 Test.StopTest();
 }
}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
CharuDuttCharuDutt
Hii Nilesh Walke
Try Below Code
@istest
public class unitTest {
  @istest
    public static void unitTest(){
        Id userId=UserInfo.getUserId();
        List<contact> lstCon= new List<contact>();
        for(integer i=0;i<5;i++){
            contact Con= new contact();
            Con.lastname='Test Con '+i;
            Con.User__c=userId;
            Con.Phone='1234567';
            Con.MobilePhone='0123456789';
            Con.Fax='7654321';
            Con.Email = 'Test'+i+'@test.com';
            lstCon.add(Con);
        }
        insert lstCon;
        test.startTest();
        testclass.syncfields(lstCon);
        test.stopTest();
    }
}
Please Mark This As The Best Answer. If It Helps
Thank You! 



 
This was selected as the best answer
nilesh walkenilesh walke
Helllo  guys thanks for answers all answers are right and doing well but i thing i better undertand  @CharuDutt,s answer 
and @mukesh gupta  thanks  and  @Suraj thanks :
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com', mobilePhone = '888888888',fax = '444444444',phone = '545454465' 
i might thinking that this data is not mandatory for test the class  !
?
is it true let me know!?
mukesh guptamukesh gupta
Hi Nilesh,

you have SOQL query on user object in main class that's why you need to insert user from test class and profile is mandatory on user creation time that's why i have used

please let me know what is your test class code coverage. please share screen shot of running test class.

so Please always focus on concept.

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
mukesh guptamukesh gupta
Hi Nitesh,

Please consider on below code
@isTest 
public class ContTestClass 
{
 static testMethod void testMethod1() 
 {
	 // Setup test data
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com'
			
			);

 
	System.runAs(u) {
	List<Contact> conList = new List<Contact>();
			  Contact con = new Contact();
			 con.Name='Test Contact';
			 con.User__c = u.id
			 con.mobilePhone = '888888888',
			 con.fax = '444444444',
			 con.phone = '545454465'
			 insert con;
			 conList.add(con);
            
        }
 
 

 Test.StartTest(); 
  yourClassName.syncfields(conList)
 Test.StopTest();
 }
}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh