Apex is a strongly typed Object - oriented programming language and it will run on Force.com platform.
Here we give you the info about 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.
Apex Class Structure
Below is the sample structure for Apex class definition.
Syntax
private | public | global[virtual | abstract | with sharing | without sharing]
class ClassName[implements InterfaceNameList][extends ClassName] {
// Classs Body
}
Example Following is a sample structure for Apex class definition−
public class MySampleApexClass { //Class definition and body
public static Integer myValue = 0; //Class Member variable
public static String myString = ''; //Class Member variable
public static Integer getCalculatedValue() {
// Method definition and body
// do some calculation
myValue = myValue + 10;
return myValue;
}
}
More Sample for Apex class: -
Example 1: -Class for Account Creation
Public Class AccountCreation {
Public List < Account > CreateAccount(String s, String p) {
List < Account > a = new List < Account > ();
for (Account acc: a) {
acc.Name = s;
acc.phone = p;
insert all;
}
return a;
}
}
Go to Developer Console and execute the following code: AccountCreation ac = new AccountCreation();
Ac.CreateAccount(‘Osmania University’, 11112’);
Example 2 - for Static Keyword
Public Class HelloWorld {
Public static void Method1() {
System.debug('TechnoEric Solutions');
}
Public void method2() {
System.debug('Osmaina University');
}
}
Go to the Developer Console and execute the following HelloWorld hw = new HelloWorld();
Hw.Method1(); // It gives Error. For static methods we cannot have instant for the class. HelloWorld.Method1();
Hw.Method2();
Example 3: -Retrieving Account Records by passing Single field value(Phone number):
Public class FetchAccountNameFromPhone {
Public Set < String > FetchAccName(String p) {
Set < String > s1 = new Set < String > ();
List < Account > acc = “Select id, Name From Account where phone”;
for (Account a: acc) {
String s = a.name;
s1.add(s);
}
System.debug('xxxxxxxxx' + s1);
return s1;
}
}
Example 4: -Apex class for creating account, contact, opportunity ata a time
Public class Acc_Con_Opty_Creation {
Public Account CreateAccount(String n) {
Account a = new Account();
a.Name = ’n’;
insert a;
Contact c = new Contact();
c.LastName = 'Vikas Singh';
c.AccountId = a.id;
insert c;
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
Date d = Date.newinstance(‘2012, 2, 17’);
opp.CloseDate = d;
opp.StageName = 'Closed Lost';
opp.Accountid = a.id;
insert opp;
return a;
}
}
Go to the Developer Console and execute the following: Acc_Con_Opty_Creation acoc = new Acc_Con_Opty_Creation();
Acc.CreateAccount(‘capital info Solutions’);
No comments:
Post a Comment