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 

Write a trigger and a handler class

HI Everyone,

Can you help me write a handler class and a trigger for the below 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.
meghanadh Gmeghanadh G
Well for above scenario :
Creating an apex handler class and relate to orphan contact to an existing Accountbbased on the site and Rating criteria. 


Apex Handler class:
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;
                    }
                }
    }
}


Trigger: 
This trigger will fire when a new Contact record is created.

trigger ContactTrigger on Contact(after insert)
{
    ContactTriggerHandler.handleAfterInsert(Trigger.new)
}



 
Shruthi MN 88Shruthi MN 88
I am getting the attached errorsUser-added image
Shruthi MN 88Shruthi MN 88
HI 
I have written the below code. I have corrected the errors and now the code is not working. B elow is the trigger senario:.

 
I have two feilds on contact Site__c and Rating__c. ONce a contact gets created or updtaed the contact should search for the site name given in Site__C feild with account Site feild. Then the contact should get associated with the account and rating should be updated as per contact Rating__c feild.
Handler Class

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);     
                        }
                    } 
                }
            }
            if (!contactsToUpdate.isEmpty()) {
                
                update contactsToUpdate;                
            }
        }  
    }  

Trigger
trigger ContactTrigger on Contact(after insert)
{
    ContactTriggerHandler.handleAfterInsert(Trigger.new);
}