3

I'm learning by myself through the beginner developer Trailhead - I've come across the code to send an email and get results in the first module of Apex Basics & Database, and there are a few things I can't grasp in the syntax even after consulting the documentation.enter image description here

For example:

Does the following bit mean that the variable 'results' stores an array (pointed by the .SendEmailResult[] ) of what's to the right of the = sign?

Messaging.SendEmailResult[] results = Messaging.sendEmail( new Messaging.SingleEmailMessage[] { mail });

Why does this line needs to be written if there's a whole inspectResults() method written and after that called in the main public method?

EDIT: Also, what's the utility of the boolean typesendResult?

Grateful for any help.

1 Answer 1

4

There's no reason why you do need the variable; it could have been written as:

inspectResults(
    Messaging.sendEmail(
        new Messaging.SingleEmailMessage[] { mail }));

But this syntax tends to confuse newer developers.

A function has basic three parts: the return value, the name, and the parameters.

In this case, Messaging.sendEmail looks like this:

Messaging.SendEmailResult[] sendEmail(Messaging.EmailMessage[] messages)

Where the first part is the "return value" (the results/output of calling the method), the second part is the name ("sendEmail"), and the third part is the parameters (the input values).

The return value from inspectResults is not used here, but it could be used to, for example, return an error message to the user:

if(inspectResults(
    Messaging.sendEmail(
        new Messaging.SingleEmailMessage[] { mail }))) {
  ApexPages.addMessage(
    new ApexPages.Message(
      ApexPages.Severity.INFO,
      'The emails were sent successfully.'));
} else {
  ApexPages.addMessage(
    new ApexPages.Message(
      ApexPages.Severity.ERROR,
      'The emails failed to send successfully.'));
}

You must log in to answer this question.

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