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
Shruthi MN 88Shruthi MN 88 

Errors

Hi Everyone,

I have written a handler class for the below trigger senario:

When a Contact (Account is null) is created with rating(a custom field - text type), check for existing Account record with Site(ex: www.emaildomain.com) and rating (as contact's rating), and relate that orphan contact with that account record.

I am getting the attached errors

public class ContactTriggerHandler 
{
    public static void handleAfterInsert(List<Contact> newContacts)
    {
            Map<String, String> contactSiteRatingMap = new Map<String, String>();
                For(Contact newContact: newContacts)
                {
                    if(newContact.AccountId == null && newContact.Site != null && newContact.Rating__c != null)
                    {
                        contactSiteRatingMap.put(newContact.Site, newContact.Rating__c);
                    }
                }
                if(!contactSiteRatingMap.isEmpty())
                {
                List<Account> matchingAccounts = [SELECT Id, Site, Rating__C from Account
                                                WHERE Site IN : contactSiteRatingMap.keySet() AND 
                                                Rating__c IN : contactSiteRatingMap.values()];
                
                List<Contact> contactsToUpdate = new List<Contact>();
                for(Contact newContact : newContacts)
                {
                    if(newContact.AccountId == null && newContact.site != null && newContact.Rating__c != null)
                    {
                        for(Account matchingAccount : matchingAccounts)
                        {
                            if(newContact.Site == matchingAccount.site && newContact.Rating__c == matching.Rating__c)
                            {
                                newContact.AccountId = matchingAccount.Id;
                                contactsToUpdate.add(newContact);
                                break;
                            }
                        }
                    }
                    }
                    if(!contactsToUpdate.isEmpty())
                    {
                        update contactsToUpdate;
                    }
                }
    }
}
User-added image
HarshHarsh (Salesforce Developers) 
Hi Shruthi,

Try the code given below. I solved all your errors in the below code.
public class ContactTriggerHandler 
{
    public static void handleAfterInsert(List<Contact> newContacts)
    {
            Map<String, String> contactSiteRatingMap = new Map<String, String>();
                For(Contact newContact: newContacts)
                {
                    if(newContact.AccountId == null && newContact.Site__c != null && newContact.Rating__c != null)
                    {
                        contactSiteRatingMap.put(newContact.Site__c, newContact.Rating__c);
                    }
                }
                if(!contactSiteRatingMap.isEmpty())
                {
                List<Account> matchingAccounts = [SELECT Id, Site, Rating__c from Account WHERE Site IN : contactSiteRatingMap.keySet() AND Rating__c IN : contactSiteRatingMap.values()];
                
                List<Contact> contactsToUpdate = new List<Contact>();
                for(Contact newContact : newContacts)
                {
                    if(newContact.AccountId == null && newContact.Site__c != null && newContact.Rating__c != null)
                    {
                        for(Account matchingAccount : matchingAccounts)
                        {
                            if(newContact.Site__c == matchingAccount.Site && newContact.Rating__c == matchingAccount.Rating__c)
                            {
                                newContact.AccountId = matchingAccount.Id;
                                contactsToUpdate.add(newContact);
                                break;
                            }
                        }
                    }
                    }
                    if(!contactsToUpdate.isEmpty())
                    {
                        update contactsToUpdate;
                    }
                }
    }
}
Please mark it as Best Answer if the above information was helpful.

Thanks.
 
Shruthi MN 88Shruthi MN 88
User-added image


Errors are resolved but code is not working. AccountName feild should get updated with the account whos Site feild has the same SIte name as Site__c on Contact

public class ContactTriggerHandler 
{
    public static void handleAfterInsert(List<Contact> newContacts)
    {
            Map<String, String> contactSiteRatingMap = new Map<String, String>();
                For(Contact newContact: newContacts)
                {
                    if(newContact.AccountId == null && newContact.Site__c != null && newContact.Rating__c != null)
                    {
                        contactSiteRatingMap.put(newContact.Site__c, newContact.Rating__c);
                    }
                }
                if(!contactSiteRatingMap.isEmpty())
                {
                List<Account> matchingAccounts = [SELECT Id, Site, Rating from Account WHERE Site IN : contactSiteRatingMap.keySet() AND Rating IN : contactSiteRatingMap.values()];
                
                List<Contact> contactsToUpdate = new List<Contact>();
                for(Contact newContact : newContacts)
                {
                    if(newContact.AccountId == null && newContact.Site__c != null && newContact.Rating__c != null)
                    {
                        for(Account matchingAccount : matchingAccounts)
                        {
                            if(newContact.Site__c == matchingAccount.Site && newContact.Rating__c == matchingAccount.Rating)
                            {
                                newContact.AccountId = matchingAccount.Id;
                                contactsToUpdate.add(newContact);
                                break;
                            }
                        }
                    }
                    }
                    if(!contactsToUpdate.isEmpty())
                    {
                        update contactsToUpdate;
                    }
                }
    }
}
HarshHarsh (Salesforce Developers) 
Hi Shruthi, I have made some changes to your code after that your query is working as expected but after that, the code throws the error.
public class ContactTriggerHandler 
{
    public static void handleAfterInsert(List<Contact> newContacts)
    {
         list<string> contactsite = new List<string>();
         list<string> contactrating = new List<string>();
            Map<String, String> contactSiteRatingMap = new Map<String, String>();
                For(Contact newContact: newContacts)
                {
                   
                    if(newContact.AccountId == null && newContact.Site__c != null && newContact.Rating__c != null)
                    {

                        system.debug(newContact.Site__c);
                 //      contactSiteRatingMap.put(newContact.Site__c, newContact.Rating__c);
                        contactsite.add(newContact.Site__c);
                            contactrating.add(newContact.Rating__c);
                    }
                }
                if(!contactsite.isEmpty() )
                {
                    
                List<Account> matchingAccounts = [SELECT Id, Site, Rating__c from Account WHERE Site =: contactsite AND Rating__c =: contactrating];
                 system.debug(matchingAccounts);
                  
                List<Contact> contactsToUpdate = new List<Contact>();
                for(Contact newContact : newContacts)
                {
                    if(newContact.AccountId == null && newContact.Site__c != null && newContact.Rating__c != null)
                    {
                        for(Account matchingAccount : matchingAccounts)
                        {
                            if(newContact.Site__c == matchingAccount.Site && newContact.Rating__c == matchingAccount.Rating__c)
                            {
                                newContact.AccountId = matchingAccount.Id;
                                contactsToUpdate.add(newContact);
                               
                            }
                        }
                    }
                    }
                    if(!contactsToUpdate.isEmpty())
                    {
                        update contactsToUpdate;
                    }
                }
    }
}

Error:
ContactTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only​​​​​​.
  • This is because you are in an after-insert/update trigger and the records are read-only in that context as they have been written, but not committed, to the database.
  • Unfortunately, your trigger is relying on the ids of the records, which means you won't be able to use them before insert as the ids won't be populated at that time (as the records haven't been written to the database at that point so while you can write to them, database generated fields aren't populated).
  • In this instance, you'll need to clone the record (or query anew via SOQL) and make changes to the new copy of the record, then execute the update against the new copies.
You can refer Stack Exchange link for the error.

System.FinalException: Record is read-only (https://salesforce.stackexchange.com/questions/23922/system-finalexception-record-is-read-only-trigger-updatecompetitors-line-24)

 Please mark it as Best Answer if the above information was helpful.

Thanks.