0

For example

        trigger DisqualifyTestLeads on Lead (after insert, before update) {

        String testString = 'Test';
        for (Lead L : Trigger.New){
            if(L.Name.ContainsIgnoreCase(testString)){ 
            L.Status = 'Disqualified';
            }
    }
}

If the lead name contains the word test then the lead status is updated to Disqualified...

I just get a "method must have a body" error when i try this.

1
  • Can you post the complete Code so that we can have a closure look as Your code seems to be fine?
    – Amit Singh
    Jul 13, 2018 at 3:30

2 Answers 2

8

method must have a body

This usually means mismatched braces. It can occur when you have one too many braces elsewhere, so the compiler thinks you're trying to define a method. For example, the following code won't compile:

public class someClass {
  public void method();
}

Double-check the pairings of your open/close curly brackets.


String testString = 'Test';
for (Lead L : Trigger.New){
if(L.Name.Contains(testString)) 
    L.Status = 'Disqualified';

There's a lot wrong here:

for (Lead L : Trigger.New){

You have an opening {, but no closing }. If this was just because you didn't copy that part, then ignore this part, but I'm pretty sure you're missing a } somewhere.


if(L.Name.Contains(testString)) 
    L.Status = 'Disqualified';

This is a trap waiting to happen. Always use { and } around the statement for the if statement, even if there's only one. With some clever indentation and a lack of curly brackets, you can lead yourself into a trap:

if(L.Name.Contains(testString)) 
    L.Status = 'Disqualified';
    L.Email = '[email protected]';

While you think that both of these statements might only execute if Name contains the string, the truth is, the Email field will always be set, because you forgot the curly brackets. Avoid this trap by getting in the habit:

if(L.Name.Contains(testString)) {
    L.Status = 'Disqualified';
    L.Email = '[email protected]';
}

if(L.Name.Contains(testString)) {

This is a particularly clever trap waiting to happen; the Name field isn't automatically populated in some situations, like a Before Insert trigger, because it'a compound field. You shouldn't rely on that field being populated. This is true for Accounts (with Person Accounts), Contacts, and Leads. Other types of objects don't have that problem.

It's usually sufficient to just check the LastName, which is always a required field:

if(L.LastName.Contains(testString)) {

Finally, "contains" is case-sensitive. That means I can easily get around your code by writing "test" instead of "Test". For this specific case, consider using containsIgnoreCase:

if(L.LastName.containsIgnoreCase(testString)) {

Finally, keep in mind that you might be filtering out legitimate customers if you do this: the Test clan, of which first migrated to the United States in the 17th century, has many different variations, including:

... Testa, Testi, Testini, Testino, Testoni, Testone, Testai, Testaj, Testani, Testabruna, Testadiferro, Testagrossa, Testaquadra ...

You might want to come up with a more elaborate method of filtering out test leads rather than depend on a specific name being entered, such as using a custom field or something else.

5
  • @sfdcfoxthank thank you; I'm still getting the "method must have body" error, what i have in total is this trigger DisqualifyTestLeads on Lead (after insert, before update) { String testString = 'Test'; for (Lead L : Trigger.New){ if(L.Name.ContainsIgnoreCase(testString)){ L.Status = 'Disqualified'; } } }
    – joebob
    Jul 13, 2018 at 16:17
  • @enossir It needs to be "before insert" instead of "after insert", or you'll get a FinalException error.
    – sfdcfox
    Jul 13, 2018 at 16:22
  • @enossir Wait, are you trying to create a class or a trigger?
    – sfdcfox
    Jul 13, 2018 at 16:23
  • I'm trying to create a trigger.
    – joebob
    Jul 13, 2018 at 17:42
  • @enossir Well, your trigger code, as written, works just fine. I think you've probably put the file in the wrong place or something.
    – sfdcfox
    Jul 13, 2018 at 17:42
0

The above should work. Is it an issue of missing brackets at the end of your if statement? Also, Trigger.new returns an array of SObjects. Do you need to cast this into an array of Leads? Hard to tell without seeing the rest of the code. The following debugs as expected.

String testString = 'world';
Account a = new Account(Name='hello world');
if (a.Name.contains(testString)) {
    System.debug('works');
}

You must log in to answer this question.

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