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
Andrew Keller 30Andrew Keller 30 

Apex Trigger to delete duplicate tasks - help?

I am trying to find a way to delete duplicate tasks assigned to contact owners. Our Salesforce is integrated with HubSpot, (though i'm sure this same issue would occur with Pardot) and any time a marketing email is opened by a contact, a task is created for the contact owner notifying them that the email was opened. 

Problem is, when spam filters trigger 10-15 opens of the same email for one contact, 10-15 tasks get created for the contact owner. 

I'd like to be able to delete a duplicate task with the same subject assigned to the same user on the same day. Salesforce support told me that this would require an Apex Trigger and a developer, but we dont have a devloper in the org. I'm technically our Salesforce Admin, and i'm learning as I go here. 

Can anyone point me to some resources that would help with this? Thanks in advance! 
AbhishekAbhishek (Salesforce Developers) 
Hi Andrew,

If you are new to Code you can try the below Sample code for your requirement,

https://www.mstsolutions.com/technical/preventing-duplicate-records-based-on-multiple-fields-in-salesforce/

For further reference, you can check the below too,

https://developer.salesforce.com/forums/?id=906F000000092IwIAI

https://salesforce.stackexchange.com/questions/208106/how-to-avoid-creating-duplicate-task-when-risk-score-is-changes

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
vishal-negandhivishal-negandhi

Hi Andrew, 

I would go with a different approach and instead of deleting tasks, I'll block them before they are inserted. 

Kindly go through some trigger documentation here : https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro

 

Also added a sample trigger that should help you achieve your goal.
 

trigger manageTasks on Task(before insert){
	// create a formula field on Task that creates a unique identifier such as "Owner:User.Id + '-' + Subject"
	// we will use this identifier to find out duplicates
	
	Set<String> setTaskUniqueIdentifier = new Set<String>();
    Set<String> duplicateIdentifiers = new Set<String>();
	
	// step 1 : here we are iterating through new task(s) and capturing their unique identifier in a set
	for(Task t : trigger.new){
		setTaskUniqueIdentifier.add(t.Unique_Identifier__c);
	}
	system.debug('...setTaskUniqueIdentifier ' + setTaskUniqueIdentifier);
	// step 2 : here we run a query to see if there are any existing tasks with the same unique identifier and created today, if yes we add these identifiers to a new set
	for(Task t : [Select Unique_Identifier__c FROM Task WHERE Unique_Identifier__c IN :setTaskUniqueIdentifier AND CreatedDate = TODAY]){
		duplicateIdentifiers.add(t.Unique_Identifier__c);	
	}
    system.debug('...duplicateIdentifiers ' + duplicateIdentifiers);
    // step 3 : based on what we have in the set from step 2, we will block the matching new tasks with duplicate identifiers
    for(Task t : trigger.new){
        if(duplicateIdentifiers.contains(t.Unique_Identifier__c))
            t.addError('this is a duplicate task');
    }
}

Hope this helps.

 

Regards,

Vishal

Abhishek BansalAbhishek Bansal
Hi Andrew,

You can stop the creation of duplicates tasks with the help of trigger code mentioned below:
trigger checkDuplicates on Task (before insert) {
	Set<String> setOfSubjects = new Set<String>();
	Set<Id> setOfOwner = new Set<Id>();
	
	for(Task newTask : trigger.new) {
		setOfSubjects.add(newTask.Subject);
		setOfOwner.add(newTask.OwnerId);
	}
	if(setOfSubjects.size() > 0 || setOfOwner.size() > 0) {
		Set<String> uniqueIdentifierSet = new Set<String>();
        Date todayDate = Date.today();
        system.debug(todayDate);
        for(Task existingTask : [Select Subject, OwnerId from Task where Subject IN :setOfSubjects AND OwnerId IN :setOfOwner AND CreatedDate = TODAY]) {
            
            String uniqueIdn = existingTask.Subject + existingTask.OwnerId;
            uniqueIdentifierSet.add(uniqueIdn);
        }
        for(Task newTask : trigger.new) {
            String uniqueIdn = newTask.Subject + newTask.OwnerId;
            if(uniqueIdentifierSet.contains(uniqueIdn)){
                newTask.addError('Task already exist');
            }
        }
	}
}
Please let me know if you face any issues with this. You can also reach out to me directly at:
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790.

Thanks,
Abhishek Bansal.