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
Nihar SharmaNihar 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 :
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..
Best Answer chosen by Nihar Sharma
BALAJI CHBALAJI CH
Hi Nihar Sharma,

Please find below Trigger on Contact which works as per your requirement.
 
trigger ContactInsertUnderAccount on Contact (before insert, before update)
{
    Map <string, Id> AccountTextIdMap = new Map<String,Id>(); //Map with Text field abc__c as key and Account Id as value
    for(Account ac : [select id, abc__c from Account])
    {
        AccountTextIdMap.put(ac.abc__c, ac.id);
    }
    
//Looping through all contacts before insert or before update 
    for(Contact con : Trigger.New)
    {
        //If condition to check abc__c field is not null and AccountId is null
        if(con.abc__c != Null && con.AccountId == Null)
        {
            //Getting value of AccountId using abc__c field
            con.AccountId = AccountTextIdMap.get(con.abc__c);
        }
    }
}

Let me know if that helps you.

Best Regards,
BALAJI

All Answers

BALAJI CHBALAJI CH
Hi Nihar Sharma,

Please find below Trigger on Contact which works as per your requirement.
 
trigger ContactInsertUnderAccount on Contact (before insert, before update)
{
    Map <string, Id> AccountTextIdMap = new Map<String,Id>(); //Map with Text field abc__c as key and Account Id as value
    for(Account ac : [select id, abc__c from Account])
    {
        AccountTextIdMap.put(ac.abc__c, ac.id);
    }
    
//Looping through all contacts before insert or before update 
    for(Contact con : Trigger.New)
    {
        //If condition to check abc__c field is not null and AccountId is null
        if(con.abc__c != Null && con.AccountId == Null)
        {
            //Getting value of AccountId using abc__c field
            con.AccountId = AccountTextIdMap.get(con.abc__c);
        }
    }
}

Let me know if that helps you.

Best Regards,
BALAJI
This was selected as the best answer
Prachi SPrachi S
There is one crucial problem that I see in your trigger i.e. it has a DML statement at the end. You cannot write a DML inside a trigger block or its helper class. This is going to cause recursion and ultimately will fail to execute.
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
}
Nihar SharmaNihar Sharma
Thanks a Lot Prachi & Balaji !!
 
Nihar SharmaNihar Sharma
Hey Balaji,

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