You are on page 1of 7

Sub Programs:

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

2 Types.

1. Procedures:
These are like Fire & Forgot Options, which contains a set of
statements to be used to perform the operations inside the application. Which
doesn't return any value to the calling environment.

2. Functions: Request & Reply.


Functions contains the business logic to be used to perform the
operations and it will return the value to the calling environment.

Note:
All the Sub Programs should be defined inside the "Class". A Class can have N
Number of Procedures and Functions.

Procedures:
===========
2 Types of Procedures

1. Non-Parameterized Procedures:

2. Parameterized Procedures:

1. Non-Parameterized Procedures:
These procedures doesn't expect any input values from the user, and it will
perform the operations based on the business logic written inside the procedure.

Syntax:
[Access Specifier] void <ProcedureName>()
{
// Write the Business Logic..
}

Ex:
Public Class AccountsHelper
{
Public void CreateAccountRecord()
{
// Write the Code to insert the Account.
}

Public void GetAllAccounts()


{
// Write the Business Logic to Get Account Records.
}
}

Invoke the Procedure:


---------------------
We can invoke the procedures by using the help of the Object of the
Class.

Syntax:
<objectName>.<ProcedureName>();

Ex:
AccountsHelper accHelper = new AccountsHelper();

accHelper.CreateAccountRecord();

accHelper.GetAllAccounts();

UseCase:
========
Write an apex program, to insert a Contact Record inside the object.

Class Code:(Developer Console)


-----------
public class ContactsHelper
{
Public void CreateNewContact()
{
Contact con = new Contact();

con.FirstName = 'Apex';
con.LastName = 'Contact Record';
con.Title = 'Project Manager';
con.Email = 'sample@gmail.com';
con.Phone = '9900998877';
con.Fax = '9900667788';
con.LeadSource = 'Web';
con.MailingCity = 'Hyderabad';
con.MailingState = 'Telangana';
con.MailingCountry = 'India';

insert con;

if(con.Id != null)
{
system.debug('Contact Record Created With Id ....: '+ con.id);
}
}
}

Execution: (Execute Anonymous Windows)


----------
ContactsHelper conHelper = new ContactsHelper();

conHelper.CreateNewContact();

UseCase:
========
Write an apex class, to insert a Hiring Manager Record and an associated
Position Record.

Class Code:
-----------
public class HiringManagerHelper
{
Public static void CreateHRRecord()
{
// Create the HR Record..
Hiring_Manager__C hr = new Hiring_Manager__C();

hr.Name = 'Ramesh Kumar';


hr.Location__c = 'Mumbai';
hr.Contact_Number__c = '9900998877';
hr.Email_ID__c = 'ramesh@gmail.com';

insert hr;

if(hr.Id != null)
{
system.debug('Hiring Manager Record Id is...: '+ hr.id);

// Create the Related Position Record..


Position__C pos = new Position__C();

pos.Name = 'Java Project Lead';


pos.Location__c = hr.Location__c;
pos.Position_Status__c = 'New Position';
pos.Number_of_Positions__c = 2;
pos.Open_Date__c = system.today();
pos.Milestone_Date__c = system.today().AddDays(45);
pos.Minimum_Budget__c = 1400000;
pos.Maximum_Budget__c = 1700000;
pos.Travel_Required__c = true;
pos.Passport_Required__c = true;
pos.HR_Contact_Number__c = hr.Contact_Number__c;
pos.HR_Email_ID__c = hr.Email_ID__c;
pos.Position_Description__c = 'Java Project Lead';
pos.Skills_Required__c = 'Java Project Lead';

// Make the Position to be associated to HR Record..


pos.HiringManager__c = hr.id;

insert pos;
if(pos.Id != null)
{
system.debug('Position Record ID is...: '+ pos.id);
}
}
}
}

Execution:
----------
HiringManagerHelper.CreateHRRecord();

Parameterized Procedures:
=========================
By using this feature, we can supply the input values to the procedures at runtime
based on the application requirement. So that we can make the functionality
dynamic.

Syntax:
[Access Specifier] void <ProcedureName>(<DataType> <param1>, <DataType>
<param2>,....,
<DataType> <paramN>)
{
// Write the Business Logic..
}

Note: We can supply "N" number of parameters to the procedure. Each Parameter
should be specified with the "DataType and Parameter Name".

Ex:
Public Class CommonHelper
{
Public void Addition(integer x, integer y)
{
// Write the Business Logic (Ex: x + y)
}

Public void CompareStrings(string s1, string s2)


{
// Write the Business Logic..
}

Public void GetStringLength(string str)


{
// Write the Code..
}

Public void GetAccountByID(Id accountRecordID)


{
// Write the Code..
}

Public void DeleteAccounts(ID[] accountIDs)


{
// Write the Business Logic..
}
}

Note:
Upon invoking the Procedures, we have to supply the required input values at
runtime.

Syntax:
<objectName>.<ProcedureName>(<Value1>, <Value2>,....., <ValueN>);

Ex:
CommonHelper cHelper = new CommonHelper();

cHelper.Addition(100, 2500);

cHelper.CompareStrings('Welcome','Hello');

cHelper.GetAccountByID('001453453453453');

UseCase:
========
Write an apex Class, to perform the Mathematical Operations.

Class Code:
-----------
public class MathOperationsHelper
{
Public static void Addition(integer firstNumber, integer secondNumber)
{
system.debug('Addition Result is...: '+ (firstNumber +
secondNumber));
}

Public static void Multiply(integer x, integer y, integer z)


{
system.debug('Multiply Result is...: '+ (x * y * z));
}

Public static void Division(integer x, integer y)


{
if( y > 0)
{
system.debug('Division Result is...: '+ (x / y));
}
else
system.debug('Division Operation Cannot be Performed.');
}

Public static void Reminder(integer x, integer y)


{
if(y > 0)
{
system.debug('Reminder Value is....: '+ Math.mod(x, y));
}
else
system.debug('Modulus Operation Cannot be Performed.');
}
}

Execution:
----------
MathOperationsHelper.Addition(2500, 5000);

MathOperationsHelper.Multiply(23, 56, 100);

MathOperationsHelper.Division(25000, 10);

MathOperationsHelper.Reminder(5600, 20);

UseCase:
========
Write an apex program, to Perform the String Operations on the String input
values.

Class Code:
-----------
public class StringOperationsHelper
{
Public static void Concatenate(string s1, string s2)
{
system.debug('Concatenation Result is...: '+ (s1 + s2));
}

Public static void GetStringLength(string str)


{
system.debug('Number of Characters is...: '+ str.length());
}

// Text Comparison
Public static void CheckEquals(string s1 , string s2)
{
if(s1 == s2)
system.debug('Both the Strings are Identical.');
else
system.debug('Both the Strings are Different.');
}

// Binary Comparison
Public static void CheckEqualsWithCase(string s1, string s2)
{
if(s1.equals(s2))
system.debug('Both the Strings are Equals.');
else
system.debug('Both the Strings are Different.');
}
}

Execution:
----------
StringOperationsHelper.CheckEquals('Hello','hello');
StringOperationsHelper.CheckEqualsWithCase('Hello','hello');
StringOperationsHelper.CheckEqualsWithCase('Hello','Hello');
StringOperationsHelper.GetStringLength('Welcome to Apex Programming.');

UseCase:
========
Write an apex program, to Create the Hiring Manager Record by passing the
values at runtime.

Class Code:
-----------
public class HiringManagerUtility
{
Public static void CreateHRRecord(string hrName, string hrCity, string
hrPhone, string hrEmail)
{
Hiring_Manager__C hr = new Hiring_Manager__C();

hr.Name = hrName;
hr.Location__c = hrCity;
hr.Contact_Number__c = hrPhone;
hr.Email_ID__c = hrEmail;

insert hr;
if(hr.Id != null)
{
system.debug('Hiring Manager Record Id is...: '+ hr.id);

// Call the Procedure to Create a Related Position Record..


CreateRelatedPosition(hr);
}
}

Public static void CreateRelatedPosition(Hiring_Manager__C hrRecord)


{
Position__C pos = new Position__C();

pos.Name = 'QTP Lead';


pos.Location__c = hrRecord.Location__c;
pos.HR_Email_ID__c = hrRecord.Email_ID__c;
pos.HR_Contact_Number__c = hrRecord.Contact_Number__c;
pos.Position_Status__c = 'New Position';
pos.Number_of_Positions__c = 1;
pos.Open_Date__c = System.today();
pos.Milestone_Date__c = System.today().AddDays(30);
pos.Minimum_Budget__c = 1100000;
pos.Maximum_Budget__c = 1400000;
pos.Position_Description__c = 'QTP Autumation Test Lead';
pos.Skills_Required__c = 'QTP Automation, Manual Testing';

pos.HiringManager__c = hrRecord.Id;

insert pos;

if(pos.id != null)
system.debug('Position Record id is...: '+ pos.id);
}
}

Execution:
----------
HiringManagerUtility.CreateHRRecord('Suman
Kumar','Hyderabad','9900887766','suman@gmail.com');

Assignments:
============
1. Write an apex program, to insert the Account Record by passing the values
at runtime.

2. Write an apex program, to insert the Lead Record by passing the values at
runtime.

3. Write an apex program, to insert the Position Record at runtime, by


passing the values.

You might also like