You need to sign in to do that
Don't have an account?
Shruthi 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);
}
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);
}
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.
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);
}
}
}
}
}
if (matchingAccount != null && newContact.Rating__c == matchingAccount.Rating) {}
Rating is not matching remove this condition.