Hi Akshata,
I am giving you a sample of the code. You can take references from it.
Case 1: Insert
trigger InsertContact on Account (after insert)
{
List <Contact> cntLst = new List<Contact>();
for(Account acc: Trigger.New)
{
Contact cnt = new Contact();
cnt.LastName = 'chirag'+''+ acc.name;
cnt.AccountId=acc.id;
cntLst.Add(cnt);
}
insert cntLst;
}
Case 2: Update
trigger accountcontat on Account (after update){
List<Contact> contactList = new List<Contact>();
List<Contact> contactList1 = new List<Contact>();
for(Account acc: [SELECT Id,(SELECT Id,checkbox__c FROM Contacts) FROM Account WHERE Id in: Trigger.new]){
If(acc.Contacts.size()>0){
contactList.addAll(acc.Contacts);
}
}
for(contact c:contactList){
c.checkbox__c=true;
contactList1.add(c);
}
update contactList1;
}
Case 3: Delete
trigger deleteAccountRecord on Contact (after delete) {
RecordType donorRecType = [SELECT ID FROM RecordType WHERE sObjectType = 'Contact' AND DeveloperName = 'Donor_Contact'];
if(Trigger.isDelete){
List<ID> accIds = new List<ID>();
for(Contact contactToDelete: Trigger.old) {
if (contactToDelete.RecordTypeId != donorRecType.Id ) {
contactIDList.add(contactToDelete.AccountId);
}
}
List<Account> DelAccountRecordList = [select id from Account where Id IN:accIds];
if(DelAccountRecordList.size() >0){
delete DelAccountRecordList ;
}
}
}
Please let me know it is working for you or not,
Please mark it as the Best Answer if it helps you.
Thank You
4 respostas