You need to sign in to do that
Don't have an account?
Nihar Sharma
How to insert contact Under Account based on matching fields with each other ?
Hello All,
I have written some code to insert Contact under Account based on text field which is common in both way..
for example : i have abc__c (text) field in Account and Contact, both have same value 'Text1'..if this is same for both way then insert that contact into that account which is matched with Account abc__c..
Please help me out and let me know if i need to go with another way..
Trigger Code :
Apex Class :
Thanks..
I have written some code to insert Contact under Account based on text field which is common in both way..
for example : i have abc__c (text) field in Account and Contact, both have same value 'Text1'..if this is same for both way then insert that contact into that account which is matched with Account abc__c..
Please help me out and let me know if i need to go with another way..
Trigger Code :
trigger ContactInsertUnderAccount on Contact (after insert, after update) { if (trigger.isAfter || trigger.isBefore) { List<Contact> toUpdate = new List<Contact>(); for (Contact record : trigger.new) if (record.abc__c != null && record.AccountId == null) toUpdate.add(record); contactInsertUnderAccount.associateExternalAccount(toUpdate); } }
Apex Class :
Public Class ContactInsertUnderAccount{ public static void associateExternalAccount(List<Contact> contacts){ List<Contact> toUpdate = new List<Contact>(); for (Contact record : [select Id, abc__c from Contact]) toUpdate.add(new Contact(Id=record.Id, Account = new Account(abc__c = record.abcl__c))); Database.update(toUpdate); } }
Thanks..
Please find below Trigger on Contact which works as per your requirement.
Let me know if that helps you.
Best Regards,
BALAJI
All Answers
Please find below Trigger on Contact which works as per your requirement.
Let me know if that helps you.
Best Regards,
BALAJI
Use Trigger.new context variables to update fields in the contact object, you don't need to explicitly perform an update DML statement. When the trigger ends, it will implicitly update the data as you have modified the values.
To simplify, Modify the fields you need to update in this block instead of calling contactInsertUnderAccount.associateExternalAccount
if (record.abc__c != null && record.AccountId == null)
{
//update contacts field here
}
Returning back with the same question and that is what is i have a lots of records (like 200000) ?
trigger might be through SOQL exception....
Can we use Batch for the same ? (i am not much familiar with batch :( )
Thanks