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
Hanna BergsmaHanna Bergsma 

Error: Compile Error: static can only be used on methods of a top level type

I am getting an error referencing the third line here "static can only be used on methods of a top level type "

Anyone have any ideas? I reworked this code from someone I found online, I'm not an experienced developer so any help is greatly appreciated!!

'm trying to create an email handler that creates a contact from incoming emails where the contact doesnt exist already

}

@isTest
private class testCreateContactFrmEmail
{static testMethod void testCreateContactFrmEmail() {
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

email.subject = 'Create Contact';
email.plainTextBody = 'FromEmail';
env.fromAddress = 'testemail@gmail.com';

CreateContactFrmEmail creatC = new CreateContactFrmEmail();
creatC.handleInboundEmail(email, env );
}
}
}
SwethaSwetha (Salesforce Developers) 
HI Hanna,
I tried your code in my org and couldn't reproduce the same message.
User-added imageCan you retry saving? Also, the below articles would be helpful in understanding the error message you received:

https://salesforce.stackexchange.com/questions/135865/only-top-level-class-methods-can-be-declared-static
https://salesforce.stackexchange.com/questions/166067/compile-error-line-3-column-30-only-top-level-class-methods-can-be-declared


Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Hanna BergsmaHanna Bergsma
Thanks, unfortunately I am getting a different error now:
System.NullPointerException: Attempt to de-reference a null object

Stack Trace: Class.CreateContactFrmEmail.handleInboundEmail: line 17, column 1 Class.CreateContactTestClass.validateCreateContactFrmEmail: line 12, column 1


This is my Apex Class:

global class CreateContactFrmEmail implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {

Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

String subToCompare = 'Create Contact';

if(email.subject.equalsIgnoreCase(subToCompare))
{
Contact c = new Contact();
c.LastName = email.plainTextBody;
insert c;

// Save attachments, if any
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
Attachment attachment = new Attachment();

attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = c.Id;
insert attachment;
}

//Save any Binary Attachment
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
Attachment attachment = new Attachment();

attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = c.Id;
insert attachment;
}
}

result.success = true;
return result;

}
}


And this is my Apex Class to give Test Coverage:

@isTest 
private class CreateContactTestClass{
    static testMethod void validateCreateContactFrmEmail() {
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

email.subject = 'Create Contact';
email.plainTextBody = 'FromEmail';
env.fromAddress = 'testemail@gmail.com';

CreateContactFrmEmail creatC = new CreateContactFrmEmail();
creatC.handleInboundEmail(email, env );
}
}