Salesforce Self-Learning (i)

Source: Internet
Author: User
Tags server hosting

Salesforce learning-reaching apex; Learning Goals:

1. Describe the key features of Apex program language;

2. Save an Apex class and use another apex class to invoke its method;

3, use developer console check debug log;

Start learning Apex:

Apex is a programming language that uses class Java syntax and uses a database-like stored procedure, Apex allows developers to add business logic to system events, such as clicking buttons, updating related records, and Visualforce pages;

As a language, Apex:
1, server hosting-save, compile and run on the force.com platform;
2, automatic upgrade-because the compiled code is stored as meta-data on the Salesforce platform;
3, Object-oriented--apex support class, interface and inheritance;
4, strong type--at compile time will be strong type of validation;
5, Apex running in an organization multi-user sharing platform, it forces monitoring code to prevent exclusive sharing of resources;
6, with the database-storage and operation record is very simple, Apex provides records and field direct access, also provides reports and query language to manipulate these records;
7, the data centralized--APEX provides the database The thing operation, allows the rollback operation;
8, the use of simple--apex-based Java terminology;
9, Test simple--apex provides built-in unit testing, creation, execution, and code coverage, and Salesforce ensures that all custom apex code runs as expected before it is pushed to the platform;
10, version control--apex can save different versions of the API;


Highlights of the Apex language:

Like other object-oriented programming languages, Apex supports the following language constructs:
1, support classes, interfaces, configuration files and collections (including arrays);
2. Objects and array symbols;
3, expressions, variables and constants;
4. Conditional statement (IF-THEN-ELSE) and control loop statement (for loop and while loop);
Unlike other object-oriented languages, Apex supports:
1, cloud development, that is, Apex is stored, compiled and executed on the cloud;
2, trigger, similar to the trigger in the database;
3, the database statement allows direct invocation, support query data;
4, transaction and rollback;
5, global access modifier, more relaxed than the public modifier, allowing access between namespaces and applications;
6. Version control of custom code;

In addition, Apex is not case-sensitive;

Development tools:

Can be developed directly in the browser with Salesforce's development console, or it can be developed with the plugin provided by Eclipse with force.com;

Data Type Overview:

Apex supports a wide variety of data types, including one for Salesforce special data type--sobject data type;
Apex supports the following data types:
1, the most basic, such as Integer, Double, Long, Date, Datetime, String, ID, Boolean, and so on;
2, SObject, there are general SObject and special sObject, such as an account, a connection, or a custom sObject;
3, a collection, including:
① a list collection (or array) of sObject that holds the underlying type data, sObject, user-defined sObject, Apex creation;
② a set of basic type data;
③ a base type-the base type, the base type-sobject map collection;
4, a fixed type is worth the list collection, also known as enumeration;
5, user-defined Apex class;
6, the system provides apex class;


Apex Collection: List;
The list holds an ordered object, and the list and arrays in apex are synonymous, and the two can be exchanged for use;
Generally, it is easier to create a list than an array because the list does not require an advance decision on how many elements to allocate;
You can call the Add () method to add elements to the list when the list is created or after it has been created;
The elements in the list can be read by specifying subscripts in a pair of parentheses, just like elements of an array, and you can also get a list element by using the Get () method;
Apex also supports two other types of collections: set and map;

Apex Class:

One of the conveniences of apex classes is the reusability of code, which can be invoked by triggers or by other classes;


Practice:

Create and save a Emailmanager apex class; Salesforce will automatically compile your class when you save it;

public class Emailmanager {

Public method
public void SendMail (string address, string subject, string body) {
Create an email Message object
Messaging.singleemailmessage mail = new Messaging.singleemailmessage ();
string[] toaddresses = new string[] {address};
Mail.settoaddresses (toaddresses);
Mail.setsubject (subject);
Mail.setplaintextbody (body);
Pass this e-mail message to the built-in SendEmail method
of the Messaging class
Messaging.sendemailresult[] results = Messaging.sendemail (
New messaging.singleemailmessage[] {mail});

Call a helper method to inspect the returned results
Inspectresults (results);
}

Helper method
private static Boolean Inspectresults (messaging.sendemailresult[] results) {
Boolean Sendresult = true;

SendEmail returns an array of result objects.
Iterate through the list to inspect results.
In this class, the methods send only one email,
So we should has only one result.
for (Messaging.sendemailresult res:results) {
if (res.issuccess ()) {
System.debug (' Email sent successfully ');
}
else {
Sendresult = false;
System.debug (' The following errors occurred: ' + res.geterrors ());
}
}

return sendresult;
}

}

Next, use Debug to invoke the method in the above class to test;
In the upper left corner of the developer console, click Open Execute Anonymous window under Debug, and in the new window that opens, enter the code:
Emailmanager em = new Emailmanager ();
Em.sendmail (' Your email address ', ' Trailhead Tutorial ', ' 123 body ');

Of course, the location of the your email address is written as a valid mailbox;

Click Execute execution in the lower right corner of the window, and now the methods in the above class are executed, and the mailbox you filled in debug should receive an email with the content "123 body" with the theme "Trailhead Tutorial"; Check your email.

To view the debug log:


Debug log is very useful for debugging your code, when the apex method is called, the process will be recorded in the debug process, of course, you can also write the debug information in the past, to help you debug your code to see where the specific problem;

At the bottom of the Developer console page, click Logs, select the recent log double click to view the details, click the Debug only filter log to display only the lines of System.debug ();
Of course, you can also search for the desired log information by entering keywords yourself;
In the above DEBUG test, we can see that the log information is 16:22:01:433 User_debug [23]| Debug| Email send successfully;

Invocation of a static method:
Because in the above class, the SendMail () method does not use a member variable in the class, so it does not need to be an instance method; Now we change the method to a static method, that is, add the static keyword to the declaration of the method;
The invocation of a static method is easier than an instance method, since it is not necessary to instantiate the class, but it can be called directly from the class name;

After you change the SendMail () method to a static method, you do not need to invoke the method by instantiating the class first in the Execute anonymous window, directly calling:

Emailmanager.sendmail (' Your email address ', ' Trailhead Tutorial ', ' 123 body ');

Click Execute to execute, the effect will be the same;

Attached: Because the new company needs me to study Salesforce, I began to grope, just contact, time tight, with the official English document side translation, there may be a lot of translation and technical problems, but also welcome peer correction and exchange of learning, thank you!

Salesforce Self-Learning (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.