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
sfdc guru 17sfdc guru 17 

test class !!!

Hi All
 How to write test class for the my method 
public static void updating(List<Contact> triggerNew,Map<Id,Contact> triggerOldMap){
        try{
            
            List<String> addressIds = new List<String>();
            for(Contact cont : triggerNew){
                if(cont.location__c != null && cont.location__c !=triggerOldMap.get(cont.Id).location__c ){
                    addressIds.add(cont.location__c);
                }
                
            }
            
            Map<Id,custom__c> add = new Map<Id,custom__c>([Select Id,,name,field1__C,field2__C,field3__C
                                                             from custom__c where id IN: addressIds]);
                


		for(Contact cont : triggerNew){
                
                if(cont.location__c != null && cont.location__c !=triggerOldMap.get(cont.Id).location__c ){
                    if(add.get(cont.location__c) != null){


                         cont.field1__C = add.get(cont.location__c).Name;
                         cont.field2__C = add.get(cont.location__c).field2__C;
                         cont.field3__C = add.get(cont.location__c).field3__C;
                                   
                    }
                }
                
                
            }
               
        }catch(DmlException e) {
               Logger.error('DMLException : '+e.getMessage(), null, 'updating');
        }catch(Exception ex) {
               Logger.error('DMLException : '+ex.getMessage(), null, 'updating');
        }
       }
sfdc guru 17sfdc guru 17
@Rahul I know all this procedure .​ Thanks & Regards, *Maheswar Pradhan,* *Sr. Salesforce CRM Consultant |** SGN Software (P)Ltd* Mob :+91-8450887957* |* Email: maheswar.pradhan@sgnsoftware.com
Leo10Leo10
Hi 
Try this
@isTest

private class updating_Test{ 
	static testMethod void testMethods(){
		list<Contact> contacts = new list<Contact>();
		list<custom__c> customs = new list<custom__c>();
		
		Contact contact1 = new Contact( Lastname='Demo1', location__c='usa');
		contacts.add(contact1);
		Contact contact2 = new Contact( Lastname='Demo2', location__c='can');
		contacts.add(contact2);
		insert contacts;
		
		custom__c custom1 = new custom__c(id=contact1.id,name='test1',field1__C='value1',field2__C='value2',field3__C='value3');
		customs.add(custom1);
		custom__c custom2 = new custom__c(id=contact1.id,name='test1',field1__C='value1',field2__C='value2',field3__C='value3');
		customs.add(custom2);
		insert customs;
		
		contact1.location__c='ind';
		update contact1;
		contact2.location__c='aus';
		update contact2;

	}
}

if you are getting any error plz let me knw.
Thank you