Showing posts with label Training. Show all posts
Showing posts with label Training. Show all posts

Tuesday, 27 November 2018

SFDC Fundamentals

1 Apex Basics & Data Base :
   1.1 Get Started with Apex
    Agenda:
  •  Describe the key features of the Apex programming language(Cloud compile,Object   Oriented, Strongly Typed,Direct data Base Connection, version. )  
  •  Save an Apex class and call methods with Anonymous Apex(see bellow class)
  •  Use the Developer Console to inspect debug logs

     Challenge: Create an Apex class with a method that returns an array (or list) of strings.
Create an Apex class with a method that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...). The length of the array is determined by an integer parameter.
The Apex class must be called String Array Test and be in the public scope
The Apex class must have a public static method called generate StringArray
The generateStringArray method must return an array (or list) of strings
The method must accept an incoming Integer as a parameter, which will be used to determine the number of returned strings
The method must return a string value in the format Test n where n is the index of the current string in the array
Solution: 
/*  Author : Subbareddy Peddireddy
* ClassName : StringArrayTest
* */
public class StringArrayTest {
/* Method with the name generateStringArray and integer as parameter
*  and with string list return type*/
public static List<String> generateStringArray(Integer inputNumber)
{
List<String> arrayList = new List<String>();
for(Integer i=0;i<inputNumber;i++)
{
   arrayList.add('Test '+i);
}
                                   System.debug('@@@arrayList'+arrayList);
return arrayList;
}
}

 1.2 Use sObjects
        Agenda:
  • Describe the relationship between sObjects and Salesforce records

sfdc object fields directly integrated with Apex. every standard or custom object is of                          abstract type sObject.
  • Create and use specific sObject variables

    Account acct = new Account(Name='Acme');

  • Cast a generic sObject to a specific sObject

sObject sobj1 = new Account(Name='Trailhead'); 

Quiz: 
1.Describe the relationship between sObjects and Salesforce records.
a.The name of an sObject field in Apex is the label of the field in Salesforce.
b.A custom object's API name and label are the same.
c.Every record in Salesforce is natively represented as an sObject in Apex.
Ans: c
2.You can obtain an instance of an sObject, such as Account, in one of the following                              ways:
a.By creating the sObject only.
b.Either by creating the sObject or by retrieving a persistent record from                                                   Salesforce using SOQL.
c.By retrieving the sObject only.
Ans: b
3.Which of the following is correct about a generic sObject variable?
a.A generic sObject variable can be assigned only to another generic sObject.
b.Generic sObjects can't be created.
c.A generic sObject variable can be assigned to any specific sObject, standard or                                      custom. Such as Account or Book__c.
Ans: c
 1.3 Manipulate Records with DML

Agenda:
  • Use DML to insert, update, and delete records.(insert,update,upsert{upsert sObject |                               sObject[]​​ field},delete,undelete,merge)
  • Perform DML statements in bulk

List<Account> accountList=new List<Account>();
//add data
Update accountList
Use upsert to either insert or update a record.
Contact jane2 = new Contact(FirstName='Jane',
                         LastName='Smith',
                         Email='jane.smith@example.com',
                         Description='Prefers to be contacted by email.');
// Upsert the contact by using the idLookup field for matching.
upsert jane2 Contact.fields.Email;
Catch a DML Exception.
try{
Insert Acc;
}
catch(DMLException Ex){
throw ex;
}
  • Use a Database method to insert new records with the partial success option and process the results

database.SaveResult[] results=database.insert(accList,false);
Database.Error err : results.getErrors()
System.debug(err.getStatusCode() + ': ' + err.getMessage());
Know when to use DML statements and when to use Database methods.
Perform DML operations on related records.

Challenge: 
Create a method for inserting accounts.
To pass this challenge, create an Apex class that inserts a new account named after an                           incoming parameter. If the account is successfully inserted, the method should return the                     account record. If a DML exception occurs, the method should return null.
The Apex class must be called AccountHandler and be in the public scope
The Apex class must have a public static method called insertNewAccount
The method must accept an incoming string as a parameter, which will be used to create the Account name
The method must insert the account into the system and then return the record
The method must also accept an empty string, catch the failed DML and then return null

Solution: 
/*  Author : Subbareddy Peddireddy
* ClassName : AccountHandler
* */
public class AccountHandler {
/* Method with the name insertNewAccount and String as parameter
*  and with Account as return type*/
public static Account insertNewAccount(String accountName){
system.debug('###accountName'+accountName);
   Account acc=null;
if(!String.isEmpty(accountName)){
acc=new Account();
  acc.Name=accountName;
try{
   insert acc;
}
catch(DMLException e){
   // acc=null;
  throw e;
}
}

   return  acc;
}

}


 1.4 Write SOQL Queries 
Agenda:
  • Write SOQL queries in Apex.
  • Execute SOQL queries by using the Query Editor in the Developer Console.
  • Execute SOQL queries embedded in Apex by using Anonymous Apex.
  • Query related records.(SELECT ID,(SELECT Id FROM Contacts) FROM Account)

Solution:
    /*  Author : Subbareddy Peddireddy
* ClassName : ContactSearch
* */

public class ContactSearch {
/* Method with the name searchForContacts and String as parameters
*  and with Contact List as return type*/

public static List<Contact> searchForContacts(String lastName,String                                              maillingPostal){
List<Contact> contactList=[SELECT id,LastName,MailingPostalCode  FROM Contact where LastName=:lastName AND MailingPostalCode=:maillingPostal ];
return contactList;

}

}
 1.5 Write SOSL Queries
Agenda:
  • Describe the differences between SOSL and SOQL

SOQL will work on single object at a time and it will give exact match for filter .                                 SOSL Will Work on Multiple objects and will work as contains.
  • Search for fields across multiple objects using SOSL queries.
  • Execute SOSL queries by using the Query Editor in the Developer Console.

Challenge:
Create an Apex class that returns both contacts and leads based on a parameter.
To pass this challenge, create an Apex class that returns both contacts and leads that have first or last name matching the incoming parameter.
The Apex class must be called ContactAndLeadSearch and be in the public scope
The Apex class must have a public static method called searchContactsAndLeads
The method must accept an incoming string as a parameter
The method should then find any contact or lead that matches the string as part of either the first or last name
The method should finally use a return type of List<List< SObject>>
NOTE: Because SOSL indexes data for searching, you must create a Contact record and Lead record before checking this challenge. Both records must have the last name Smith. The challenge uses these records for the SOSL search
Solution: 
/*  Author : Subbareddy Peddireddy
* ClassName : ContactAndLeadSearch
* */
public class ContactAndLeadSearch {
/* Method with the name searchContactsAndLeads and String as parameters
*  and with List<List<SObject>>   as return type*/
public static List<List<Sobject>> searchContactsAndLeads(String serchKey){
List<List<SObject>> searchResultsList = [FIND :serchKey IN ALL FIELDS
  RETURNING Lead(LastName,FirstName), Contact(FirstName,LastName)];
return searchResultsList;
}

}