+ Start a Discussion
Vikram Singh 157Vikram Singh 157 

Error in Triggers as given below for this requirement :--"Delete all the contact with the same name as of account name that we are inserting"

trigger Conrecords on Account (before insert)
 {
 List<String> name1 = new List<String>();
    for(Account a : trigger.new)
 {
    myname.add(a.name);
    } 
List<contact> contact1=[select id,name from contact where name in : name1];
 delete contact1;
}
Best Answer chosen by Vikram Singh 157
Devanshu soodDevanshu sood
trigger Conrecords on Account (before insert)
 {
 List<String> name1 = new List<String>();
    for(Account a : trigger.new)
 {
    name1.add(a.name);
    } 
List<contact> contact1=[select id,name from contact where name in : name1];
if(contacts.size() > 0)
 delete contact1;
}

try this,
select as best answer if it works
thanks

All Answers

Waqar Hussain SFWaqar Hussain SF
trigger Conrecords on Account (after insert)
 {
 List<String> name1 = new List<String>();
    for(Account a : trigger.new)
 {
    myname.add(a.name);
    } 
List<contact> contacts = new list<contact>([select id,name from contact where name in : myname]);
if(contacts.size() > 0)
 delete contacts;
}

try above code.
Devanshu soodDevanshu sood
trigger Conrecords on Account (before insert)
 {
 List<String> name1 = new List<String>();
    for(Account a : trigger.new)
 {
    name1.add(a.name);
    } 
List<contact> contact1=[select id,name from contact where name in : name1];
if(contacts.size() > 0)
 delete contact1;
}

try this,
select as best answer if it works
thanks
This was selected as the best answer
Devanshu soodDevanshu sood
myname variable is not defined anywhere use name1 instead.