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
ynwaynwa 

Test Class not working

Hi All,

 

I need help with the below test class (trigger code below). It's my first trigger. I am trying to test an insert (for now) and it seems like my trigger is not working. However my trigger works when I insert/update in Salesforce and bulk insert/update using dataloader. Why does the test class fail to populate the Sales_Rep_CS__c field?

 

Thanks

 

Update: The test results in a Failure: "Assertion Failed: Expected: null, Actual: Julien Trapes"

@isTest
private class AssignSaleRep_Test_Class {

	static testMethod void Test1_TestInsertWithcountryValue() {
		list <Account> liacc = new list <Account>{};
		string SR_CS;
		Account a1 = new Account();
		a1.name = 'PRSingleTest';
		a1.BillingCountry= 'France';
		a1.BillingPostalCode = '59008';
		a1.Sales_Rep_CS__c ='F';
      	a1.Sales_Rep_M__c ='';
		system.debug('^^^^^^^^^^**********^^^^^'+a1);
		liacc.add(a1);
		insert liacc;
	
		list<Account> ia = [select name, sales_rep_cs__c from Account where id IN :liacc];
		for(Account s1 : ia)
		{
			system.debug('!!!!!!!!!!!!!!!!!!!!!'+s1.Sales_Rep_CS__c);
			system.assertEquals(s1.Sales_Rep_CS__c, 'Julien Trapes');
		}
	}
}

 

The Trigger:

trigger Assign_Sales_Rep on Account (before insert, before update) 
{
	// Grab Terriory information based on CountryPostcode
    map<String, String> map_CountryPostcode_to_SalesRepCS = new map<String, String>();
    map<String, String> map_CountryPostcode_to_SalesRepM = new map<String, String>();
    
    string AccCountryPostcode = ''; 
	List<String> postalCodes = new List<String>();
	List<Account> accList = new List<Account>();
	
	for (Account acc : trigger.new)
	{
		if (acc.BillingPostalCode == null || acc.BillingPostalCode == '' || acc.BillingPostalCode == ' ' || acc.BillingCountry == Null || acc.BillingCountry == '' || acc.BillingCountry == ' ')
	    {
	    }
		else
		{
			accList.add(acc);
			AccCountryPostcode = acc.BillingCountry+acc.BillingPostalCode;
			postalCodes.add(AccCountryPostcode);
		}
  	
	}
	
	for(Sale_Territory__c st : [SELECT CountryPostcode__c, Sales_Rep_CS__c, Sales_Rep_M__c FROM Sale_Territory__c WHERE CountryPostcode__c IN :postalCodes])
	{
		map_CountryPostcode_to_SalesRepCS.put(st.CountryPostcode__c, st.Sales_Rep_CS__c);
        map_CountryPostcode_to_SalesRepM.put(st.CountryPostcode__c, st.Sales_Rep_M__c);
        system.debug('&&&&&&&&&&&'+map_CountryPostcode_to_SalesRepCS);
	}
	
	for(Account act : accList)
	{
		if (map_CountryPostcode_to_SalesRepCS.containsKey(AccCountryPostcode))
		{
			act.Sales_Rep_CS__c = map_CountryPostcode_to_SalesRepCS.get(AccCountryPostcode);      
			system.debug('@@@@@@@'+act.Sales_Rep_CS__c);             
		}
		else
		{
			act.Sales_Rep_CS__c ='';
		}
		
		if (map_CountryPostcode_to_SalesRepM.containsKey(AccCountryPostcode))
		{
			act.Sales_Rep_M__c = map_CountryPostcode_to_SalesRepM.get(AccCountryPostcode);                
		}
		else
		{
			act.Sales_Rep_M__c ='';
		}
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

If you are creating test class of API Version greater than 24.0...You will need to create test data for sales territory. By default it would not consider existing data for the org.

 

All you need to do is create test data for sales territory with countryPostCode__c as 59008... else use @isTest (SeeAllData = true)

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

If you are creating test class of API Version greater than 24.0...You will need to create test data for sales territory. By default it would not consider existing data for the org.

 

All you need to do is create test data for sales territory with countryPostCode__c as 59008... else use @isTest (SeeAllData = true)

This was selected as the best answer
ynwaynwa

Thanks Sam...you are great...I was going through line after line of debug logs.

Thanks Again!!