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>