You need to sign in to do that
Don't have an account?
sonali verma
System.Assert failing
Hello friends
I have wrriten apex class code to prevent duplicate contacts from being entered.
My apex class code and trigger code is working but my test class is failing.
Can anyone pls help me out?
apex code:
public class No_Account_Duplicates
{
public void Display(list<Account> acc)
{
for(Account a:acc)
{
list<Account> myList=[select Name from Account where Name=:a.Name];
if (myList.size()>0)
a.Name.addError('Account Name Already Exists');
}
}
}
apex trigger code:
trigger trg_NoDuplicates on Account (before Insert)
{
if ((trigger.IsBefore) || (trigger.isInsert))
{
No_Account_Duplicates obj=new No_Account_Duplicates();
obj.Display(trigger.new); //correct
}
}
apex test class:
@istest(SeeAllData=false)
private class NoAccountDuplicates_Test
{
private static testmethod void validatetest()
{
Account a1=new Account();
a1.Name='Contact0';
insert a1;
Account a2=new Account();
a2.Name='Contact0';
insert a2;
try
{
insert a2;
}
catch (DmlException e)
{
//Assert Error Message
System.assert( e.getMessage().contains('Insert failed. First exception on ' + 'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ' +
'Account Name Already Exists'),
e.getMessage() );
e.getDmlStatusCode(0);
}
}
}
I am getting error message as:
Error Message System.AssertException: Assertion Failed: Insert failed. First exception on row 0 with id 0019000001OrsvvAAB;
first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Regards
sonali verma
I have wrriten apex class code to prevent duplicate contacts from being entered.
My apex class code and trigger code is working but my test class is failing.
Can anyone pls help me out?
apex code:
public class No_Account_Duplicates
{
public void Display(list<Account> acc)
{
for(Account a:acc)
{
list<Account> myList=[select Name from Account where Name=:a.Name];
if (myList.size()>0)
a.Name.addError('Account Name Already Exists');
}
}
}
apex trigger code:
trigger trg_NoDuplicates on Account (before Insert)
{
if ((trigger.IsBefore) || (trigger.isInsert))
{
No_Account_Duplicates obj=new No_Account_Duplicates();
obj.Display(trigger.new); //correct
}
}
apex test class:
@istest(SeeAllData=false)
private class NoAccountDuplicates_Test
{
private static testmethod void validatetest()
{
Account a1=new Account();
a1.Name='Contact0';
insert a1;
Account a2=new Account();
a2.Name='Contact0';
insert a2;
try
{
insert a2;
}
catch (DmlException e)
{
//Assert Error Message
System.assert( e.getMessage().contains('Insert failed. First exception on ' + 'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, ' +
'Account Name Already Exists'),
e.getMessage() );
e.getDmlStatusCode(0);
}
}
}
I am getting error message as:
Error Message System.AssertException: Assertion Failed: Insert failed. First exception on row 0 with id 0019000001OrsvvAAB;
first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Regards
sonali verma
you can do above without any code.
create custom field on Account name it as Unique Check
check its unique property (check box true)
create workflow rule to update the Unique Check field with account Name
also you can use duplicate rules https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US (https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US)
All Answers
you can do above without any code.
create custom field on Account name it as Unique Check
check its unique property (check box true)
create workflow rule to update the Unique Check field with account Name
also you can use duplicate rules https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US (https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_create.htm&language=en_US)
next System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false. Both are used in Test Class for “Positive Case Testing” and “Negative Case Testing” with single and multiple records.
Let me know if this helps!