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

Testclass for trgger code
Hai hall
i got only 88% code only
i wrote my trigger code & testclass below please help me.
Red mark show only a.name.adderror in code coverage.
trigger code:
trigger validationoncontact on Account (before insert) {
set<string>setv=new set<string>();
// soql query to return the no of records.
list<contact>con=[select id,lastname from contact];
// if size of the records is more than 0 ,then proceed our logic
if(con.size()>0)
{
// Return the number of records one by one
for(contact c:con)
{
// add a contact lastname to setv to comapre the values.
setv.add(c.lastname);
}
}
// "trigger.new" which holds the list of new records, whatever trying to insert the records.
for(account a:trigger.new){
// it returns a true if the string contains specfied character otherwise it return false.
if(setv.contains(a.name)){
a.name.adderror('Duplicate : Already contact is Existed with this name');
}
}
}
test class
@istest
public class testclassforvalidationcontact {
@istest
static void testme(){
contact c=new contact(lastname='arjun');
insert c;
string names='';
list<contact>cc=[select id,lastname from contact where id=:c.id];
for(contact c1:cc){
names=c1.lastname;
}
account a1=new account(name='mallik');
try{
insert a1;
system.assertnotEquals(a1.name, names);
}catch(exception e){
System.debug('should not be created duplicate record');
}
}
}
i got only 88% code only
i wrote my trigger code & testclass below please help me.
Red mark show only a.name.adderror in code coverage.
trigger code:
trigger validationoncontact on Account (before insert) {
set<string>setv=new set<string>();
// soql query to return the no of records.
list<contact>con=[select id,lastname from contact];
// if size of the records is more than 0 ,then proceed our logic
if(con.size()>0)
{
// Return the number of records one by one
for(contact c:con)
{
// add a contact lastname to setv to comapre the values.
setv.add(c.lastname);
}
}
// "trigger.new" which holds the list of new records, whatever trying to insert the records.
for(account a:trigger.new){
// it returns a true if the string contains specfied character otherwise it return false.
if(setv.contains(a.name)){
a.name.adderror('Duplicate : Already contact is Existed with this name');
}
}
}
test class
@istest
public class testclassforvalidationcontact {
@istest
static void testme(){
contact c=new contact(lastname='arjun');
insert c;
string names='';
list<contact>cc=[select id,lastname from contact where id=:c.id];
for(contact c1:cc){
names=c1.lastname;
}
account a1=new account(name='mallik');
try{
insert a1;
system.assertnotEquals(a1.name, names);
}catch(exception e){
System.debug('should not be created duplicate record');
}
}
}
Try to insert an Account whose Name == Contact's LastName.
You have written the code as, error if the account name matchs any of the contacts Lastname.
Thanks.
try this:
Thanks
Ankur Saini
http://mirketa.com