In test class and methods, we can access data in the organization. Prior to it we can just access test data. Using this we can access organization data.
For Apex code saved using Salesforce.com API version 24.0 and later, use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test didn’t create.
Starting with Apex code saved using Salesforce.com API version 24.0, test methods don’t have access by default to pre-existing data in the organization. However, test code saved against Salesforce.com API version 23.0 or earlier continues to have access to all data in the organization and its data access is unchanged.
Unit Testing for Triggers
Sample Trigger:
trigger DuplicateMemberCheck on Member__c (before insert)
{
for(Member__c memb:trigger.new)
{
List<Member__c> mem = new List<Member__c>();
String email = memb.E_Mail_Id__c;
String sql = 'SELECT E_Mail_Id__c FROM Member__c';
mem = Database.Query(sql);
for(Member__c tempMember:mem)
{
if(tempMember.E_Mail_Id__c == email)
{
memb.E_Mail_Id__c.addError('Duplicate Record');
}
}
}
}
Sample Test Class for Sample Trigger:
@isTest
public class TestDuplicateMemberCheckClass
{
static testMethod void test()
{
Date birthday = Date.valueOf('2000-12-20');
Member__c mem = new Member__c(Name = 'Test', E_Mail_Id__c = 'magulan.d@igatepatni.com', Mobile_Number__c = '99966663322', Birthday__c = birthday);
Member__c mem1 = new Member__c(Name = 'Test', E_Mail_Id__c = 'magulan@igatepatni.com', Mobile_Number__c = '99966663322', Birthday__c = birthday);
try
{
insert mem;
insert mem1;
}
catch(DMLException e)
{
System.assert(e.getMessage().contains('Duplicate Record'));
}
}
}
How to use multiple substitute function in Salesforce?
We have to use nested SUBSTITUTE() methods for multiple replacing of characters.
Example:
SUBSTITUTE(SUBSTITUTE(Name," ", ""),"-","" )
In this example, we can remove "-" and space.
Validation rules for Country codes(USA, Mexico, Argentina, Canada, Australia) in Salesforce
Example:
OR(
AND(
ISPICKVAL( Country_Code__c , "USA"),NOT( REGEX( Pincode__c , "\\d{5}(-\\d{4})?") )
),
AND(
ISPICKVAL( Country_Code__c , "MEX"), NOT(REGEX( Pincode__c , "\\d{5}")), NOT(REGEX( Pincode__c , "\\d{6}"))
),
AND( ISPICKVAL( Country_Code__c , "AUS"), NOT(REGEX( Pincode__c , "\\d{4}"))
),
AND( ISPICKVAL( Country_Code__c , "CAN"), NOT(REGEX( Pincode__c , "[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\\s[0-9]{1}[a-zA-Z]{1}[0-9]{1}"))
),
AND( ISPICKVAL( Country_Code__c , "ARG"), NOT(REGEX( Pincode__c , "[a-zA-Z]{1}\\d{4}[a-zA-Z]{3}")),NOT(REGEX( Pincode__c , "\\d{4}"))
)
)
0 Comments