You are on page 1of 14

=====================================================================================

===========

Salesforce Training By Mohan - VLR Training Institute ||| Contact: +91-6301332114

=====================================================================================
===========

Topic : Apex Methods

=============================================================================

* Apex methods are used

- to write the business logic,

- to query the records

- to do the dml operations

- etc....

* We can create different type of apex methods

1. void method

2. return type method (Primitive,Non-Primitive)

3. pagereference method

* Whatever we have done in constroctor, same we can do here also

* We can create with && without arguments methods

=============================================================================

* Generic sytax:

[accessmodifiers] [void|returntype|pagereference] methodName(){

}
* Generic Syntax Example:

===================================================

public void methodName(){

public primitivedatatype methodName(){

return primitivevalue/null;

public non-primitive methodName(){

return non-primitivevalue/null;

public pagereference methodName(){

return VFPage;

Req-1) Create an Apex class. Create a void method and call this method from anonymous window.

=====================================================================================
==

public class VaribleDifference {


public string strTopVar;

public account ac = new account();

public account a1 ;

public VaribleDifference(){}

public void methoTwo(){

strTopVar = 'Outer String';

ac.name = 'Outer Account Ram';

a1 = new account();

a1.name = 'Ram Accont';

string strInner = 'ram Inner';

account a = new account();

a.name = 'Inner RAm';

________________________________________________

VaribleDifference v = new VaribleDifference();

string s = v.strTopVar;

account a = v.ac;

account a2 = v.a1;

string sinner = v.strInner; // You can not access inner variables

account a3 = v.a;

system.debug('v==>' + v);
v.methoTwo(); //calling method

=====================================================================================
===

* Step by Step Debugging:

- Below are the general Debugging Events we need to remember

1. STATEMENT_EXECUTE

2. HEAP_ALLOCATE

3. VARIABLE_ASSIGNMENT

4. Method Exit

5. VARIABLE_SCOPE_BEGIN

- High level Flow of debugging in Apex Class with out VF Page

1. Class level variables execute [statement execute + heat allocate + variable


assignment]

2. Constructor will execute line by line

3. If any methods are calling then those methods will execute

=====================================================================================
===

Req-1) Create an apex class(VaribleDifference) with void method. Create one more apex
class(SecondApexClass) with void method. Call 'VaribleDifference' class method from 'SecondApexClass'
class

=====================================================================================
==

public class SecondApexClass {


public string str;

public SecondApexClass(){

system.debug('constroctor Entry==>');

myMethod();

system.debug('constroctor after method==>');

public void myMethod(){

system.debug('myMethod Entry==>');

VaribleDifference v = new VaribleDifference();

//str = v.strInner; // you can't get local variables from another clas

/* comments can be written in between lines */

system.debug('str==>' + str);

_______________________________________________________

public class VaribleDifference {

public string strTopVar = 'variableVa';

public account ac = new account();

public account a1 ;

public VaribleDifference(){}

public void methoTwo(){

system.debug('methoTwo Entry==>');

strTopVar = 'Outer String';


ac.name = 'Outer Account Ram';

a1 = new account();

a1.name = 'Ram Accont';

string strInner = 'ram Inner';

account a = new account();

a.name = 'Inner RAm';

_______________________________________________________

SecondApexClass sc = new SecondApexClass();

Req-2) Create an apex class(VaribleDifference2) with void method. Create one more apex
class(SecondApexClass2) with void method. Call 'VaribleDifference2' class method from the constroctor
of the same class. call 'VaribleDifference2' class from 'SecondApexClass2' class.

===============================================================================

public class SecondApexClass2 {

public string str;

public SecondApexClass2(){}

public SecondApexClass2(string st){

myMethod(st);

public void myMethod(string s){


VaribleDifference2 v = new VaribleDifference2(s);

str = v.strTopVar;

____________________________________________________________

public class VaribleDifference2 {

public string strTopVar ;

public account ac = new account();

public account a1 ;

public VaribleDifference2(string s){

methoTwo(s);

public void methoTwo(string st){

strTopVar = st;

____________________________________________________________

SecondApexClass2 sc2 = new SecondApexClass2();

Req-3) Create 2 apex classes. call one class from another class.

=========================================================================

public class SecondApexClass {

public string str;

public account ac = new account();


public SecondApexClass(){}

public SecondApexClass(string st,account a){

myMethod(st,a);

public void myMethod(string s,account acc){

VaribleDifference v = new VaribleDifference(s,acc);

str = v.strTopVar;

ac = v.ac;

_______________________________________________________

public class VaribleDifference {

public string strTopVar ;

public account ac = new account();

public account a1 ;

public VaribleDifference(string s,account a){

methoTwo(s,a);

public void methoTwo(string st,account a2){

strTopVar = st;

ac = a2;

_______________________________________________________

SecondApexClass sc = new SecondApexClass();


Req-4) Create 3 apex classes. call one class from another class.

=========================================================================

public class SecondApexClass {

public string str;

public account ac = new account();

public account ac2 ;

public SecondApexClass(){

ApexUtility uti = new ApexUtility();

ac2 = uti.creatAccountMethod('Rama','Hyderabad','Energy');

system.debug('ac2==>' + ac2);

public SecondApexClass(string st,account a){

myMethod(st,a);

public void myMethod(string s,account acc){

VaribleDifference v = new VaribleDifference(s,acc);

str = v.strTopVar;

ac = v.ac;

_______________________________________________________

public class VaribleDifference {


public string strTopVar ;

public account ac = new account();

public account a1 ;

public VaribleDifference(string s,account a){

methoTwo(s,a);

public void methoTwo(string st,account a2){

strTopVar = st;

ac = a2;

_______________________________________________________

public class ApexUtility {

/***

* reuse purpose

* to avoid more lines of code

***/

// Reuse of account creation

public account creatAccountMethod(string accName,string sit,string ind){

account ac = new account();

ac.name = accName;

ac.site = sit;

ac.industry = ind;

return ac;

}
}

_______________________________________________________

SecondApexClass s = new SecondApexClass();

account a = new account();

a.name = 'Test Account';

SecondApexClass s = new SecondApexClass('Passing Values',a);

system.debug('s==>' + s.str);

system.debug('s account==>' + s.ac);

system.debug('s account fields==>' + s.ac.name);

=========================================================================

Req-5) Create an apex class with different method types and constroctor

=========================================================================

public class Dec13_Methods {

//Class level variables

public string str ;

public account acc;

//Constructor-with out argument

public Dec13_Methods(){

str = 'VLR';

system.debug('constroctor==>' + str);

acc = new account(name='stansys');

//Constructor-with single argument

public Dec13_Methods(string st){


str = 'VLR';

str = st;

system.debug('constroctor==>' + str);

acc = new account(name='stansys');

//void method with out arguments

public void mysampelmethd(){

str = 'Mohan';

system.debug('str im method==>' + str);

acc.name = 'Mohan vlr stansys';

// void method with 2 arguments

public void mymethod(string st,account a){

str = st; //'venkat

acc = a;

//return type method wiht out argument && with - primitive(string) datatype

public string mystrmethod(){

str = 'mohan';

return str;

//Return method without argument & with non-primitive(account) datatype

public account myaccond(){


acc = new account();

acc.name = 'vlr stansys';

return acc;

_______________________________________________________

Dec13_Methods de = new Dec13_Methods('ameerpet');

system.debug('str before method called==>' + de.str);

system.debug('acc before method called==>' + de.acc);

de.mySecondmethod();

system.debug('str after method 1 called==>' + de.str);

system.debug('acc after method 1 called==>' + de.acc);

de.mysampelmethd();

system.debug('str after method called==>' + de.str);

system.debug('acc after method called==>' + de.acc);

string st = de.mystrmethod();

system.debug('st ==>' + st);

account a = de.myaccond();

system.debug('a ==>' + a);

de.mymethod('venkat',new account());
system.debug('acc =>' + de.acc);

system.debug(de.str);//'venkat

account accc = de.retuAcocunt('Mounika',new account(name='manasa'));

system.debug(accc);

=====================================================================================
===========

Salesforce Training By Mohan - VLR Training Institute ||| Contact: +91-6301332114

=====================================================================================
===========

You might also like