2

I have a few simple (and a bit hacky) apex scripts that i use to count /clean up duplicate leads. I haven't used them or my dev environment in about a year, and trying to execute them now triggers compile errors. Nothing has changed in the scripts since they worked. I can't see anything wrong. Please help.

I am running Eclipse Version: Mars.2 Release (4.5.2) and Force.com IDE plugin version 36.0.0.

Using the below script as an example, executing autonomously triggers the following error: Compile error at line 35 column 5 Unexpected token '<'.

IF i create a new Apex class file and paste the scrip in there, i get this completely different syntax error: Unexpected token 'list' at line 8 error

Here's my script:

/*
Delete Duplicate FXWare Leads based on email
*/


//Get a list of all leads that have duplicate email addresses.

// Include all leads
list<AggregateResult> dupLeads = [Select count(id) numDupLeads, email from Lead where isConverted = false and recordTypeId = '012A0000000xnhD' group by email having count(id) > 1];

// Include only Open leads
//list<AggregateResult> dupLeads = [Select count(id) numDupLeads, email from Lead where isConverted = false and recordTypeId = '012A0000000xnhD' and Status = 'Open' group by email having count(id) > 1];


// list for storing the email addresses of duplicate leads
list<string> dupLeadEmails = new list<string>();


//loop through the results and add the email to a list so we can find them again
for(AggregateResult ar : dupLeads)
{
    dupLeadEmails.add(string.valueOf(ar.get('email')));
}


/*
 Get Ids of all duplicate leads, order by email then created date or last modified date, 
 depending on which line is commented.
 */

// Created Date ASC. Choose this to keep the first lead created.
//list<Lead> listLeads = [select id, email from Lead where email in :dupLeadEmails and recordTypeId = '012A0000000xnhD' and isConverted = false order by email, createdDate ASC ];

//Last Modified date DSC. Choose this to keep the recently edited lead
list<Lead> listLeads = [select id, email from Lead where email in :dupLeadEmails and recordTypeId = '012A0000000xnhD' and isConverted = false order by email, LastModifiedDate DSC ];


//List of leads that will be deleted
list<Lead> leadsToDelete = new list<Lead>();

//Store the last email looked at
string lastLeadEmail = 'placeholder12345';

for(Lead l : listLeads)
{
    if(l.email == lastLeadEmail)
    {
        leadsToDelete.add(l);
    }
    //set the variable for the next iteration
    lastLeadEmail = l.email;
}

if(!leadsToDelete.isEmpty())
{
    //delete those suckers.
    System.debug('Number of Duplicates: ' + leadsToDelete.size());
    //delete leadsToDelete;
}
else
{
    System.debug('Nothing to Do!!!!!!');
}

2 Answers 2

3

It turns out that I had a query with a syntax error, 'DSC' instead of 'DESC'. I found the real problem by correctly structuring my code as a class and pasting it into a new class file. thanks to Sfdcfox for the hint.

1
  • I actually just found that myself. It's odd that the error would be so far away from the actual problem (I'd expect something like "unexpected keyword DSC" or something). Glad I was able to help you out, though.
    – sfdcfox
    Commented Mar 8, 2018 at 19:12
0

This code is incomplete. At minimum, you need a class to hold all the code, and this executable code also needs to be in a method.

Overall, you need to add at least four lines of code:

public class DeleteDuplicateFXwareLeads {
  public static void doDeletion() {
    //... existing code here ...
  }
}

The rest of the code looks more or less okay, but without the outer levels of code, this simply won't compile. As written, it looks more like an execute anonymous script.

1
  • It is an execute anonymous script. I only pasted it into a class file to see if it would catch the same syntax errors. I will structure it like a class and see what is caught then.
    – Ken L
    Commented Mar 8, 2018 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .