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
Janin DancauseJanin Dancause 

Help to write the first unit test



This is my trigger I have write to add a specific account ID to a Contact. I am new to salesforce programming and I try to write the unit test but I need some help to do it.
trigger Contacts_Accounts on Contact (Before Insert, Before Update) {

for (Contact newContact : Trigger.New){
        String id_account;
        id_account = [select id from Account where idClientSoquij__c = :newContact.idClientSoquij__c].id;
        newContact.AccountId = id_account;
        }
}

 
Rahul.MishraRahul.Mishra
Hi Janin,

Since your trigger is on insert and update of contact, you have to create Contacts in your class, to cover all the cases you to insert a case, also update a Case.
Account also you have to create since your trigger does query that, below is the sample code I am pasting here for you for your trigger:
 
@isTest
private class Test_Contacts_Accounts  {

	private static testMethod void test() {
	    
    	    // Assuming idClientSoquij__c field is of type text on both contact and account, update the value if it is number type
    Account acc1 = new Account(Name = 'Test Account', idClientSoquij__c = 'testAccId');
    insert acc1;
    
    // To cover insert case scenario
    Contact con1 = new Contact(FirstName = 'Test', LastName = 'TestCon-1', idClientSoquij__c = 'testAccId');
    insert con1;
    
    Contact con2 = new Contact(FirstName = 'Test', LastName = 'TestCon-2', idClientSoquij__c = 'abcd2');
    insert con2;
    
    
    // To cover update case scenario
    con2.idClientSoquij__c = 'testAccId'
    update con2;

	}

}

Also I would suggest, do not write the query inside the loop since you will encounter exception (Salesforce lemitations) when you will insert more then 150 contact records through data loader.

Mark my answer as best if it does help you.

 
mukesh guptamukesh gupta
Hi Janin,

Please use this code:-
 
@isTest
private class TestAccountDeletion {
    @isTest static void TestDeleteAccountWithOneOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
        Account acct = new Account(Name='Test Account');
        insert acct;
	
        Contact c   = new Contact();
       c.FirstName = 'Stephen';
       c.LastName  = 'Curry';
       c.Email     = 'test@gmail.com';
       insert c;
     
      
    }
    
}

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

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

Thanks
Mukesh

 
Akshay_DhimanAkshay_Dhiman
Hi Janin Dancause

 
Hi Janin Dancause


@isTest
private class TestClassForContactTrigger {
    @isTest static void testMethod() {
       //idClientSoquij__c 
	    System.Test.startTest();
		idClientSoquij__c customObj = new idClientSoquij__c();
		customObj.        // fill all requrid fields value after than insert it 
		Insert customObj;
        accObj.Account accObj = new Account();
		accObj.Name='Test Account';
		accObj.idClientSoquij__c = customObj.id;  
        insert accObj;
	
        Contact conObj   = new Contact();
        conObj.FirstName = 'test';
        conObj.LastName  = 'Data';
        conObj.Email     = 'mail@gmail.com';
		conObj.idClientSoquij__c = customObj.id;
        insert conObj;
        System.Test.stopTest();
      
    }
    
}

Thanks


If you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay