function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shekhar 46shekhar 46 

trigger on account to check no contact associated and then create dummy contact

Write a trigger whenever account created, no contact is associated to that account, create dummy contact with contact name as account name. need to check no contact is associated with it and then create dummy contact
Best Answer chosen by shekhar 46
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shekhar,

Yes, the trigger is perfectly correct and you followed best practice as well by writing dml out of For loop.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shekhar,

As per process when Account is getting created there is no chance of contact availale relted to that Account. So you mean a dummy contact should be created every time account is created?

Thanks,
 
shekhar 46shekhar 46
yes sai praveen.

Can u tell me my code is right for above scneario
trigger inserttrigger on account(after insert){

list<contact> listcon= new list<contact>();

for (account A: trigger.new){
contact C =new contact();
 
c.accountID=A.id;
c.firstnam='test';
c.lastname=A.name;

listcon.add(c);
}
insert listcon;
}
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shekhar,

Yes, the trigger is perfectly correct and you followed best practice as well by writing dml out of For loop.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer