Learn All About Apex in Salesforce | The Developer Guide
What is Apex in Salesforce?
Apex is a strongly typed, object-oriented programming language that allows developers to run flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Apex Code can be executed by the anonymous window and from triggers on objects.
Lets us take an example:
Example 1:- Create 10 new Accounts Records in Salesforce.
Step 1:- Create a new Apex Class having the name CreateAccount and paste the below code.
public class CreateAccount { public static void createAccountMethod(){ List<Account> accounts = new List<Account>(); for(integer i=1;i<=10;i++){ Account a=new Account(); a.Name='New' + i; a.AccountNumber='1234567810'+i; a.Ownership='Subsidiary'; a.Type='Prospect'; a.Industry='Banking'; accounts.add(a); } insert accounts; } }
Step 2:- click ctrl+E and paste the below code.
CreateAccount.createAccountMethod();
Now, you can check your 10 newly created account records in the Account object.
Don't forget to check out: Concept of Virtual and Abstract Modifiers in Apex | Salesforce Developer Guide
Example 2:- Create 10 opportunities.
Step 1:- Create a new apex class having the name CreateOpportunity and paste the below code.
public class CreateOpportunity { public static void createOpprortunityMethod(){ List<Opportunity> opprnty = new List<Opportunity>(); for(integer i=1;i<=10;i++) { Opportunity opp = new Opportunity(); opp.Name='New Opp'+i; opp.closeDate=system.today(); opp.StageName='Needs Analysis'; opprnty.add(opp); } insert opprnty; } }
Step 2:- click ctrl+E and paste the below code.
CreateOpportunity.createOpprortunityMethod();
Now, you can check your 10 newly created records in the Opportunity object.
Example 3:- Create 10 Contacts with different Account Relationship values with no same 2 accounts in them.
Step 1:- Create a new Apex class having name CreateContact and paste the below code.
public class CreateContact { public static void createContactMethod(){ List<Account> accList= new List<Account>(); accList = [SELECT Id,Name FROM Account WHERE Name LIKE '%New%' LIMIT 11]; List<Contact> conList=new List<Contact>(); integer j =0; for(integer i=1;i<=10;i++){ j=j+1; Contact con=new Contact(); con.LastName= accList.get(j).Name; con.AccountId=accList.get(j).Id; conList.add(con); } Insert conList; } }
Step 2:- click ctrl+E and paste the below code.
CreateContact.createContactMethod();
Now, you can check your 10 newly created records in the Contact object.
Check out another amazing blog by Anshu Raj here: Process Builder in Salesforce - Everything You Need to Know
Example 4:- On Product to set up default price book entry in the "Standard Pricebook" as 1$.
Step 1:- Create a new Apex Trigger on sObject Product2 with the name DefaultPriceBook and paste the below code.
trigger DefaultPriceBook on Product2 (after insert) { if(trigger.isAfter && Trigger.isInsert) { ProductTriggerHandler.DefaultPriceBook(Trigger.new); } }
Step 2:- Create a new Apex class having the name ProductTriggerHandler and paste the below code.
public class ProductTriggerHandler { public static void DefaultPriceBook(List<Product2> Products) { list<PricebookEntry> pblist = new list<PricebookEntry>(); Pricebook2 pb=[Select id FROM Pricebook2 WHERE IsStandard=true]; for(Product2 prdt:Products){ PriceBookEntry pbe=new PriceBookEntry(); pbe.pricebook2id = pb.id; pbe.Unitprice=1; pbe.Product2Id=prdt.id; pblist.add(pbe); } insert pblist; } }
Now, you can check if the trigger is working fine by creating a new product.
Responses