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 

Handler class and trigger

Hi Everyone,

I have to write a handler class and a trigger on the below senario

Create two feilds on Contact Ratilg__c and Site__c. NOw once a contact is created or updated the contact should search for the site name of the attacunt which is entered on site__c  of the oncate and associated the account with the contact. Then update the accounts Rating feild  as per the contact Rating__c feild.

Below is the code that I have written

Handl;er 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);
}

 
Deepak Singh 211Deepak Singh 211
Hi,
If you are using after Insert Event then why you are using Update Commad? I think it will work without using update command if you are updating the Contact records.
Shruthi MN 88Shruthi MN 88
I rempved update it is still not working

public class ContactTriggerHandler {

    public static void handleAfterInsert(List<Contact> newContacts) {

        Map<String, Account> siteToAccountMap = new Map<String, Account>();
        Set<String> contactSites = new Set<String>();
        List<Contact> contactsToUpdate = new List<Contact>();
List<Account> matchingAccounts = [SELECT Id, Site FROM Account WHERE Site IN :contactSites];
        for (Contact newContact : newContacts) {
            if (newContact.AccountId == null && newContact.Site__c != null && newContact.Rating__c != null) {
                siteToAccountMap.put(newContact.Site__c, null);
            }
        }

        if (!siteToAccountMap.isEmpty()) {
            for (Account matchingAccount : [
                SELECT Id, Site, Rating
                FROM Account
                WHERE Site IN :siteToAccountMap.keySet()
            ]) {
                siteToAccountMap.put(matchingAccount.Site, matchingAccount);
            }

            for (Contact newContact : newContacts) {
                Account matchingAccount = siteToAccountMap.get(newContact.Site__c);

                if (matchingAccount != null && newContact.Rating__c == matchingAccount.Rating) {
                    newContact.AccountId = matchingAccount.Id;
                    contactsToUpdate.add(newContact);
                }
            }
        }
    }
}
Deepak Singh 211Deepak Singh 211
Can you share the error msg?
Shruthi MN 88Shruthi MN 88
I am not getting any error but the account is not getting updated in the conatct in the accpount name feild
Deepak Singh 211Deepak Singh 211

  if (matchingAccount != null && newContact.Rating__c == matchingAccount.Rating) {}

Rating is not matching remove this condition.