You need to sign in to do that
Don't have an account?
ABC XYZ 39
Urgently Need help with triggers - Both after insert and After update trigger firing at the same time for new records created -REPOSTING 3rd time
Hi,
Basic Logic:
Create Contact Scenario -
Conditions - Last Name should not be same as Account Name, Contact type is Customer
Everytime a new contact is created meeting the above conditions, it is written to Queue object
In this case, Description would be 'New Contact' (to be written into Queue object)
Update Contact Scenario -
2 scenarios here -
1) In addition to abouve conditions being valid, if custom field PID is missing and any of the
fields in Contact record are updated, push the record to Queue object.
In this case, Description would be 'PID Missing'
2) In addition to above conditions - Last Nane not same as Account Name, Contact type being 'Customer'
type, if custom fields A, B, C or D are changed on the existing record, push the record to
Queue object.
In this case, Description would be 'Updated Contact'
Behavior Observed Now :
Whenever a new contact is created, 2 copies of the same record are inserted into the Queue object. This I am assuming is happening because 'after insert' and 'after update' triggers aer firing for every new contact created.
Insert Scenario(New Contact Creation) - Update scenario triggered - 'MDM Id Missing' message observed; Record inserted into Queue object twice
Update Scenario (PID null and record updated) - Working as expected - 'MDM Id Missing' message written into Queue object
Update scenario ( A,B,C or D field values change) - Update Scenario triggerd - 'Update contact' message written into Queue object
I don't understand why this is happening. There are no workflows impacting the behavior . It has to do with logic in the class below or the way I am invoking the trigger.
Controller Class and Trigger Invocation are below.
I am trying to fix this code for sometime without any luck. I need to fix it as soon as possible. I am new to programming and can't figure out how to make this functionality work. I have posted the same queury for past couple of days. I will greatly appreciate if you could help identify and fix this issue..Thanks.
// Controller Class with processContact method
public class ContactController {
public void processContact(Map<Id, Contact> newContactMap, Map<Id, Contact> oldContactMap)
{
List<Queue__c> lstQ = new List<Queue__c>();
Id idContactCustomerRecType;
Map<Id, String> mapIdToAccountName = new Map<Id, String>();
List<RecordType> lstRecType = new List<RecordType>([SELECT Id FROM RecordType WHERE Name = 'Customer' AND SobjectType = 'Contact']);
if(!lstRecType.isEmpty())
idContactCustomerRecType = lstRecType[0].Id;
if(idContactCustomerRecType != null)
{
// we can't fetch parent fields directly, so collecting account Ids first
for(Contact objContact : newContactMap.values())
{
if(objContact.AccountId != null)
mapIdToAccountName.put(objContact.AccountId, null);
}
}
// iterate over account with matching collected Ids and fetch names
for(Account objAccount : [SELECT Id, Name FROM Account WHERE Id IN : mapIdToAccountName.keySet()])
{
mapIdToAccountName.put(objAccount.Id, objAccount.Name);
}
// loop again to perform business logic
for(Contact objContact : newContactMap.values())
{
/* 1. If Contact RecordType is Customer AND if LastName != Account.Name, insert Queue **/
if( objContact.RecordTypeId == idContactCustomerRecType
&& objContact.AccountId != null && objContact.LastName != mapIdToAccountName.get(objContact.AccountId)
{
if(Trigger.isUpdate)
{
if(label.Unauthorized_User!=objContact.lastmodifiedbyid)
{
if(objContact.A__c!= oldContactMap.get(objContact.Id).A__c
|| objContact.B__c!= oldContactMap.get(objContact.Id).B__c
|| objContact.D__c!= oldContactMap.get(objContact.Id).D__c)
{
objQ.Description__c= 'Updated Contact';
objQ.Record_Id__c = objContact.Id;
objQ.Notification_Timestamp__c= objContact.CreatedDate;
lstQ.add(objQ);
}
else if(objContact.PID__c == null)
{
objQ.Description__c= 'PID Missing';
objQ.Record_Id__c = objContact.Id;
objQ.Notification_Timestamp__c= objContact.CreatedDate;
lstQ.add(objQ);
}
}
}
if(Trigger.isInsert)
{
objQ.Description__c= 'New Contact';
objQ.Record_Id__c = objContact.Id;
objQ.Notification_Timestamp__c= objContact.CreatedDate;
lstQ.add(objQ);
}
}
}
if(!lstQ.isEmpty())
insert lstQ;
}
}
Trigger Invocation :
Contact - After Insert
if(Trigger.isInsert && Trigger.isAfter){
ContactController.processContact(Trigger.newMap, Trigger.oldMap);
}
Contact - After Update
if(Trigger.isUpdate && Trigger.isAfter){
ContactController.processContact(Trigger.newMap, Trigger.oldMap);
}
Basic Logic:
Create Contact Scenario -
Conditions - Last Name should not be same as Account Name, Contact type is Customer
Everytime a new contact is created meeting the above conditions, it is written to Queue object
In this case, Description would be 'New Contact' (to be written into Queue object)
Update Contact Scenario -
2 scenarios here -
1) In addition to abouve conditions being valid, if custom field PID is missing and any of the
fields in Contact record are updated, push the record to Queue object.
In this case, Description would be 'PID Missing'
2) In addition to above conditions - Last Nane not same as Account Name, Contact type being 'Customer'
type, if custom fields A, B, C or D are changed on the existing record, push the record to
Queue object.
In this case, Description would be 'Updated Contact'
Behavior Observed Now :
Whenever a new contact is created, 2 copies of the same record are inserted into the Queue object. This I am assuming is happening because 'after insert' and 'after update' triggers aer firing for every new contact created.
Insert Scenario(New Contact Creation) - Update scenario triggered - 'MDM Id Missing' message observed; Record inserted into Queue object twice
Update Scenario (PID null and record updated) - Working as expected - 'MDM Id Missing' message written into Queue object
Update scenario ( A,B,C or D field values change) - Update Scenario triggerd - 'Update contact' message written into Queue object
I don't understand why this is happening. There are no workflows impacting the behavior . It has to do with logic in the class below or the way I am invoking the trigger.
Controller Class and Trigger Invocation are below.
I am trying to fix this code for sometime without any luck. I need to fix it as soon as possible. I am new to programming and can't figure out how to make this functionality work. I have posted the same queury for past couple of days. I will greatly appreciate if you could help identify and fix this issue..Thanks.
// Controller Class with processContact method
public class ContactController {
public void processContact(Map<Id, Contact> newContactMap, Map<Id, Contact> oldContactMap)
{
List<Queue__c> lstQ = new List<Queue__c>();
Id idContactCustomerRecType;
Map<Id, String> mapIdToAccountName = new Map<Id, String>();
List<RecordType> lstRecType = new List<RecordType>([SELECT Id FROM RecordType WHERE Name = 'Customer' AND SobjectType = 'Contact']);
if(!lstRecType.isEmpty())
idContactCustomerRecType = lstRecType[0].Id;
if(idContactCustomerRecType != null)
{
// we can't fetch parent fields directly, so collecting account Ids first
for(Contact objContact : newContactMap.values())
{
if(objContact.AccountId != null)
mapIdToAccountName.put(objContact.AccountId, null);
}
}
// iterate over account with matching collected Ids and fetch names
for(Account objAccount : [SELECT Id, Name FROM Account WHERE Id IN : mapIdToAccountName.keySet()])
{
mapIdToAccountName.put(objAccount.Id, objAccount.Name);
}
// loop again to perform business logic
for(Contact objContact : newContactMap.values())
{
/* 1. If Contact RecordType is Customer AND if LastName != Account.Name, insert Queue **/
if( objContact.RecordTypeId == idContactCustomerRecType
&& objContact.AccountId != null && objContact.LastName != mapIdToAccountName.get(objContact.AccountId)
{
if(Trigger.isUpdate)
{
if(label.Unauthorized_User!=objContact.lastmodifiedbyid)
{
if(objContact.A__c!= oldContactMap.get(objContact.Id).A__c
|| objContact.B__c!= oldContactMap.get(objContact.Id).B__c
|| objContact.D__c!= oldContactMap.get(objContact.Id).D__c)
{
objQ.Description__c= 'Updated Contact';
objQ.Record_Id__c = objContact.Id;
objQ.Notification_Timestamp__c= objContact.CreatedDate;
lstQ.add(objQ);
}
else if(objContact.PID__c == null)
{
objQ.Description__c= 'PID Missing';
objQ.Record_Id__c = objContact.Id;
objQ.Notification_Timestamp__c= objContact.CreatedDate;
lstQ.add(objQ);
}
}
}
if(Trigger.isInsert)
{
objQ.Description__c= 'New Contact';
objQ.Record_Id__c = objContact.Id;
objQ.Notification_Timestamp__c= objContact.CreatedDate;
lstQ.add(objQ);
}
}
}
if(!lstQ.isEmpty())
insert lstQ;
}
}
Trigger Invocation :
Contact - After Insert
if(Trigger.isInsert && Trigger.isAfter){
ContactController.processContact(Trigger.newMap, Trigger.oldMap);
}
Contact - After Update
if(Trigger.isUpdate && Trigger.isAfter){
ContactController.processContact(Trigger.newMap, Trigger.oldMap);
}
You are calling same method of COntactController class from your trigger and thats why youa re facing problem.
Call two separate methods and your problem should be resolved assuing that the logic in controller is correct.
Something like this:
in processContact1 method write only that part of code that should execute for newly inserted records
and in processContact2 method write only that part of code that should execute for recently updated records.
Mark this as Best Answer, if this solves your problem.
Hope it works now.
Trigger:
Correct typing mistakes, syntax errors if any.
Add additional conditions in the code, depending on your requirement.
Mark this as Best Answer, if this helps.