Documente Academic
Documente Profesional
Documente Cultură
By Gayatri Sharma
SObjects
• Generic sObject means, it can be of any persisted SFDC objects type.
• For ex: Vehicle is generic type and Car, Motor Bike all are concrete
types of Vehicle.
• In SFDC,sObject is generic and Account, Opportunity,
CustomObject__c are its concrete type.
• Generic sObject abstracts the actual representation of any object
assigned to it.
• sObject is the name of the generic abstract type that can be used to
represent any persisted object type.
Exercise:
• Create a class
• Add method to class which will take input as string name of object
and return object of that type
• Print in the anonymous class the values returned by methods
Custom Objects and Standard Objects
• Salesforce comes with a variety of standard objects. For example, the Case
object lets you store information about customer inquiries.
• Administrators can also create custom objects for records that store
information that is unique to an organization.
• Standard objects, such as accounts, contacts, or opportunities are included
with Salesforce by default.
• Custom objects are based on standard objects and extend their
functionality.
• The Salesforce administrator defines custom objects and their properties,
such as custom fields, relationships to other types of data, page layouts,
and a custom tab.
• If the administrator created a tab for a custom object, click the custom
object’s tab to view the records
Exercise
• Create records for standard objects
• Create custom object and create records for it
• On both Objects :
• add custom fields, relationships to other types of data,
• page layouts, and a custom tab.
Custom Settings
• List Type Custom Settings: A type of custom setting that provides a
reusable set of static data that can be accessed across your
organization. If you use a particular set of data frequently within your
application, putting that data in a list custom setting streamlines
access to it.
• Hierarchy Type Custom Settings: A type of custom setting that uses a
built-in hierarchical logic that lets you personalize settings for specific
profiles or users. If the custom setting is a hierarchy, you can add data
for the user, profile, or organization level. For example, you may want
different values to display depending on whether a specific user is
running the app, a specific profile, or just a general user.
Hierarchy Type Custom Settings:
• Hierarchy Custom Settings allow you to associate specific rows with
specific Users or Profiles. For example, you could define a Hierarchy
Custom Setting that associates a different commission rate with
different sales reps, with separate rows for each sales rep’s User Id.
Then you could add a formula field to the Opportunity object, and
reference the Custom setting with a generic formula
like: $Setup.CustomSettingName__c.CustomSettingFieldName__c.
• Whenever someone views an Opportunity record, the formula will be
evaluated, and Salesforce will find the Custom Setting row for the
currently-running User; if none is found, it will find the row for the
current User’s Profile (if one has been defined); and if none is found
again, it will find an organization-wide default value (if defined).
Use of custom settings
• Avoiding Governor Limits: you can retrieve all of the values in a Custom
Setting with absolutely no impact on the governor’s count of the number
of queries you’ve performed or the number of rows you’ve retrieved. This
makes Custom Settings particularly useful for reference data, like lists of
Postal Code / State mappings.
• Hierarchical Access to Data: it allows you to define user-specific
customizations. A common use of Hierarchy Custom Settings is to define
application-specific defaults on a user-by-user basis. For example, imagine
you’ve developed a Visualforce page displays a list of Accounts, allows the
user to select one, and then displays the Cases for that Account. You could
store each user’s most recently selected Account in a Hierarchy Custom
Setting, and then, the next time the user visits the Visualforce page, it
could pre-select the Account that the user selected the last time he was on
the page.
Difference between Custom setting and
Custom Objects
• Custom Settings look very much like Custom Objects. In fact, if you use a tool like the Data Loader
to view a list of objects in your org, you’ll see that Custom Settings are listed together with
Custom Objects, without any visible distinction between the two.
• Limited field types – Custom Settings support only Checkbox, Currency, Date, Date/Time, Email,
Number, Percent, Phone, Text, Text Area, and URL field types. Most notably absent are Formula
and Picklist, as well as field types that define relationships to other objects, like Lookup and
Master/Detail. You can’t create lookups from Custom Objects to Custom Settings either.
• No validation rules – You can’t define validation rules on Custom Settings.
• No workflow or triggers – You can’t define workflow rules or triggers on a Custom Setting. Any
validation of data, update of related records, or other actions that you might use workflow or a
trigger to perform for a Custom Object have to be implemented differently for a Custom Setting.
• No page layouts or record types – You can’t re-arrange fields on the page layout for Custom
Settings. Custom Settings aren’t really intended to be visible to every-day users. If you need them
to be, you can create Visualforce pages to allow users to view and manipulate Custom Setting
data.
List Custom Setting Methods
• The following are instance methods for list custom settings.
• getAll(): Returns a map of the data sets defined for the custom
setting.
• getInstance(dataSetName): Returns the custom setting data set
record for the specified data set name. This method returns the exact
same object as getValues(dataSetName).
• getValues(dataSetName): Returns the custom setting data set record
for the specified data set name. This method returns the exact same
object as getInstance(dataSetName).
Hierarchy Custom Setting Methods
• getInstance(): Returns a custom setting data set record for the current user. The fields
returned in the custom setting record are merged based on the lowest level fields that
are defined in the hierarchy.
• getInstance(userId): Returns the custom setting data set record for the specified user ID.
The lowest level custom setting record and fields are returned. Use this when you want
to explicitly retrieve data for the custom setting at the user level.
• getInstance(profileId): Returns the custom setting data set record for the specified
profile ID. The lowest level custom setting record and fields are returned. Use this when
you want to explicitly retrieve data for the custom setting at the profile level.
• getOrgDefaults(): Returns the custom setting data set record for the organization.
• getValues(userId): Returns the custom setting data set record for the specified user ID.
• getValues(profileId): Returns the custom setting data set for the specified profile ID.
How to fetch records of Custom settings:
Hierarchy__c CS = Hierarchy__c.getInstance();
System.debug(CS.OverrideMe__c == 'Fluffy');
System.debug(CS.DontOverrideMe__c+'World’);
=============================================
Foundation_Countries__c myCS1 =
Foundation_Countries__c.getValues('United States');
String myCCVal = myCS1.Country_code__c;
Foundation_Countries__c myCS2 =
Foundation_Countries__c.getInstance('United States');
String myCCInst = myCS2.Country_code__c;
system.debug(myCCinst+’ ’+ myCCVal);
One important note –
Custom Settings aren’t automatically transferred between sandboxes
and production, so make sure you include them in both places or your
code will break!
Country and State Code Custom Settings
Example
• The Foundation_States custom setting is also a List type of custom
setting and has the following fields: Country Code
• State Code
• State Name
Example:
Example:
• You’re writing a trigger that sets a “Customer Service Rep” field on an
Account every time there’s a high value Opportunity associated with it.
Two things are certain: (1) The CSR on duty changes every week and (2) the
threshold for a “high value” opp changes often since your company is
expanding.
• A perfect use case to use Custom Settings to set the CSR on duty and the
“high value” opp threshold!
• Benefits of using Custom Settings:
• Change your variables through Salesforce.com without deploying code!
• Any non-coder admin can now modify your variables and change how your
code works!
Step 1: Create a Custom Setting “object” for your
trigger: Setup >> Develop >> Custom Settings >> New
Step 2: In your Custom Setting object, create
Custom Fields for each needed variable:
Step 3: Create a Custom Setting record and edit its
variables: Navigate to your Custom Setting >>
Manage >> New
Step 4: Use your Custom Setting in your code!
trigger AddCSR on Opportunity (before insert) {
// Grab your Custom Setting values
CSR_Settings__c settings = CSR_Settings__c.getInstance('csr');
String CSR_USER_ID = settings.CSR_User_ID__c;
Decimal OPP_MIN_VALUE = settings.Opp_Minimum_Value__c;