You need to sign in to do that
Don't have an account?

Need help to write test class for the trigger
Please provide any test class that will cover atleast 75% code area for the following trigger
trigger Duplicate on Account (before insert, before update)
{
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account : System.Trigger.new)
{
//expected to execute when converting leads to account
String tempAccountSite = '';
if (account.Site == null || account.Site == '')
{
account.Site = account.Lead_Site__c;
//force the formula of Account_Site_c since mapping of Lead Site to Account Site
//occurs after this trigger
tempAccountSite = account.Name + account.Lead_Site__c;
}
// Make sure we don't treat an Account_Site that is
// not changing during an update as a duplicate.
if ((account.Account_Site__c != null) && (System.Trigger.isInsert ||
(!account.Account_Site__c.EqualsIgnoreCase(System.Trigger.oldMap.get(account.ID).Account_Site__c))))
{
// Make sure another in the batch is not a duplicate
if (accountMap.containsKey(account.Account_Site__c.ToLowerCase()))
{
account.addError('A Account is a duplicate - Account Name and Account Site already exists');
}
else if (tempAccountSite != '')
{
accountMap.put(tempAccountSite.ToLowerCase(), Account);
}
else
{
accountMap.put(account.Account_Site__c.ToLowerCase(), Account);
}
}
}
// With a single database query, find all the accounts in
// the database that have the same name and site as any
// of the accounts being inserted or updated.
for (Account account : [SELECT Account_Site__c from Account WHERE Account_Site__c IN :accountMap.KeySet()])
{
Account newAccount = accountMap.get(account.Account_Site__c.ToLowerCase());
if (newAccount != null)
{
newAccount.addError('B Account is a duplicate - Account Name and Account Site already exists');
}
}
}
Hi,
You can make use of the below test method:
Hope this helps.
Thanks
Hi Sureka,
I am afraid to get an answer of this question on this portal.. pls help
It's a new custom object created in sandbox.. and i am trying to deploy it to production. I have the following trigger and for this trigger i have written the following text class... the code coverage is 100% in sandbox. But when i tried deploying it to production it gives me an error saying "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required" please help me with that..
trigger updaterequestowner on Pre_Sales_Request__c (before insert)
{
Profile auth_profile = [select id from profile where name='HS_StandardUser_Presales'];
Id v_User = UserInfo.GetProfileId();
for(Pre_Sales_Request__c ob : Trigger.new)
{
if(v_User != auth_profile.Id)
{
ob.Request_Owner__c = Userinfo.getUserId();
}
}
}
and i have the following test class :
@isTest
private class TriggerTests {
public static testmethod void testTrigger() {
Pre_Sales_Request__c psrc=new Pre_Sales_Request__c( name = 'a' );
insert psrc;
psrc.name = 'b';
update psrc;
}
}