Play Games

Search This Blog

Wednesday, March 16, 2016

Points to consider while using custom Settings

Points:
1) Custom settings are a type of custom object. Each custom setting counts against the total number of custom objects available for your organization.
2) The total amount of cached data allowed for your organization is the lesser of these two values:
 a)10 MB
 b)1 MB multiplied by the number of full-featured user licenses in your organization.
3) we can add up to 300 fields per custom setting, unless your field limit for custom objects is lower than 300.
4) We can’t share a custom setting object or record.
5) No owner is assigned when a custom setting is created, so the owner can’t be changed.

Important Tip : When we click on custom Settings,for each custom setting, this page lists the size of one record, the number of records created, and the total size used for each custom setting.
Record size is based on the maximum field size of each field type, not the actual storage that’s used in each field.
When adding fields to a custom setting definition, use the appropriate type and specify a length that doesn’t exceed what’s needed for your data. This action helps you avoid reaching the cached data limit.
Example: if you create a US social security number (SSN) field, select the Text data type and specify a length of 9. If instead you selected a Text Area data type, the field would add 255 characters to the usage count for each record, regardless of the number of characters entered.

Which event will fire when a record is recovered from recycle bin?

Ans) After undelete Event.

After Undelete Event in Trigger

The After undelete event fires when we recover the records from recylebin. It means if we delete the records and then once you undelete from Recycle Bin,at that time after undelete event triggers will fire.
The after undelete trigger events only run on top-level objects.
For example, if you delete an Account, an Opportunity may also be deleted. When you recover the Account from the Recycle Bin, the Opportunity is also recovered. If there is an after undelete trigger event associated with both the Account and the Opportunity, only the Account after undelete trigger event executes.
The after undelete trigger event only fires for the following objects:
1)Account
2)Asset
3)Campaign
4)Case
5)Contact
6)ContentDocument
7)Contract
8)Custom objects
9)Event
10)Lead
11)Opportunity
12)Product
13)Solution
14)Task

Sunday, March 13, 2016

Rest api Integration Example

In this example I am fetching the data from one salesforce instance into other salesforce instance.
Steps:
Step 1: Create a rest resource in one salesforce instance from which we need to retrieve data.Assume this instance as destination.
The sample rest resource class will be like this.
Apex Class:
@RestResource(urlMapping='/displayAccounts/*')
global class AccountRESTController {
    @HttpGet
    global static LIST<Account> getAccounts() {
        List <Account> accountList;
        try {
            accountList = [select id,name from Account limit 5];
            return accountList;
        }
        catch(Exception e) {
           system.debug('errror'+e.getMessage());
        }
        return accountList;
    }
}

In order to access this rest resource from outside, the url or endpoint will be like this "https://ap1.salesforce.com/services/apexrest/displayAccounts" .It means the url of this resource will be salesforcebaseurl, followed by services, followed by apexrest ,followed by keyword used in url mapping.
Step 2:  Create one connected app in destination org where rest resource class is created for authentication purpose.
Go to setup --> create --> apps --> Go to Connected Apps section and click "New" button.
Fill the mandatory fields like connected app Name,API Name,Contact Email fields and select "Enable OAuth Settings checkbox in API(Enable OAuth Settings) Section.
Provide the callback url and the OAuth Scope and then click save button and then continue.
Now the connected app is created as shown in the image below.

The consumer Key and Consumer Secret are generated Which are later used in authentication.
Step 3: Now create one apex class in source instance to fetch data from destination instance.
Apex Class:
public class DataController {
    public List<Account> accountList {get;set;}
    public void retrieveData() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://ap1.salesforce.com/services/oauth2/token');
        req.setMethod('GET');
        req.setBody('grant_type=password&client_id=clientID&client_secret=secretkey&username=username&password=password');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        String resBody = res.getBody();
        Map<String,Object> jsonMap = (Map<String,Object>)JSON.deserializeUntyped(resbody);
        system.debug('jsonMapjsonMap'+jsonMap );
        String accessToken ='';
        if(jsonMap != null && jsonMap.containsKey('access_token')) {
            accessToken = String.valueOf(jsonMap.get('access_token'));
            system.debug('accessTokenaccessToken'+accessToken);
        }
        System.debug('initial response to get token'+res.getBody());
        if(accessToken != null) {
            HttpRequest req1 = new HttpRequest();
            req1.setEndpoint('https://ap1.salesforce.com/services/apexrest/displayAccounts');
            req1.setMethod('GET');
            req1.setHeader('Authorization','OAuth '+accessToken);
            Http http1 = new Http();
            HTTPResponse res1 = http1.send(req1);
            String resBody1 = res1.getBody();
            accountList = (List<Account>) JSON.deserialize(resBody1, List<Account>.class);
            system.debug('accountListaccountList '+accountList );
        }
    }
}
In the above class replace clientID with consumer key and secretkey with Consumer Secret generated in connected App.
Also replace  username and password with salesforce destination instance username and password.
Note :We need to add the destination endpoint url in remote site settings of Source instance as shown in image below.

Step 4: Now create one Visualforce page in source instance to display fetched data from destination instance.
Visualforce Page:
<apex:page controller="DataController">
    <apex:form >
        <apex:commandButton value="Retrieve Data from SFDC" action="{!retrieveData}"/>
        <apex:pageBlock>
            <apex:pageblockTable value="{!accountList}" var ="acc">
                <apex:column value="{!acc.name}"/>
                <apex:column value="{!acc.Id}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output:


Note: Please let me know if you have any doubts...Enjoy Coding.