Pages

Wednesday, April 3, 2013

Apex Class with Sharing and without sharing


Before 2 days back when we were solving production issue I realized knowledge in not enough, you must apply. The issue was one of our trigger was working when records are get created from Salesforce Org but It’s was not working when records are created from Customer Portal.  Trigger is firing but it’s not working when records are getting created from portal.

After spending 30 min we realized power of with sharing Keyword. Our trigger handler class marked as with sharing. So sharing rules are getting applied and Some SOQL queries returns zero record and trigger get failed L and make me sad.
Even I was aware of with sharing and without sharing. I did mistake which cost 30 min and one production blocker. 

So what is the base line? Trigger Handler class “without sharing” keyword can save your 30 min.


 Note:  All SOQL or SOSL queries that use PriceBook2 ignore the with sharing keyword

Monday, September 12, 2011


Regular Expression in Salesforce

Data quality is the most important part of  any Salesforce Organisation. So applications works for
salesforce they are nothing but deals with the data. But sometime it works like GIGO “Garbage In Garbage Out”.
If application accepts  the garbage data and saved in Salesforce database  then we are loosing the data quality.
It means salesforce application should be capable to filter data before saving in the salesforce database.
At this point we requires some validation on the input data. To handle this type of situations best process
it to have some regular expression on the input data.
Regular expressions are a way to describe a set of strings based on common characteristics shared by each
string in the set. They can be used to search, edit, or manipulate text and data.
It uses the Pattern and Mapper classes and there methods for Regular Expression.
Pattern class in Apex is exact copy of Java Pattern class.
  • Pattern Class
The Pattern class defines an alternate compile method that accepts a set of flags affecting the way the
 pattern is matched. The flags parameter is a bit mask that may include any of the following public static fields:
In short we need to provide the regx in string format to the Patter.complie(Regx String) method.
Until now, we’ve only been interested in whether or not a match is found at some location within a particular
input string. We never cared about where in the string the match was taking place.
You can make your pattern matches more precise by specifying such information with boundary matchers.
For example, maybe you’re interested in finding a particular string, but only if it appears at the beginning or end of a line. Or maybe you want to know if the match is taking place on a word boundary, or at the end of the previous match.
The following table lists and explains all the boundary matchers.
Boundary ConstructDescription
^The beginning of a line
$The end of a line
\bA word boundary
\BA non-word boundary
\AThe beginning of the input
\GThe end of the previous match
\ZThe end of the input but for the final terminator, if any
\zThe end of the input
e.g     As noted above, ^ matches the beginning of a line, and $ matches the end.
           Enter your regex: ^dog$
           Enter input string to search: dog
           I found the text "dog" starting at index 0 and ending at index 3.

e.g. Salesforce Example
private string GetVersionPhoneNumber(string strTelephone)
{
string strVersionTelephone;
Integer lastEnd=0;
try
{
// Regular expression to get only digit from the Telephone RAW string
// Label.Vcard_TelephoneDigitParserRegx contains the regx string ’[^0-9]‘
Pattern qpEncodingPattern = Pattern.compile(Label.Vcard_TelephoneDigitParserRegx);
strVersionTelephone = qpEncodingPattern.matcher(strTelephone).replaceAll(”);
}
catch(Exception ex){
System.debug(‘Inside the vCard_FieldMap_XformCapture : GetVersionPhoneNumber====>’+ex);
}
return strVersionTelephone;
}