You need to sign in to do that
Don't have an account?
ajay ambati
trigger not firing on chekbox feild i
i am entering new values in account if phone is anything in account that must be same related contact phone and create related contact for that account.so its firing. but, after this i want to check the checkbox field in account thats not firing pls hlp me
trigger creatingcasebasedonaccounttrigger on Account (after insert)
{
list<schema.contact> lst = new list<schema.contact>();
list<account> gh = new list<account>();
list<Id> listIds = new List<Id>();
//schema.accc=new schema.contact();
if(trigger.isafter)
{
if(trigger.isinsert)
{
for(account acc:trigger.new)
{
System.debug('@@@@id is: ' + acc.Id);
System.debug('####name is : ' + acc.Name);
if(acc.phone !=null)
{
schema.contact cs = new schema.contact();
//cs.Account=acc.Name;
cs.lastname=acc.name;
//cs.closed Date='';
cs.accountid =acc.id;
cs.phone=acc.phone;
cs.contactrelaioncheckbox__c=true;
lst.add(cs);
}
}
}
//acc.CHECKEDACCOUNT__c=true;
insert lst;
list<account> lk =[select id,name,CHECKEDACCOUNT__c from account where id in:listIds];
for(account an:lk)
{
an.CHECKEDACCOUNT__c=true;
gh.add(an);
}
}
update gh;
}
trigger creatingcasebasedonaccounttrigger on Account (after insert)
{
list<schema.contact> lst = new list<schema.contact>();
list<account> gh = new list<account>();
list<Id> listIds = new List<Id>();
//schema.accc=new schema.contact();
if(trigger.isafter)
{
if(trigger.isinsert)
{
for(account acc:trigger.new)
{
System.debug('@@@@id is: ' + acc.Id);
System.debug('####name is : ' + acc.Name);
if(acc.phone !=null)
{
schema.contact cs = new schema.contact();
//cs.Account=acc.Name;
cs.lastname=acc.name;
//cs.closed Date='';
cs.accountid =acc.id;
cs.phone=acc.phone;
cs.contactrelaioncheckbox__c=true;
lst.add(cs);
}
}
}
//acc.CHECKEDACCOUNT__c=true;
insert lst;
list<account> lk =[select id,name,CHECKEDACCOUNT__c from account where id in:listIds];
for(account an:lk)
{
an.CHECKEDACCOUNT__c=true;
gh.add(an);
}
}
update gh;
}
try this once.
Try this code.
Let me know if it helps.
Make it as best answer if it helps
Thanks.
You forgot to populate listIds thats the main reason.
Thanks.
trigger AccountTrigger on Account (after insert) {
List<Contact> contactList = new List<Contact>();
for(Account acc : Trigger.new) {
Contact con = new Contact();
//All fields mapping here.
//con.firstname = account.name
contactList.add(con);
acc.CHECKEDACCOUNT__c = true;
}
insert contactList;
}
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger creatingcasebasedonaccounttrigger caused an unexpected exception, contact your administrator: creatingcasebasedonaccounttrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0012800000sJlLXAA0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, record already exists: []: Trigger.creatingcasebasedonaccounttrigger: line 43, column 1
You have active validation rule on account.Deactivate it and tryonce.
Let me know if it helps.
If it helps make it as best answer.
Thanks.
trigger AccountTrigger on Account (after insert) {
List<Contact> contactList = new List<Contact>();
for(Account acc : Trigger.new) {
Contact con = new Contact();
//All fields mapping here.
//con.firstname = account.name
contactList.add(con);
}
List<Account> accList = [select id,CHECKEDACCOUNT__c from Account where id in :Trigger.newMap.keySet()];
for(Account acc : accList) {
acc.CHECKEDACCOUNT__c = true;
}
insert accList;
}
Here you may face recusive try to user static variable to avoid recursive