Monday, April 10, 2017

Built-In Exception Classes in Salesforce

Following are built in exceptions present in apex.
AsyncException – Any problem with an asynchronous operation, such as failing to enqueue an asynchronous call.
CalloutException – Any problem with a Web service operation, such as failing to make a callout to an external system.
DmlException – Any problem with a DML statement, such as an insert statement missing a required field on a record.
EmailException – Any problem with email, such as failure to deliver. For more information, see Outbound Email.
InvalidParameterValueException – An invalid parameter was supplied for a method or any problem with a URL used with Visualforce pages.
LimitException – A governor limit has been exceeded. This exception can’t be caught.
JSONException – Any problem with JSON serialization and deserialization operations. For more information, see the methods of System.JSON, System.JSONParser, and System.JSONGenerator.
ListException – Any problem with a list, such as attempting to access an index that is out of bounds.
MathException – Any problem with a mathematical operation, such as dividing by zero.
NoAccessException – Any problem with unauthorized access, such as trying to access an sObject that the current user does not have access to. This is generally used with Visualforce pages.
NoDataFoundException – Any problem with data that does not exist, such as trying to access an sObject that has been deleted. This is generally used with Visualforce pages.
NoSuchElementException – Used specifically by the Iteratornext method. This exception is thrown if you try to access items beyond the end of the list. For example, if iterator.hasNext() == false and you call iterator.next(), this exception is thrown.
NullPointerException – Calling method on a object who’s value is null
QueryException – Any problem with SOQL queries, such as assigning a query that returns no records or more than one record to a singleton sObject variable.
RequiredFeatureMissing – A Chatter feature is required for code that has been deployed to an organization that does not have Chatter enabled.
SearchException – Any problem with SOSL queries executed with SOAP APIsearch() call, for example, when the searchString parameter contains less than two characters.
SecurityException – Any problem with static methods in the Crypto utility class. For more information, see Crypto Class.
SerializationException – Any problem with the serialization of data. This is generally used with Visualforce pages.
SObjectException – Any problem with sObject records, such as attempting to change a field in an update statement that can only be changed during insert.
StringException – Any problem with Strings, such as a String that is exceeding your heap size.
TypeException – Any problem with type conversions, such as attempting to convert the String ‘a’ to an Integer using the valueOf method.
VisualforceException – Any problem with a Visualforce page. For more information on Visualforce, see the Visualforce Developer’s Guide.
XmlException – Any problem with the XmlStream classes, such as failing to read or write XML.

Wednesday, March 29, 2017

How To Get Current Page URL from Apex code in Salesforce

To implement this we need to use Apexpages.currentPage() methods.

Get Current Page URL
getHeaders() : It return a map of the request headers, where the key string contains the name of the header, and the value string contains the value of the header.
After that get the ‘Host’ key value from that map

getUrl() : It returns the relative URL associated with the PageReference when it was originally defined, including any query string parameters and anchors.
Apex Code :


public with sharing class currentpageurl{
    public  String  headerdata{get;set;}
    public string urlvalue{get;set;}
    public string url{get;set;}
         
public currentpageurl(){      
headerdata= ApexPages.currentPage().getHeaders().get('Host');
urlvalue=Apexpages.currentPage().getUrl();
url='https://' + headerdata+ urlvalue;
    }  
}
Visualforce Page : 

<apex:page controller="currentpageurl"> 

1. {!headerdata} 
2. {!urlvalue} 
3. {!url} 

</apex:page>

 Output:

1. c.cs3.visual.force.com

2. /apex/Pageurl

3. https://c.ap2.visual.force.com/apex/Pageurl

Saturday, March 11, 2017

How to pass parameters from JavaScript to Controller in Salesforce

VisualForce Page

<apex:page>
  <apex:form>
    <apex:pageblock>
   <select id="dropDownId">
   <apex:commandButton value="Update" onclick="validate();return false;" />
 </apex:pageblock>

  <apex:actionFunction name="updateRecords" action="{!performUpdate}"
                              reRender="resultPanel,errMsg" status="statusSaveTrip">
        <apex:param id="status" name="selStatus" value="" />
        <apex:param id="renerTabId" name="renderTabId" value="" />
        <apex:param id="phone" name="phone" value="" />
    </apex:actionFunction>
  </apex:form>
</apex:page>

<script>
  funtion validate(dropDownId )
  {
    //Some processing here
  updateRecords(param1 value,param2 value,param3 value);
  }

</script>

Apex Controller

public class MyController
{
   public MyController()
   {
 
   }
 
   public pagereference performUpdate()
    {
     System.debug('..entered...performUpdate..');  
     String selDropdownVal=Apexpages.currentPage().getParameters().get('selStatus');
     String tableIdtoRender = Apexpages.currentPage().getParameters().get('renderTabId');
     String phone = Apexpages.currentPage().getParameters().get('phone');
     System.debug('..selDropdownVal...'+selDropdownVal+'..'+tableIdtoRender+'..'+phone);
   }

}

What is the maximum number of Promoted Terms we can create in Salesforce

These are nothing but the terms or words which we will be attaching to knowledge articles to make the smother search of the Articles very quick based on prompted words which matches with given search keywords.

These terms are limited to 2000 for an organisation and for better search results it's better to have less than 2000.

Friday, March 10, 2017

How to stop recursive trigger in salesforce

Below are the two primary scenarios:

1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.

2) You want to write a trigger that creates a new record ; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on.

We can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.

Apex Class with Static Variable 

public class ContactTriggerHandler {
     public static Boolean isFirstOccurance = true;
}

  Trigger Code
trigger ContactTriggers on Contact (after update)
{
    Set<String> accIdSet = new Set<String>();
    if(ContactTriggerHandler. isFirstOccurance)
    {
        ContactTriggerHandler. isFirstOccurance = false;
         for(Contact conObj : Trigger.New)
  {
            if(conObj.name != 'Test')
     {
                accIdSet.add(conObj.accountId);
            }
         }
   // any code here
    }

}