Apex Trigger – What is Recursion and Solutions to Avoid Recursion in Apex Trigger

Before moving on to Recursion in Apex, one needs to understand what is Apex Trigger.

Apex Trigger – Apex Trigger is a piece of code that executes Before and After a record is Inserted, Updated, Deleted in a Database.

Triggers are majorly used to Modify Same Records and Create or Updates Related Records, and configure Follow up Emails on Record Creation.

In order to take understanding as how “Apex Triggers” work and what other things we can do using “Apex Triggers” please refer to the following URL.

http://www.vinaychaturvedi.com/blog/apex-triggers-in-salesforce

Once we have understanding as how Apex Triggers work we need to avoid “Recursion” in it.

What is“Recursion” in Apex Trigger?

Recursion is when same code is executing again and again & it exceeds the “Governor Limit”.

When Trigger is called Over and Over and It Exceed the Maximum Trigger Depth that is Called Recursive Trigger.

If we Try to Insert Account After Insertion of Account Using Trigger Then it Call again & again and Exceed the Governor Limit (Maximum Trigger Depth) and gives Exception

trigger RecursionExample on Account (After Insert) {

if(Trigger.isAfter&&Trigger.isInsert){

Account acc = new Account(Name = 'Recursive Account');

Insert acc;

}

}

2nd

This is common mistake that most of the Developers do, let us move to a Solution for the same.

How to Avoid Recursion or How can we Write Recursion Free Apex Trigger?

Write Apex Handler Class with Static Boolean Variable and Method and return true if Boolean Variable is true otherwise False .

Check in Apex Trigger if Handler class Method Return True Then Execute your Logic

public Class HandlerClass{

public static booleanRecursionHandler = True;

public static booleanHandlerMethod(){

if(RecursionHandler){

RecursionHandler = False;

return true;

}

else{

return RecursionHandler;

}

}

}

Apex Trigger in Which Handler Class Called.

trigger RecursionExample on Account (After Insert) {

if(Trigger.isAfter&&Trigger.isInsert){

if(HandlerClass.HandlerMethod()){

Account acc = new Account(Name = 'Recursive Account');

Insert acc;

}

}

}

OR

If you do not want to use Handler Class Method thenWrite Apex Handler Class with Static Boolean Variable and Check Handler Class Variable in Apex Trigger IF it is True then Execute Your Logic and Make it false So that Trigger Can not execute Again.

public Class HandlerClass{

public static boolean RecursionHandler = True;

}

 

trigger RecursionExample on Account (After Insert) {

if(Trigger.isAfter&&Trigger.isInsert){

if(HandlerClass.RecursionHandler){

HandlerClass.RecursionHandler = False;

Account acc = new Account(Name = 'Recursive Account');

Insert acc;

}

}

}

 

 

 

How to Tackle View State Issue During Upload file as a Attachment through Visualforce Page

In Previous Post, We Saw How to Upload File in Salesforce Through Visualforce .

So This Post is just to Handle the View State Error Occurred During Uploading File through Visualforce.

1st

For Complete Detail of View State Refer Link .

If you look into the Previous Post there were many Variables in Apex Controller which were use for getting file Body, Name, Description .

2nd

3rd

So If User Upload Large File Then file Body, Name and Description Come in these variables and It would Easily Increase the Heap Size Limit and View State Issue would arise.

4thSo For Avoiding this Issue Directly Make a Attachment Object Instance in Apex Controller and Get Body, Name of File From Visualforce Using that Instance .

5th

6th

Instead of Storing file body, Name, Description in these variables in apex controller it would Store directly in SObject Attachment So Heap Size Limit will not exceed.

Please Refer the Visualforce Page and Apex Controller for More Details.







Upload File as Attachment
public with sharing class AttachmentUploadController {
public Id PageId ;
public Attachment attachment {
get {
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}

public AttachmentUploadController(){
PageId = ApexPages.currentPage().getParameters().get('id');
}

public PageReference upload() {
attachment.ParentId = PageId ;
attachment.IsPrivate = true;
try {
insert attachment;
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
} finally {
attachment = new Attachment();
}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}

}

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at [email protected] .

 

 

How to Tackle View State Issue During Upload file as a Attachment through Visualforce Page – Part 2

1st

Hello guys , I am Back with my new Blog Post . In this Post You will Learn Two things .

  • Upload file as a Attachment  Without Apex Controller
  • What is Salesforce Ajax toolkit and How you can use Ajax Toolkit for Upload File

Please Refer my Previous Blog Posts to get to know Upload file through visualforce and Tackle View State Issue through apex Controller

First of all Do you Know about Salesforce Ajax Toolkit ???q-590x446

If you don’t know then do not worry just take long breath and read this artical.

Let’s come to the Point and discuss What is Salesforce Ajax Toolkit ?????

Ajax Toolkit is like a Javascript Wrapper which you can use in Your Visualforce Page to Interact with Salesforce Standard and custom Objects .

You can query Salesforce Objects Data , Create records and attachments without using Apex with your Visualforce Page.

Now the Questions arise When to use ajax toolkit??

Best situation to work with ajax toolkit is when you have to work with small amount of data.

AJAX works best with  small amounts of data (up to 200 records, approximately six fields with 50 characters of data each).

You have to follow three steps for working with ajax toolkit in your visualforce Page.

  • Connecting to the API – Include Connection.js file that is ajax toolkit Javascript file in your Visualforce Page and Get the Sforce Session using sforce.connection.sessionId='{!GETSESSIONID()}’

    After execution of Connection.js script , the toolkit is loaded and a global object, sforce.connection, is created. This object contains all of the API calls and AJAX Toolkit methods, and manages the session ID.

  • Embed API Calls in Javascript – Write Javascript code for Making Instance of  Sforce Object like “new sforce.SObject(“Attachment”)” and create attachment using sforce.connection.create([attachment] .
  • Processing result – Process the Result on creation of attachment and show success or failure message .onSuccess : function(result, source) {

    if(result[0].getBoolean(“success”)) {

    // do your code whatever you want to show on success

    }

    },

    onFailure : function(error, source) {

    // do your code on failure

    }

You can go through the Salesforce Ajax Toolkit Developer Docs for more reference .

Here is the Look of your Visualforce page .

When you Load the Page .

1st

When you Clicked Upload Button Without Record ID in URL .

3rd

When you Clicked Upload Button Without Selecting File for upload.

4th

When you selected file and clicked Upload Button .

2nd

Here is the Visualforce Page Code With Ajax Toolkit.

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at [email protected] .

Visualforce – Get All Accounts Related to Name Written in Input field

To Achieve this Functionality, one needs to use Jquery.

In order to use Jquery , Upload Jquery Script as a Static resource in your Salesforce Organization and Use this Script in Visualforce Page.

In order to take understanding as how it works please refer to the following URL.

Here is the Controller and Visualforce Page.







Account Name : 






















 

public with sharing class FindAccountRelated_To_Search_Ext {

public String AccName {get;set;}
public List AccountList {get;set;}

public PageReference RelatedAccounts(){

return null;
}

public List getAllAccounts(){

AccountList = [Select ID,Name From Account Where Name LIKE : ('%'+AccName+'%') ];
return AccountList;
}

}

Some points which you should know for this Visualforce Page.

1 – Use Input Field for Name of Account.

2 – Use Pageblock Table or DataTable For Display Records.

3 – Use Keypress  Jquery Event to Get the Input.

4 – Use Action function to Send the Input Value to Controller.

5 – Use SOQL Query In controller to Get All Related Records as Written in Input Field.

 

Please Follow if you like my Blog and share your valuable inputs.

 

 

 

Upload File – Upload file as an Attachment OR as a Salesforce File Related to Particular Object using Visualforce Page

Visualforce Page for Upload File as an Attachment OR in Chatter.

2ndTo Upload file as an attachment or as a file using Visualforce,one must have Record Id so that the Uploaded file links with that Record.

In Order to use Bootstrap, JQuery or CSS for Designing purpose you can refer to the following link to access Script files

https://drive.google.com/open?id=0B55QW3bpdoFPYkpyWXZJOE1rZFU

Upload this Zip file as a static Resource in your Salesforce Org and use it in Visualforce Component.

1st











Now Use this Component in your Visualforce Page and use some Apex Input and Output Tags.

  1. to get File title and to get File Description as Input from User .
  2. for choose attachment file.
  3. to Save Attachment .
  4. Jquery function to show to name of file.







Upload File as Attachment

In order to Insert file as an Attachment Related to Record Just make the instance of Attachment Object and put the Values Like File Title, File Description, Body, Related Record Id.

In Order to Insert file as Salesforce File, first of all we need to make the Lookup field for particular Object with related list in which we want to insert file in Content Version. Please refer to the steps I have performed “I have created 2 Lookup fields in Lead and Account Objects”.

To create Lookup fields in Content Version

Salesforce Files -> Create, Modify, Delete custom fields –> New Field

3rd

4th

5th

After creating this Lookup Field Make Instance of Content Version and put Required Fields as we did for Attachment.

Please refer to the controller for more details.

public with sharing class UploadFileController {

Public Id PageId {get;set;}
public Blob AttchBody {get;set;}
public String AttchDesc {get;set;}
public String AttchName {get;set;}
Public Attachment attch {get;set;}

public UploadFileController(){

PageId = ApexPages.CurrentPage().getParameters().get('Id');

}

public PageReference Upload(){

attch = new Attachment(ParentId=PageId,Description=AttchDesc,Name=AttchName,Body=AttchBody);

try{
insert attch;
AttchBody = null;
AttchDesc = null;
AttchName = null;
}
catch(Exception ex){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getlineNumber()+' '+ex.getMessage()));
return null;

}
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}

public PageReference UploadChatterFile() {


ContentWorkspace cw =[SELECT Id FROM ContentWorkspace WHERE DeveloperName = 'Test_Workspace'];

ContentVersion v = new ContentVersion();
v.versionData = AttchBody;
v.title = AttchName;
v.FirstPublishLocationId = cw.Id;
v.pathOnClient = AttchName;

If(PageId.getSObjectType() == Account.sObjectType){
v.Account__c = PageId;
}
If(PageId.getSObjectType() == Lead.sObjectType){
v.Lead__c = PageId;
}

v.Description = AttchDesc;

try{
insert v;
AttchBody = null;
AttchDesc = null;
AttchName = null;
}
catch(Exception ex){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getlineNumber()+' '+ex.getMessage()));
return null;
}

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully in Chatter'));
return null;
}
}

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at [email protected] .

 

 

Salesforce CRM Content – Brief Description about CRM Content and How can Developer and Admin Use CRM Content

Salesforce CRM Content Organize, share, search, and manage content within your organization .Content includes all file types, from traditional business documents such as Microsoft PowerPoint presentations to audio files, video files, Web pages, and Google docs.

It Simplifies content management by incorporating user-friendly features into the following :

  • Organizing
  • Searching
  • Subscribing
  • Contributing

Most of the People Think Why should I Use Salesforce CRM Content? what is the Benefits of using CRM Content ?

Why-is-Thinking-so-important-300x300

I Suggest to those People Please Refer the Below amazing Articles.

http://cloud4good.com/announcements/why-should-you-use-salesforce-content-why-not/

https://www.linkedin.com/pulse/what-benefits-professionals-can-leverage-from-crm-content-systems/

So After Understanding the concept let’s Move to the other Step

How to enable CRM Content in your Organization?

Refer Below Link and Enable CRM Content in your Organization Step by Step.

https://support.cloudhq.net/how-to-enable-document-libraries-in-salesforce/

What Salesforce Admin Should Know about CRM Content?

  • Publish Files to Libraries –

Go to the Libraries Tab and click on Contribute .

2nd

Choose File from your Local system and Enter Description , Select Library in which you want to Publish the file and Click on Save .

3rd

4th

You can Share File with your Standard OR Custom Object But you need to make Lookup field of that Object with Standard Object (Content Version) of CRM Content .

  • Create a New Library –

Click on New Button and enter Name, Description and Unique Name of the Library.

5th

  • Update Content Version –

Select any file Uploaded in CRM Content  From the Top Content Section  Or you can View file from Library and Click on Edit and Select Update Content Version and Choose file and Update the Version of that Content.

Using this you can publish a new Version of the file and update file Properties.

6th

8th

  • CRM Content Permissions ,Custom fields ,Customization of content Page Layouts

Search Salesforce Files in the left side Search Bar.

9th

You would These Options After clicking on Salesforce Files

10th

So These are the all customization which you can do regarding Library Permissions for Users, Create, modify and Delete Fields on Content Version, Manage Validation Rules etc..

Create, modify and delete custom fields is the option where you can make Standard or custom objects Lookup with Content Version So that you can upload file with the object from libraries tab.

As you see in Below Picture I can Upload File with Lead Or Account Object.

11th

What about Developers ?

11_md_confab_380

What Developer should Know about CRM content for working with Apex ?

Below is the CRM Content Object Hierarchy or you can say Data Model . This is thing which Developer must know for working with CRM Content.

 

12th

ContentWorkspace – ContentWorkspace is a Library which you can use for publish File . You can use Workspace(Library) Id while making Instance of Content Version so the File publish in that Workspace.

13th

ContentWorkspaceDoc – ContentWorkspaceDoc  Represents a link between a document and a public library in Salesforce CRM Content . It allows a document to be shared with multiple libraries.

In the ContentWorkspaceDoc there are two lookup Fields that is ContentWorkspaceId and ContentDocumentId.

14th

ContentDocument – ContentDocument is a document that has been uploaded in library in CRM Content.

For upload file as Content Document you have to Make Instance of Content Version Because Create() call not supported in Content Document.

15th

ContentVersion – This is a Specific Version of a Content Document(File) in CRM Content.

16th

So these are some things Developer or Admin should Know.

Please Refer the Below Links for Complete Details of Content Object.

https://help.salesforce.com/articleView?id=content_parent.htm&type=0

https://resources.docs.salesforce.com/210/latest/en-us/sfdc/pdf/salesforce_content_implementation_guide.pdf

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_content.htm#sforce_api_erd_content

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at [email protected] .

Apex Rest API – Salesforce Integration with Twilio(Get a Call on your Mobile Number from Salesforce Through Twilio Voice API)

First of all, you need to Create an account on Twilio. If you do not a have an account on Twilio please refer to the link.

If you have Already then just Log in to your Twilio account. After Log in Your Twilio Account, you will See ACCOUNTSID and AUTH TOKEN as shown in the screenshot below.

1st

Copy this ACCOUNTSID and AUTH TOKEN we will use it later.

After this Click on Left Side Icon in Twilio Account and click on # Phone Numbers

3rd

4th

You would see your Twilio number (you would receive calls from this number on your connected Mobile number)

5th

After this Move on to the Verified Caller IDs on Left Side Bar and Enter Your Mobile Number on which you want to receive Call and Verify by Twilio.

6th

Now Login into your Salesforce Account and create a Remote Site Setting of Twilio End Point URL as given in the Twilio API Documentation

2nd

For Beginners Here is the Explanation of Remote Site Setting.

“When you want to access the external sites in your Salesforce application using callouts, web services, etc… You need to add that in the Remote Site Settings. This is just a security that force.com platform is going to check.
Before any Visualforce page, Apex callout, or JavaScript code using XmlHttpRequest in an s-control or custom button can call an external site, that site must be registered in the Remote Site Settings page, or the call will fail”

Now moving on to the Integration part, according to the Twilio voice API . We Need DEFAULT VERSION , ACCOUNTSIDAUTH TOKEN , Twilio Phone Number(From Phone Number) , Twilio Verified Phone Number(To Phone Number), Voice API End Point for Doing this Integration From Salesforce .

7th

 

8th

 

Here is Apex Controller with Apex REST API and Visualforce Page

public with sharing class TwilioVoiceCallExt 
{
 public String mobileNumber {get;set;}
 public String responseBody {get;set;}
 public CallResponse callRes{get;set;}
 public String DEFAULT_VERSION = '2010-04-01';
 public String AccountSID = 'YOUR ACCOUNTSID';
 public String Authtoken = 'YOUR AUTH TOKEN';
 public String FromPhoneNumber = 'YOUR Twilio Phone Number';
 public String VoiceAPIEndPoint = 'https://demo.twilio.com/welcome/voice/';

 public String Body = '';

 public Void VoiceAPI()
 { 
 String Crediential = EncodingUtil.base64Encode(Blob.valueOf(AccountSID + ':' + Authtoken));
 String EndPoint = 'https://api.twilio.com/'+DEFAULT_VERSION+'/Accounts/'+AccountSID+'/Calls.json';

 if(mobileNumber != '')
 {
 Body = 'To='+mobileNumber+'&'+
 'From='+FromPhoneNumber+'&'+
 'Url='+VoiceAPIEndPoint;
 }
 else
 { 
 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Mobile Number is required.'); 
 ApexPages.addMessage(myMsg); 
 return;
 }

 Http h = new Http();
 HttpRequest request = new HttpRequest();
 request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
 request.setHeader('Authorization', 'Basic '+Crediential);
 request.setEndpoint(EndPoint);
 request.setMethod('POST');
 request.setBody(Body);
 HttpResponse res = h.send(request);
 responseBody = res.getBody();

 //------------Deserialize the Reponse in different Class------------------//
 callRes = (CallResponse)JSON.deserialize(responseBody, CallResponse.class);

 if(callRes.Status == 'queued')
 {
 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Call successfully sent..!! Please Check Entered Mobile Number'); 
 ApexPages.addMessage(myMsg); 
 }
 else if(callRes.status == '400'){
 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Mobile number is not valid'); 
 ApexPages.addMessage(myMsg); 
 } 

 }// End of function

 //-----class for get Response---------//
 public class CallResponse 
 {
 public String Status; 
 }
}









In Case of Trail Twilio Account You can Call Only One Mobile Number which is Verified by Your Twilio Account !!











Mobile Number









Refer Below Links for Complete Details of APEX REST API.

http://www.salesforcetutorial.com/salesforce-integration-rest-api-basics/

http://www.jitendrazaa.com/blog/salesforce/call-salesforce-rest-api-from-apex/

https://trailhead.salesforce.com/en/modules/apex_integration_services/units/apex_integration_rest_callouts

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at [email protected] .

 

Create Remote Site Setting through Visualforce Page

Hey Salesforce Ohana, I am back with new blog post. In this blog I will explain How to create a Remote Site Setting through coding.

Most of developer or admin usually create Remote Site Setting through Remote Sites Section [Administer -> Security Control -> Remote Site Settings] in your Salesforce organization.

If you do not know what is Remote Site Setting?

Why we use Remote site setting?

You can refer this blog post for complete details.

Now it’s time for coding.

Coding

Before starting , you should know about MetaData API .

A Secrete key to access, create your salesforce.com Metadata like Workflows, Remote Site, Validation Rules.

Let’s download MetaData WSDL File [Build -> API -> MetaData WSDL] and generate apex class using this WSDL File [Build -> Develop -> Apex Classes -> Generate from WSDL] or you can refer this MetadataService apex class.

1st

Add your Salesforce Organization domain name and your visualforce page domain name in Remote site setting by following steps.

[Administer -> Security Control -> Remote Site Settings]

Now, Create a Visualforce Page for input Remote site details and apex controller for business logic.

Below is your apex controller code which will call MetaDataPort Method in the MetaDataService apex class for generating of session Id your salesforce organization and enter remote site details and call createMetaData() method.

2nd

You can refer the complete code here.

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at [email protected] .

Happy Coding ?