You need to sign in to do that
Don't have an account?
John Athitakis
Apex code is firing twice when invoked by Ringlead
So I have code that creates an event when a field is populated (on create or update). This is a way for secondary systems to trigger event creations. The logic is on create if the field isnt null, make an event with some attributes. On update, only do so if the new value differs from the old AND is not null. For some reason in my testing, it appears fine, but Ringlead (a third party tool thats populating this field) is causing double activities to create and I'm confused as to how/why.
/*
Author: John Athitakis
Synopsis: When a new or existing Lead has a special string field populated, an event is created with that string as its subject line
*/
trigger LeadCreateEvent on Lead (after insert, after update) {
// Logic: On insert if the Event Activity Field is Populated, Create an Event.
if(Trigger.IsInsert) {
for (Lead UpdatedLead: Trigger.new) {
if(UpdatedLead.Event_Activity_Subject__c!=null){
Event event=new Event(
OwnerId = UpdatedLead.CreatedById,
WhoId = UpdatedLead.id,
StartDateTime = UpdatedLead.CreatedDate,
EndDateTime = UpdatedLead.CreatedDate,
Subject = UpdatedLead.Event_Activity_Subject__c
);
insert event ;
}
}
}
// Logic: On update, if there is a change in the Event Activity Field & it is not Null, Create an Event
if(Trigger.IsUpdate){
for (Lead UpdatedLead2: Trigger.new) {
Lead PriorLead = Trigger.oldMap.get(UpdatedLead2.ID);
if (UpdatedLead2.Event_Activity_Subject__c != PriorLead.Event_Activity_Subject__c && UpdatedLead2.Event_Activity_Subject__c!=null){
Event event2 = new Event(
OwnerId = UpdatedLead2.CreatedById,
WhoId = UpdatedLead2.id,
StartDateTime = UpdatedLead2.CreatedDate,
EndDateTime = UpdatedLead2.CreatedDate,
Subject = UpdatedLead2.Event_Activity_Subject__c
);
insert event2 ; }}
/*
Author: John Athitakis
Synopsis: When a new or existing Lead has a special string field populated, an event is created with that string as its subject line
*/
trigger LeadCreateEvent on Lead (after insert, after update) {
// Logic: On insert if the Event Activity Field is Populated, Create an Event.
if(Trigger.IsInsert) {
for (Lead UpdatedLead: Trigger.new) {
if(UpdatedLead.Event_Activity_Subject__c!=null){
Event event=new Event(
OwnerId = UpdatedLead.CreatedById,
WhoId = UpdatedLead.id,
StartDateTime = UpdatedLead.CreatedDate,
EndDateTime = UpdatedLead.CreatedDate,
Subject = UpdatedLead.Event_Activity_Subject__c
);
insert event ;
}
}
}
// Logic: On update, if there is a change in the Event Activity Field & it is not Null, Create an Event
if(Trigger.IsUpdate){
for (Lead UpdatedLead2: Trigger.new) {
Lead PriorLead = Trigger.oldMap.get(UpdatedLead2.ID);
if (UpdatedLead2.Event_Activity_Subject__c != PriorLead.Event_Activity_Subject__c && UpdatedLead2.Event_Activity_Subject__c!=null){
Event event2 = new Event(
OwnerId = UpdatedLead2.CreatedById,
WhoId = UpdatedLead2.id,
StartDateTime = UpdatedLead2.CreatedDate,
EndDateTime = UpdatedLead2.CreatedDate,
Subject = UpdatedLead2.Event_Activity_Subject__c
);
insert event2 ; }}
You can try below advice to stop the trigger firing twice
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
Also you have to builkify your inserts
add the events to a list before calling the DML operation.
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
All Answers
I'm CPO at RingLead, I just happened to see your post when logging in to post a question of my own. Two questions on this...
1) Which app(s) are you using in particular when this is happening?
2) Do you have "field history tracking" turned on for the Event_Activity_Subject__c field? I would be curious if this field is being updated twice somehow... (insert followed by update).
We opened a case with Ringlead as well--Case #: 00025019
1) Unique Upload App
--We are only using it to update existing Leads, so it should never be triggering the after insert portion.
2) I enabled history tracking today and after a few tests, only see Ringlead updating the field once (i was curious if that was happening or if it might have been updating/nulling/updating the filed but I cant see that behavior).
Any standard testing I'm doing cant replicate what Ringlead seems to be doing. That being if I update the values from X to Y, i only get one new activity.
You can try below advice to stop the trigger firing twice
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
Also you have to builkify your inserts
add the events to a list before calling the DML operation.
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code