You need to sign in to do that
Don't have an account?
Shruthi 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;
}
}
}
}
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;
}
}
}
}
Try the code given below. I solved all your errors in the below code. Please mark it as Best Answer if the above information was helpful.
Thanks.
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;
}
}
}
}
Error:
- 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.