You need to sign in to do that
Don't have an account?
delete trigger.new
Hi,
I am new to apex and creating a trigger to delete newly inserted contacts with duplicate emails. Actually since emails have to be unique in our salesforce instance, we are trying to do a cleanup right after new contacts are inserted.
Here's my trigger and I cannot get it saved due to this error:
Error: Compile Error: unexpected token: global at line 28 column 0
Could any please help?? Thank you!!
****trigger****
trigger manageEmailAfterInsert on Contact (after insert, after update) {
/* This trigger is created by Heller to delete the duplicate contacts being inserted */
Set<String> setEmail = new Set<String>();
Map<String, Id> contactMap1 = new Map<String, Id>();
List<String> ToBeDeleted = new List<String>();
for (Contact Contact : trigger.new)
{
setEmail.add(Contact.Email);
contactMap1.put(Contact.Email,Contact.Id);
}
for(Contact dupContact : [select email from Contact where email in: setEmail])
{
if(contactMap1.containsKey(dupContact.Email))
{
ToBeDeleted.add(contactMap1.get(dupContact.Email));
}
}
If(ToBeDeleted.size() > 0)
//delete ToBeDeleted;
DeleteDupContact.deleteRecords();
}
global class DeleteDupContact {
@future
public deleteRecords(String[] ToBeDeleted) {
try {
DeleteResult[] deleteResults = connection.delete(ToBeDeleted);
for (int i = 0; i < deleteResults.length; i++) {
DeleteResult deleteResult = deleteResults[i];
if (deleteResult.isSuccess()) {
System.out
.println('Deleted Record ID: ' + deleteResult.getId());
} else {
// Handle the errors.
// We just print the first error out for sample purposes.
Error[] errors = deleteResult.getErrors();
if (errors.length > 0) {
System.out.println('Error: could not delete ' + 'Record ID '
+ deleteResult.getId() + '.');
System.out.println(' The error reported was: ('
+ errors[0].getStatusCode() + ') '
+ errors[0].getMessage() + '\n');
}
}
}
} catch (ConnectionException ce) {
ce.printStackTrace();
}
}
try Database.DeleteResult[] instead of DeleteResult[]
same 2 lines farther
All Answers
When i tried to save that as a separate class, i got this error: Invalid type: DeleteResult at line 6 column 11(DeleteResult[] deleteResults = connection.delete(ToBeDeleted);)
but ToBeDeleted is actually a list of IDs...
Technically it's a list of Strings :
try
Also you didn't give the argument to the call DeleteDupContact.deleteRecords() at then end of the trigger, but I guess you added it.
[edit]
same in the trigger :
try Database.DeleteResult[] instead of DeleteResult[]
same 2 lines farther
You can however put a public class inside the trigger
The problem is that you are only deleting duplicates within the context of the trigger. So if you update multiple records you will only be deleting the records that are duplicated within the transaction.
Say I have 2 records in the data base but am only updating one of them, nothing will be deleted.
If you are mass updating, you are only checking with the 200 records for that batch size.
Are you wanting to check all existing contacts and de dup when any record is inserted or updated? If so, you will have to
1. Query the database for all records that contain email addresses from the trigger records and EXCLUDE the record within the trigger context.
2. Check the trigger context for dups.
3. Once dups are identified, you will have to figure out how to handle them. Keep oldest, most recent, etc.
4. Send the list of IDs to delete to the future class you created once you get it working or have a field to indicate it needs deleted and run a batch process to delete those marked accordingly.
You are right! Thank you very much!