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
Sai ManojSai Manoj 

Help Test Class

Hi Experts,
 
Please help me test class for below trigger.

​ 
Trigger AccountandContactUpdateTrigger on Case(before insert, before update){
 
    List<String> AccNoSet = new List<String>();
    for(Case c: trigger.new){
     AccNoSet.add(c.Account_Number__c);
    }
 
    Map<String, Account> AccMap  = new Map<String, Account>();
    for(Account acc : [SELECT Id, Parent_VRP_Number__c, VRP_Account_Number__c from Account where VRP_Account_Number__c IN :AccNoSet]){
     AccMap.put(acc.Parent_VRP_Number__c , acc);
    }
 
    for(Case c:trigger.new){
         c.Accountid = AccMap.get(c.Account_Number__c).id;
         if(c.accountid!=null){
         List<Contact> ConList = [select id, name from Contact where accountid =:c.accountid];
         if(!ConList.isempty()){
         c.Contactid = ConList[0].id;
         }
         }
    }
}
 
Regards,
Sai
Dhanya NDhanya N
Hi Sai,

Check below test class:
@isTest
private class Test_CaseUpdate {

    static testMethod void myUnitTest() {
	
	Test.startTest();
	Account objAccount = new Account(Name = 'Test Account', VRP_Account_Number__c = 1234, Parent_VRP_Number__c = 2345);
	insert objAccount;
	
	Contact objContact = new Contact(LastName = 'Test');
	insert objContact;
	
	Case objCase = new Case(Account_Number__c = 1234);
	insert objCase;
	
	Test.stopTest();
	System.assertEquals(objAccount.Id, [Select AccountId From Case Where Id = objCase.ID].AccountId);
	System.assertEquals(objContact.Id, [Select ContactId From Case Where Id = objCase.ID].ContactId);	
	}
}
Thanks,
Dhanya
 
ManojjenaManojjena
Hi Sai,

Please try to strat writing test class and let us know when you will face problem .Why I am telling ,bacause it wil help you to learn so that you will confident  to write test class in future .

Also your trigger have issue in coding standard .you have query inside for loop which willl give you 101 SOQL error in near future  .
Check with below link it will help to start .
http://manojjena20.blogspot.in/2015/06/tips-and-tricks-for-test-class.html

Incase you have started please post your code so that we wil hlep you to reach 100% and code coverage and all functional test case .

Thanks
Manoj
Amit Chaudhary 8Amit Chaudhary 8

Please update your trigger as below to avoid 101 SOQL error
Trigger AccountandContactUpdateTrigger on Case(before insert, before update)
{
 
    List<String> AccNoSet = new List<String>();
    for(Case c: trigger.new){
     AccNoSet.add(c.Account_Number__c);
    }
 
    Map<String, Account> AccMap  = new Map<String, Account>();
    for(Account acc : [SELECT Id, Parent_VRP_Number__c, VRP_Account_Number__c,( select id ,name from contacts ) from Account where VRP_Account_Number__c IN :AccNoSet])
	{
		AccMap.put(acc.Parent_VRP_Number__c , acc);
    }
 
    for(Case c:trigger.new)
	{
		if(AccMap.containsKey(c.Account_Number__c) )
		{
			Account acc = AccMap.get(c.Account_Number__c);
			List<Contact> ConList = acc.contacts;
			if(!ConList.isempty())
			{
				c.Contactid = ConList[0].id;
			}
		}
    }
}
And please try below test class
@isTest
private class AccountandContactUpdateTrigger  
{

    static testMethod void myUnitTest() 
	{
		Account objAccount = new Account();
			objAccount.Name = 'Test Account'; 
			objAccount.VRP_Account_Number__c = 1234;
			objAccount.Parent_VRP_Number__c = 1234;
		insert objAccount;
		
		Contact objContact = new Contact();
			objContact.LastName = 'Test';
			objContact.FirstName = 'Test';
			objContact.accountid = objAccount.id;
		insert objContact;
	
		Test.startTest();
		
			Case objCase = new Case();
				objCase.Status = 'Working';
				objCaseOrigin = 'Phone';
				objCase.Account_Number__c = 1234;
			insert objCase;
		
		Test.stopTest();
		
			//System.assertEquals(objContact.Id, [Select ContactId From Case Where Id = objCase.ID].ContactId);	
			
	}
}

Let us know if this will help you

Thanks
Amit Chaudhary
 
Salesforce seekarSalesforce seekar
@hello AMit : can you tell me what kind of  custom fields are these on account object ie Parent_VRP_Number__c, VRP_Account_Number__c . are they lookup fields to case