Вы находитесь на странице: 1из 28

Salesforce Training

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;

// Create a master list of accounts to bulk update


List<Account> accounts = new List<Account>();

for (Opportunity opp : Trigger.new) {


// Make sure we meet the minimum threshold
if (opp.Amount >= OPP_MIN_VALUE) {
// This is a trick to get the related account!
Account acc = new Account();
acc.Id = opp.AccountId;

// Update the CSR and add to master list


acc.CSR__c = CSR_USER_ID;
accounts.add(acc);
}
}
// Update the master list of accounts
update accounts;
}
Exercise
• Create class and fetch custom settings records in method
• Store values returned from custom settings in getter setter method
• Print these values on the log and show on page
• Create hierarchy type custom setting to by pass validations in triggers
for profiles users from data loader or integration users
The Visualforce page shows two picklists: one
for country and one for state.
<apex:page controller="CountryStatePicker">
<apex:form >
<apex:actionFunction name="rerenderStates" rerender="statesSelectList" >
<apex:param name="firstParam" assignTo="{!country}" value="" />
</apex:actionFunction>
<table><tbody>
<tr>
<th>Country</th>
<td>
<apex:selectList id="country" styleclass="std" size="1"
value="{!country}" onChange="rerenderStates(this.value)">
<apex:selectOptions value="{!countriesSelectList}"/>
</apex:selectList>
</td>
</tr>
<tr id="state_input">
<th>State/Province</th>
<td>
<apex:selectList id="statesSelectList" styleclass="std" size="1"
value="{!state}">
<apex:selectOptions value="{!statesSelectList}"/>
</apex:selectList>
</td>
</tr>
</tbody></table>
</apex:form>
</apex:page>
public with sharing class CountryStatePicker {
// Variables to store country and state selected by user
public String state { get; set; }
public String country {get; set;}
// Generates country dropdown from country settings
public List<SelectOption> getCountriesSelectList() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('', '-- Select One --'));
// Find all the countries in the custom setting
Map<String, Foundation_Countries__c> countries = Foundation_Countries__c.getAll();
// Sort them by name
List<String> countryNames = new List<String>();
countryNames.addAll(countries.keySet());
countryNames.sort();
// Create the Select Options.
for (String countryName : countryNames) {
Foundation_Countries__c country = countries.get(countryName);
options.add(new SelectOption(country.country_code__c, country.Name));
}
return options; } // To generate the states picklist based on the country selected by user.
public List<SelectOption> getStatesSelectList() {
List<SelectOption> options = new List<SelectOption>();
// Find all the states we have in custom settings.
Map<String, Foundation_States__c> allstates = Foundation_States__c.getAll();
// Filter states that belong to the selected country
Map<String, Foundation_States__c> states = new Map<String, Foundation_States__c>();
for(Foundation_States__c state : allstates.values()) {
if (state.country_code__c == this.country) {
states.put(state.name, state);
}}
Cont..
// Sort the states based on their names
List<String> stateNames = new List<String>();
stateNames.addAll(states.keySet());
stateNames.sort();

// Generate the Select Options based on the final sorted list


for (String stateName : stateNames) {
Foundation_States__c state = states.get(stateName);
options.add(new SelectOption(state.state_code__c, state.state_name__c));
}

// If no states are found, just say not required in the dropdown.


if (options.size() > 0) {
options.add(0, new SelectOption('', '-- Select One --'));
} else {
options.add(new SelectOption('', 'Not Required'));
}
return options;
}
}
Example 2: Create in your org and try fetching in your
code to by pass trigger validations based on profile
Practice all examples minimum two times 
Thank You !!

Вам также может понравиться