Apex Programming Basics, Lecture notes of Sales Management

Apex Programming Basics Datatypes and Variable Declaration

Typology: Lecture notes

2017/2018

Uploaded on 10/12/2021

raghu-teja
raghu-teja 🇮🇳

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Apex is strongly typed object-oriented, on-demand programming language. It is
compiled, stored, and run entirely on the Force.com platform (multi-tenant
environment and is very controlled in its invocations and limits).
Apex syntax looks mostly like Java and acts like stored procedures.
Apex allows developers to attach business logic to the record save process
Apex has built-in support for unit test creation and execution.
As a language apex is Integrated, Easy to use, Data focused, Rigorous, Hosted,
Multi-tenant aware, automatically up-gradable, easy to test and versioned.
Below image helps you to understand how apex works:
Below are few capabilities of Apex
Apex provided build in support for:
DML calls to insert, update, and delete a record.
Inline SOQL or SOSL statements for retrieving records.
Looping control structures that help with bulk processing.
A record locking syntax that prevents record update conflicts.
Custom public API calls.
Send and receive emails.
Web services or XML request/response integrations.
Warnings and errors to prevent objects referenced by Apex from being
modified.
Difference between traditional code & Apexprogramming
A traditional code is fully flexible and can tell the system to do anything.
Apex is governed, can only do what the system allows.
What is the apex class and Triggers?
Apex Classes:It is a collection of variables and a6library of methods that can be
reused.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Apex Programming Basics and more Lecture notes Sales Management in PDF only on Docsity!

Apex is strongly typed object-oriented, on-demand programming language. It is compiled, stored, and run entirely on the Force.com platform (multi-tenant environment and is very controlled in its invocations and limits).  Apex syntax looks mostly like Java and acts like stored procedures.  Apex allows developers to attach business logic to the record save process  Apex has built-in support for unit test creation and execution. As a language apex is Integrated, Easy to use, Data focused, Rigorous, Hosted, Multi-tenant aware, automatically up-gradable, easy to test and versioned. Below image helps you to understand how apex works: Below are few capabilities of Apex  Apex provided build in support for:  DML calls to insert, update, and delete a record.  Inline SOQL or SOSL statements for retrieving records.  Looping control structures that help with bulk processing.  A record locking syntax that prevents record update conflicts.  Custom public API calls.  Send and receive emails.  Web services or XML request/response integrations.  Warnings and errors to prevent objects referenced by Apex from being modified. Difference between traditional code & Apex programming  A traditional code is fully flexible and can tell the system to do anything.  Apex is governed, can only do what the system allows. What is the apex class and Triggers? Apex Classes: It is a collection of variables and a library of methods that can be reused.

Apex Trigger: It is a script that executes before or after a specific data manipulation language (DML) event on a particular Salesforce object. When can we use Apex programming? Apex should be used as a solution when:  You need to apply complex business logic to rows of data being saved by any means.  You need to create additional web services API functionality for exposing logic either within Salesforce or to external applications.  You need to call out to external Web service and process the results.  You need to handle incoming or outgoing emails in ways more complex than the declarative functionality. Apex triggers execute no matter how the triggering data is being saved. Apex executes regardless of whether the action originates in the user interface, through AJAX toolkit, or from web services API. If you only want the code to execute through UI, consider making a Visualforce page and controller. A class is the group of variables and methods. We can create a class in Salesforce in different ways. See the below post to know about apex class. Below is the example to create a simple class Public class MyFirstApexClass { // body of the Apex class. Here we can define variables and methods } This is the simple class definition. Generally in salesforce to define a class you must use “class” keyword followed by access specifier. here access specifier and ‘class’ and class names are mandatory for every class in salesforce. Now we will learn how to create a class in Salesforce? To create a class in salesforce go to Setup -> Build -> Develop -> Apex Class and click on NEW button and create class there.

This is a small example to create class and test class for that. We will more about Apex classes and Test classes later. Alternation of Apex Class Creation We can also create new Apex classes directly in the Developer Console.  Open the Developer Console.  Click the repository tab.  The setup Entity type panel lists the different items. We can view and edit in the Developer Console.  Click on classes, and then click “New” button.  Enter “Message” for the name of the new class and click “ok” bottom.  Add the following static method to the new class public static string hellowMessage () { return ( 'Welcome to salesforetutorial.com'); } Click on “Save” button. Public class AccountCreation { Public Account createAccount(String name){ Account a= new Account(); a.Name = name; insert a; return a; } } Go to the Developer console, and execute the following code Account Creation ac = new Account Creation(); creating an instance for the above class. ac.CreateAccount('Osmania University'); calling a method system.debug(ac); check the account is created or not in the USER-DEBUG Log.

Data types & Variables – Coding basics Data types are used to define variables and methods in classes. In Apex, all variables & expressions have one of the below data types. Those are  Primitive data types (Integer, Boolean, String, …… etc.)  Enum (an enumerated list)  sObjects (sObject, or Account, contact, object__c….)  Collection (list, set, map)  Null (for the null constant. Which can be assigned to any variable.  An object created by the user – or system-defined classes.

  1. Primitive data types: know more about primitive data types
  2. Enumerated list or Enum data type: Enumerated list (or Enum) is an abstract data type that stores one value of a finite set of specified identifiers. To define Enum, use the enum keyword in the variable declaration and then define the list of values. You can use enums to specify a set of constants. Know more about Enum
  3. sObjects: Know more about sObject types
  4. Collection data types: Three types of collection are available in Apex List: An ordered collection of primitives, sObjects, collections, or Apex objects based on indices. Set: An unordered collection of unique primitives. Map: A collection of unique, primitive keys that map to single values which can be primitives, sObjects, collections, or Apex objects. Notes:  Collections can have not more than 1000 elements.  Lists and maps may contain other collections – Collections can only be nested up to five levels deep.  Collections do not support adding or removing elements while iterating over the collections. Know more about collections Variables – coding basics Static keyword is used to define static variables to store data that is shared within the class.  Static variables are not memory constants.  All instances of the same class share a single copy of the static variable.  This can be a technique used for setting flags to prevent recursive triggers.

7. ID

Represents 18-character Force.com record identifier. Example: ID id='00300000003T2PGAA0';

  1. Integer 32-bit number that doesn’t include a decimal point. Minimum value -2,147,483,648 — maximum value of 2,147,483, Example: Integer i = 1;
  2. Long Represents 64-bit number that doesn’t include a decimal point. minimum value of -263 — maximum value of 263-1. Example: Long l = 2147483648L;
  3. String Represents Set of characters surrounded by single quotes. Example: String s1 = 'Hello';
  4. Time Represents particular time. Example: Time myTimeVar = Time.newInstance(18, 30, 2, 20); Integer myMinutes = myTimeVar. minute(); List Type Keywords Below are the Collection Type keyword used in Apex: 1. List Ordered collection of typed primitives, sObjects, objects, or collections that are distinguished by their indices. Example: **// Create an empty list of String List my_list = new List(); My_list.add('first'); String x = my_list.get(0); // Create list of records from a query List accs = [SELECT Id, Name FROM Account LIMIT 1000];
  5. Map** Collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type, or an object. Example: Map<String, String> mys = new Map<String,String>();

Map<String, String> mys = new Map<String, String>{'a' => 'b', 'c' => 'd'.toUpperCase()}; Account myAcct = new Account(); Map<Integer, Account> m = new Map<Integer, Account>(); m.put(1, myAcct);

3. Set Unordered collection that doesn’t contain any duplicate elements. Example: Set s = new Set(); s.add(1); s.add(1); System.assert(s.size()==1);