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.