Professional Documents
Culture Documents
Sub Programs
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.
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.
}
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.
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);
}
}
}
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();
insert hr;
if(hr.Id != null)
{
system.debug('Hiring Manager Record Id is...: '+ 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)
}
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));
}
Execution:
----------
MathOperationsHelper.Addition(2500, 5000);
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));
}
// 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);
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.