SALESFORCE USEFUL VALIDATION RULES

Improve the quality of your data using validation rules. Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. A validation rule can contain a formula or expression that evaluates the data in one or more fields and returns a value of “True” or “False”. Validation rules also include an error message to display to the user when the rule returns a value of “True” due to an invalid value.

After you have defined validation rules:

  1. The user chooses to create a new record or edit an existing record.
  2. The user clicks Save.
  3. All validation rules are verified.
  • If all data is valid, the record is saved.
  • If any data is invalid, the associated error message displays without saving the record. 
    4.The user makes the necessary changes and clicks Save again.

There are many operators and functions which we can use in salesforce validation.
The most important and tipical is use to REGEX function in validation.

A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager.

REGEX: Compares a text field to a regular expression and returns TRUE if there is a match. Otherwise, it returns FALSE. A regular expression is a string used to describe a format of a string according to certain syntax rules.

How to use regex:
REGEX(text_field, regex_text) replace regex_text with the regular expression you want to match.

Validation Rule Example:
1. This example ensures that a custom field called SSN matches a regular expression representing a valid social security number format of the form 999-99-9999.

Regex to validate user input in SSN field: "[0-9]{3}-[0-9]{2}-[0-9]{4}"
[0-9] => 0 through 9.
{3}   => 3 times.
So [0-9]{3} => 0 through 9 3 times.

Function:
NOT(
    OR(
LEN (SSN__c) = 0,
REGEX(SSN__c, "[0-9]{3}-[0-9]{2}-[0-9]{4}")
    )
)

2. Validates that the account Billing Zip/Postal Code is in the correct format if Billing Country is Canada. Canadian postal code must be in A9A 9A9 format.

Regex to validate user input in account Billing Zip/Postal Code field: "((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"

?i =>   Case-insensitive matching enabled
[ABCEGHJKLMNPRSTVXY] => A, B... or Y
\ => Nothing, but quotes the following character
\d =>   A digit: [0-9]
\s => A whitespace character
[A-Z] =>  A through Z
X? => X, once or not at all

Function:
AND(
OR(
BillingCountry = "CAN",
BillingCountry = "CA",
BillingCountry = "Canada"
),
NOT(
REGEX(
BillingPostalCode,
"((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"
)
)
)


Salesforce + OAuth 1.0


Salesforce integration

The below code is OAuth 1.0 from salesforce.
Here i just give Yelp api intregration example, you can change the code as per your requirement.

public class YelpApiCallout {
    public String consumerKey = 'xxxxxxxxxxxxxxxxx';
    public String token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    public String timestamp = '';
    public String nonce = '';
    public String consumerSecret = 'xxxxxxxxxxxxxx';
    public String tokenSecret = 'xxxxxxxxxxxxxxxxxxx';
    public String signature = '';
    public Map<String, String> parameters = new Map<String, String>();
 
    public String location  {get; set;}
    public String jasonData ;
    public YelpDataWrapper apiRecords   {get; set;}
    public String term   {get; set;}
 
    public void accessToken(){
        term = ApexPages.CurrentPage().getParameters().get('term');
        String locationWOspace = location.replace(' ', '%20');
        String termWOspace;
        if(term != null){
            termWOspace = term.replace(' ', '%20');
        }
        HttpRequest req = new HttpRequest();
        String endpointurl ='http://api.yelp.com/v2/search?                            location='+locationWOspace+'&limit=20&term='+termWOspace;   
        req.setEndpoint(endpointurl);
        req.setMethod('GET');
        sign(req);
        Http httpOath = new Http();
        HTTPResponse res = httpOath.send(req);
     
        jasonData = res.getBody();
     
        //Step 3:parse the received JSON
        JSONParser parser = JSON.createParser(jasonData);
        while (parser.nextToken() != null) {
            if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                 apiRecords = ( YelpDataWrapper)parser.readValueAs(YelpDataWrapper.class);
            }
        }
    }
 
    private void refreshParameters() {
        parameters.clear();
        parameters.put('oauth_consumer_key',consumerKey);
        if(token!=null) {
            parameters.put('oauth_token',token);
        }
        parameters.put('oauth_signature_method','HMAC-SHA1');
        parameters.put('oauth_timestamp',timestamp);
        parameters.put('oauth_nonce',nonce);
    }
 
    private Map<String,String> getUrlParams(String value) {
        Map<String,String> res = new Map<String,String>();
        if(value==null || value=='') {
            return res;
        }
        for(String s : value.split('&')) {
            System.debug('getUrlParams: '+s);
            List<String> kv = s.split('=');
            if(kv.size()>1) {
              String encName = EncodingUtil.urlEncode(EncodingUtil.urlDecode(kv[0], 'UTF-8'), 'UTF-8').replace('+','%20');
              String encValue = EncodingUtil.urlEncode(EncodingUtil.urlDecode(kv[1], 'UTF-8'), 'UTF-8').replace('+','%20');
              System.debug('getUrlParams:  -> '+encName+','+encValue);
              res.put(encName,encValue);
            }
        }
        return res;
    }
 
    private String createBaseString(Map<String,String> oauthParams, HttpRequest req) {
        Map<String,String> p = oauthParams.clone();
        if(req.getMethod().equalsIgnoreCase('post') && req.getBody()!=null &&
           req.getHeader('Content-Type')=='application/x-www-form-urlencoded') {
            p.putAll(getUrlParams(req.getBody()));
        }
        String host = req.getEndpoint();
        Integer n = host.indexOf('?');
        if(n>-1) {
            p.putAll(getUrlParams(host.substring(n+1)));
            host = host.substring(0,n);
        }
        List<String> keys = new List<String>();
        keys.addAll(p.keySet());
        keys.sort();
        String s = keys.get(0)+'='+p.get(keys.get(0));
        for(Integer i=1;i<keys.size();i++) {
            s = s + '&' + keys.get(i)+'='+p.get(keys.get(i));
        }
 
        return req.getMethod().toUpperCase()+ '&' +
            EncodingUtil.urlEncode(host, 'UTF-8') + '&' +
            EncodingUtil.urlEncode(s, 'UTF-8');
    }
 
    public void sign(HttpRequest req) {
        nonce = String.valueOf(Crypto.getRandomLong());
        timestamp = String.valueOf(DateTime.now().getTime()/1000);
 
        refreshParameters();
 
        String s = createBaseString(parameters, req);
 
        System.debug('Signature base string: '+s);
 
        Blob sig = Crypto.generateMac('HmacSHA1', Blob.valueOf(s),
                       Blob.valueOf(consumerSecret+'&'+
                                    (tokenSecret!=null ? tokenSecret : '')));
        signature = EncodingUtil.urlEncode(EncodingUtil.base64encode(sig), 'UTF-8');
        System.debug('Signature: '+signature);
 
        String header = 'OAuth ';
        for (String key : parameters.keySet()) {
            header = header + key + '="'+parameters.get(key)+'", ';
        }
        header = header + 'oauth_signature="'+signature+'"';
        System.debug('Authorization: '+header);
        req.setHeader('Authorization',header);
    }
 
    public class YelpDataWrapper
    {
       public List<businesses> businesses  {get; set;}
    }
 
    public class businesses
    {
        public String rating               {get; set;}
        public String image_url            {get; set;}
        public String review_count         {get; set;}
        public String name                 {get; set;}
        public String snippet_image_url    {get; set;}
        public String url                  {get; set;}
        public String rating_img_url_large {get; set;}
        public String id                   {get; set;}
        public String snippet_text         {get; set;}
    }
 
}

AngularJS + OAuth 1.0


OAuth callout is very simple and easy with AngularJS.
Here i just used a simple Yelp Api integration example . You can change this as per your requirment.

We need to construct a signature "base string", which consists of a concatenation of three request elements:
1. The HTTP request method.
2. The base URL the request is being sent to.
3. A normalized string of the parameters in the request.

and generate an oauth_signature using HMAC-SHA1 method.


Js code:
var app = angular.module('MyApp', []);

app.controller('MainCtrl', ['$scope', 'MyYelpAPI', function($scope, MyYelpAPI) {
$scope.businesses = [];
MyYelpAPI.retrieveYelp('', function(data) {
$scope.businesses = data.businesses;
    });

}]).factory("MyYelpAPI", function($http) {
return {
"retrieveYelp": function(name, callback) {
var method = 'GET';
var url = 'http://api.yelp.com/v2/search';
var params = {
callback: 'angular.callbacks._0',
location: 'San+Francisco',
oauth_consumer_key: 'xxxxxxxxxxxxxxxx', //Consumer Key
oauth_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //Token
oauth_signature_method: "HMAC-SHA1",
oauth_timestamp: new Date().getTime(),
oauth_nonce: randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),
term: 'Restaurants'
};
var consumerSecret = '_wkOfBWAlsQK6kUPcDw_MXC8sMM'; //Consumer Secret
var tokenSecret = 'uEqyN4F3GLKEOTAEgNAlyugh1iI'; //Token Secret
var signature = oauthSignature.generate(method, url, params, consumerSecret, tokenSecret, { encodeSignature: false});
params['oauth_signature'] = signature;
$http.jsonp(url, {params: params}).success(callback);
}
}
});

function randomString(length, chars) {
     var result = '';
     for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
                return result;
}



Html code:
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
        <script type="text/javascript"  src="oauth-signature.min.js"></script>
    </head>
    <body ng-app="plunker">
        <div  ng-controller="MainCtrl">
            <ul>
                <li data-ng-repeat="business in businesses">
                    {{business.name}}
                </li>
            </ul>
        </div>
    </body>
</html>

Salesforce Lightning Connect

Lightning Connect is a framework that enables you to access and use external data stored outside Salesforce, such as data in an enterprise resource planning (ERP) system. Copying the external data into your organization can be redundant and inefficient, especially if the data is constantly changing. You can instead connect to the external data source and access just the data you need when you need it, via Web service callouts.

Note: "You can currently use Lightning Connect to read external data, but not to create or update it."

External Objects vs. Custom Objects:

External objects share much of the same functionality as custom objects. For example, you can:

  • Access external objects via list views, detail pages, record feeds, custom tabs, and page layouts.
  • Define relationships between external objects and standard or custom objects to integrate data from different sources.
  • Enable Chatter feeds on external object pages for collaboration. 
Here is a quick comparison of the features supported in external objects and custom objects:

FeatureCustom ObjectsExternal Objects
Data stored in SalesforceYesNo
ReadYesYes
WriteYesNot Yet
Tabs, LayoutsYesYes
VisualforceYesYes
Field-level SecurityYesYes
SharingYesNo
REST and SOAP APIYesYes
SOQLYesYes (limited)
Search and SOSLYesYes (pass-through)
Formula FieldsYesNot Yet
Workflow, TriggersYesNot Yet
Reports and AnalyticsYesNot Yet
ChatterYesYes (no field tracking)
Types of External Connections:

To connect to an external data source and create external objects for it, Lightning Connect uses a specially designed adapter. There are currently three types of adapters.
  • OData 2.0 adapter—This lets you create external objects for data exposed by any OData 2.0 producer on the Internet. OData (Open Data Protocol) is a modern, REST-based protocol for integrating data. Vendors such as SAP and Microsoft have already implemented OData support, so products such as NetWeaver and SharePoint are directly accessible. Integration products from Salesforce partners extend the reach of Lightning Connect to a much wider range of back-office systems.
  • Salesforce adapter (pilot only)—This lets you create external objects for data from other Salesforce organizations. It uses the standard Salesforce APIs and directly connects to the remote organization without the need of any intermediary Web service, as is the case with OData.
  • Apex adapter (pilot only)—This is an SDK you can use to write your own adapters. It provides additional flexibility in case the OData or Salesforce adapters aren’t suitable for your needs.


Custom labels in javascript

We can use '{!$Label.Label_Name}' in JS function if this funtion is written on Visual force to access labels.

But Its tricky, If we need to access labels in JS file which is saved in static resource:
There are two way to do the same requiremnt:

1. Pass label as a parameter or pass-through attributes to JS function.

2. JavaScript bridging component:

2.a) loads before your script, like so:
<script>
    window.$Label = window.$Label || {};
    $Label.MyError = '{!JSENCODE($Label.MyError)}';
    $Label.MyPrompt = '{!JSENCODE($Label.MyPrompt)}';
    $Label.MyMessage = '{!JSENCODE($Label.MyMessage)}';
</script>
2.b) Now in your javascript (in a static resource) use the variables just as 
if they were VF without {!}
function errorHandler() {
    console.log($Label.MyError);
    alert($Label.MyMessage);
}

Conditional IF statements in an salesforce email template

Hi all SF guys,
Yes this is possible to have conditional IF statements in an email template.

Today I faced some issues when I was using IF statements in "HTML Email Template".

So, I found that some time its not works when you are using "HTML Email Template"  because of metadata issue.

Issue:
If you are using IF statements in HTML email template, some time its metadat in backend add some formating html and css inside of IF statements, Thats why its not working.

Solution:
Edit the email template matadata and remove all html and css which is inside your IF statement:

Note: Please use eclipse IDE or sublime text to edit email template.

Ex:
IF statements in an email template not works when email template metadata like as:
<div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;">
{!IF(Lead.vpUID__c&nbsp;=&nbsp;""<span style="font-size: 12pt;">,&nbsp;</span>"<span style="font-size: 12pt;">No"</span><span style="font-size: 12pt;">,&nbsp;</span></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><span style="font-size: 12pt;">"Yes</span><span style="font-size: 12pt;">"</span><span style="font-size: 12pt;">)}</span></div>

To make it worable please remove all strange html inside from IF statement like as:
<div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;">
{!IF(Lead.vpUID__c = "", "No", "Yes)}
</div>

After removing all these strange and useless html, Save it.
Now this will work for you :)

Custom Salesforce Button to Execute JavaScript


  1. From Setup in your DE org, click Create | Objects and Select any one on which you wants custom button.
  2. Scroll down to the Buttons, Links, and Actions section and click New Button or Link.
  3. In the Label field, enter "label name".
  4. For Display Type choose Detail Page Button.
  5. For Behavior choose Execute JavaScript.
  6. For Content Source choose OnClick JavaScript.
Notice that you are creating a Detail Page button that executes some JavaScript. For your convenience, here is the JavaScript example code that you can copy and paste into the form: 
  
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

sforce.connection.sessionId = '{!$Api.Session_ID}';     
var result = sforce.connection.describeSObject("Contact");
for (var i=0; i<result.fields.length; i++) { 
var field = result.fields[i];
alert(field.name);
}

Set Up Test Data in Salesforce Test Class

Set Up Test Data for an Entire Test Class

Use test setup methods (methods that are annotated with @testSetup) to create test records once and then access them in any test method in the test class. Test setup methods are useful and can be time-saving when you need to create a common set of records that all test methods operate on or prerequisite data.

Records created in a test setup method are rolled back at the end of test class execution. Test setup methods enable you to create common test data easily and efficiently. By setting up records once for the class, you don’t need to re-create records for each test method.

Syntax:

Test setup methods are defined in a test class, take no arguments, and return no value. The following is the syntax of a test setup method.
@testSetup static void methodName() {
}

Example:

@isTest
public class TestSetupMethodCls{
 
    @testSetup static void TestSetupMethod(){
Account acc = new Account(Name = 'Test Acc');
                insert acc;
}

static testMethod void TestAccount(){
Account acc = [Select Id, Name form Account Limit 1];
System.assertEquals('Test Acc', acc.Name);
}

}

Test Setup Method Considerations:

Test setup methods are supported only with the default data isolation mode for a test class. If the test class or a test method has access to organization data by using the @isTest(SeeAllData=true) annotation, test setup methods aren’t supported in this class.
 Multiple test setup methods are allowed in a test class, but the order in which they’re executed by the testing framework isn’t guaranteed.
  • If a fatal error occurs during the execution of a test setup method, such as an exception that’s caused by a DML operation or an assertion failure, the entire test class fails, and no further test methods are executed.
  • If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

SALESFORCE LIGHTNING COMPONENTS


Salesforce1 Lightning Overview

We’re in an increasingly multi-device world, so Salesforce created Lightning to make it easy to build responsive applications for any screen.

Lightning includes a number of exciting tools for developers, such as:  
  1. Lightning components give you a client-server framework that accelerates development, as well as app performance, and is ideal for use with the Salesforce1 mobile app.
  2. The Lightning App Builder empowers you to build apps visually, without code, quicker than ever before using off-the-shelf and custom-built Lightning components
Using these technologies, you can seamlessly customize and easily deploy new apps to mobile devices running Salesforce1. In fact, the Salesforce1 mobile app itself was built with Lightning components.

What is the Lightning Component Framework? 

The Lightning Component framework is a UI framework for developing dynamic web apps for mobile and desktop devices. It’s a modern framework for building single-page applications engineered for growth.

The Lightning Component framework is built on the open-source Aura framework available at http://github.com/forcedotcom/aura. You can build general-purpose web apps hosted on your own server using the Aura framework, which also provides features and components that may not be available in the Lightning Component framework. Many of the sample code here uses out-of-the-box components from the Aura framework, such as aura:iteration and ui:button. The aura namespace contains components to simplify your app logic, and the ui namespace contains components for user interface elements like buttons and input fields. The force namespace contains components specific to Salesforce.

Components

Components are the self-contained and reusable units of an app. They represent a reusable section of the UI, and can range in granularity from a single line of text to an entire app.


Where You Can Use Lightning Components

Salesforce provides several ways that you can use Lightning components.
  • Create a Standalone App.
  • Create Lightning Components for Salesforce1.
  • Customize Salesforce1 with Lightning Extensions (Pilot).

Lightning Components Quick Start

Step 1: Sign up for a Developer Edition Environment
Step 2: Create a Namespace
Every Lightning component is prefixed by a namespace, so the first thing you need to do is register for a unique namespace. 

  • Click Setup | Create | Packages and then click Edit. 
  • Click Continue. 
  • Type a namespace and then click Check Availability to see if it’s unique. 
  • Click Review My Selections and then Save.

Step 3: Enable Lightning Components
  • From Setup, click Develop | Lightning Components. 
  • Select the Enable Lightning Components checkbox and click Save.
Step 4: Now, Create Your First Lightning App
In your developer console, select File > New > Lightning Application.

You will then see a standard New Lightning Bundle dialog. These settings control how you will access the app and how it will be documented in AuraDocs.


You will now see a basic editing screen. Pay attetion to the elements on the right side of your console. These is where you select the type of code you want to add to your app. Also of note is the “Preview” button at the top. You will use this to preview your app.


Next, in the Application element, paste this sample code.

<aura:application>
    <h1>Hello Blog</h1>
</aura:application>
Great! You have created your first Lightning Application. When you click the preview button, you will see this:


Next, let’s style it. Select the “Style” element on the right, and replace the default with this CSS.

.THIS {
 color: red;
}

When you preview again – and you might have noticed that the preview automatically reloaded in your browser – you’ll see this. 
To see more how CSS works with Lightning components go to this link: https://developer.salesforce.com/trailhead/lightning_components/lightning_components_creating


Create a Sample Component, Add to the Sample App

Now, instead of a Lightning Application, let’s create a Lightning Component. In the Developer Console, click File > New > New Lightning Component. The prompt for a new Lightning Bundle looks pretty much the same:


And it results is a very similar empty document, except this time it’s a component.


Note that you again have an element selector on the right, this time without a Preview button. Start creating this component by pasting the following into the component element.

<aura:component >
 <h1>Component 1 - Hello!</h1>
</aura:component>

Create a Lightning Component Tab for the new Component

Before we can create a tab for the new component, we need to flag it as “appHostable”. Edit it to add that attribute to your component, and be sure to save when you’re done.

<aura:component implements="force:appHostable">
    <h1>Component 1 - Hello!</h1>
</aura:component>
Next, using the main Setup screen instead of your Developer Console, navigate to Setup > Create > Tabs, and click the New button next to Lightning Component Tabs.


Next, complete the form. If you don’t see the component in your drop down, that means the component doesn’t implement appHostable.

Next is the standard Add to Profile dialog. Since this is a demo, go ahead and leave Default On for everyone. Scroll to the bottom and click on Save.

Add to your Salesforce1 Mobile Navigation

Getting the Lightning Component Tab on your Salesforce1 mobile app is also easy. On the main setup screen, navigate to Setup > Mobile Administration > Mobile Navigation.



Move the app you created from the left to the right to put it on Salesforce1, and then move it from the bottom to the top in the right menu to make it dead easy to find. Click Save.
Next, login to Salesforce1 on your mobile device using your new Developer Edition credentials. Refresh the menu and you’ll see the new tab you created at the top.