Apex based record sharing in Salesforce

Author posted by Jitendra on Posted on under category Categories Apex, Force.com, Salesforce and tagged as Tags , with 21 Comments on Apex based record sharing in Salesforce

Working with Apex based sharing in Salesforce

There are situations where the business requirement is too complex and standard sharing rules provided by the Salesforce will not work.

Example: On Opportunity, you want to give access to record to some users which are in related list.
One way is to manually share the record which will need the interference of opportunity owner. But everyone will love automated solution.

Apex managed sharing provides developers with the ability to support an application’s particular sharing requirements programmatically via Apex code.

Pre requisite:

The object’s organization-wide default access level must not be set to the most permissive access level. For custom objects, this is Public Read/Write. For more information, see Access Levels. This object is used for creating Apex based sharing.

The user to which the record going to be shared must have the object level permission. If you are trying to share the record with edit permission but user does not have the edit permission on that object, then it will not work.

To access sharing programmatically, you must use the share object associated with the standard or custom object for which you want to share. For example, AccountShare is the sharing object for the Account object, ContactShare is the sharing object for the Contact object, and so on. In addition, all custom object sharing objects are named as follows, where MyCustomObject is the name of the custom object: “MyCustomObject__Share”.

Objects on the detail side of a master-detail relationship do not have an associated sharing object. The detail record’s access is determined by the master’s sharing object and the relationship’s sharing setting.

In this article I want to share the custom object “Test__c” with students. Let’s consider that every “Test” has lookup to the “Student”. Continue reading “Apex based record sharing in Salesforce”

APEX based DDP generation using LOOPlus

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce and tagged as Tags , , with 8 Comments on APEX based DDP generation using LOOPlus

Example of generating dynamic document packages (DDP) using Apex and LOOPlus in Salesforce

To automate the document generation in salesforce, we have two well known AppEchange products named “Conga Composer” and “LOOPlus from DrawLoop“. Both products are well used in industry and both have advantages and disadvantages over the need and situation.

This article depicts automating the document generation using Apex in LOOPlus. As i have been through this situation and didn’t find any resource on web to automate the document generation using Apex, so i thought to put this article.

Advantage of LOOPlus over Conga Composer:

  • The one advantage which i know is that using LOOPlus you can combine all the attachments into single one which is not possible in Conga Composer.

Navigate to this URL to install the trial version of Drawloop (LOOPlus) from AppExchange.

To use the Drawloop API we need to use the “Business” version of LOOPlus. To activate this, navigate to “LOOP” app and then “DDP Admin”. Click on button “Modify Subscription services“. New Page will open, select “Business” in LOOPlus level as shown in below image.

Enabling Business Version of DrawLoop
Enabling Business Version of DrawLoop

Continue reading “APEX based DDP generation using LOOPlus”

Introduction to View State in Visualforce

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce, Visualforce and tagged as Tags , with 9 Comments on Introduction to View State in Visualforce

Introduction to View State in Visualforce with example and code walk-through

As we know that http protocol is stateless protocol. Unlike Other programming languages like Java and C# the variables, objects are persisted between different method and network calls. However in case of server client model, the server only stores the session related information of client, however it does not stores the temporary data being used by the clients. Sometime hidden field values, IDs and other information are needed by server for processing.

For example : If client needs any data and is loaded by the server in first request, if error comes the server will need to retrieve data again for second request. Also it is possible that few data is created at client side also, so to persist the state of client View State is used in Visualforce.

Before Summer 12, For every form tag the View State was created. However from Summer 12 release only one View State will be created per page. View State is encrypted and transferred to client with each response. View State cannot be viewed with tools like firebug however salesforce has “View State Inspector” to check the View State Data.

Types of Data Saved in View State:
Following data are saved in Viewstate

  1. All public and private data members present in Standard, Custom and Controller extensions.
  2. Objects that are reachable from data members in Custom and Controller extensions.
  3. The component tree for that page that represents the page’s component structure and the associated state which are the values applied to those components.
  4. Small amount of data needed by visualforce for other general purposes.

Transient Variable and Objects:
As Viewstate is transferred over http with every response, it is very necessary to control the size of View State. There are lots of situations where data is not needed after postback or not needed on Visualforce page then in that case we can make variable or object transient. The transient variables are not passed to view state and therefore not stored in View State. Continue reading “Introduction to View State in Visualforce”

Method Parameters – Pass by Value or Pass by Reference

Author posted by Jitendra on Posted on under category Categories Apex, JAVA and tagged as Tags , with 5 Comments on Method Parameters – Pass by Value or Pass by Reference

Difference between pass by Value and pass by Reference in Salesforce and other Object Oriented Programming language

This topic has always been part of discussion where the answer is not satisfactory and convinced to others. Using this article, I will try to explain the concept of parameter passing in Apex Method. This Complete topic is applicable for Java or other programming languages as well.

Before moving ahead let’s have a summary on what is pass by value and pass by reference.

Pass by value means when a method is called, a second copy of the parameter variable is made in memory, and this copy is passed to the method as a parameter. This copy can be modified in the method, but the original variable in the caller is unchanged, and the changes to the copy are lost upon return.

Pass by reference means when a method is called, that actual variable is passed to the method.

Million dollar question: parameters in method are passed by value or pass by reference?
Most of answers: primitive data types like Integer, Double are pass by value and Objects are pass by reference.


However, the above statement is not 100% true. I know I just invited controversy on this post. Be Patient with me and I would prove with actual example, feel free to run these examples in your Developer Console.

Correct Answer: parameters are passed by value in Apex as well as in Java. Where, in case of object the copy of reference is first made and then passed, therefore we can change the fields of object inside method however we cannot change the Object itself.

Take a pause here and think what I just said. I’m saying, new variable copy created which points to same Object. So, it seems reference but variable technically itself is storing value which is address or reference of object.


Let’s discuss this in more detail. I am going to explain two scenarios in which I will try to change the field value and in second example I will try to change Object itself.

Example 1

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
changeWebsite(a); 
System.debug('Website - '+a.Website); 

public void changeWebsite(Account b) { 
    b.Website = 'www.salesforce.com'; 
}  

Output of above code is

Website - www.salesforce.com

What just happened in Example 1

Salesforce Pass by Value or Pass by Reference - Scenario 1
Salesforce Pass by Value or Pass by Reference – Scenario 1

Explanation:
As per code snippet, the object a is supplied in method changeWebsite(). The parameter is actually passed by value, where the copy of value a is passed to object b which is memory address (aka reference). As we can see that both variable have reference of same object and hence whatever changes done by variable b on that object it will be applicable for variable a also.

If you are still not convinced and think I’m wrong, wait for example 2.

Example 2

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
changeWebsite(a); 
System.debug('Website - '+a.Website); 

public void changeWebsite(Account b) { 
    //Create New Object and try to replace old 
    b = new Account();
    b.Website = 'www.salesforce.com'; 
}  

Before I go further and reveal answer, let’s think what would be output ?

If you assumed method parameters are passed by reference then output should be

Website - www.salesforce.com

However actual output is

Website - www.JitendraZaa.com

What just happened in example 2 ?

Salesforce Pass by Value or Pass by Reference - Scenario 2
Salesforce Pass by Value or Pass by Reference – Scenario 2

Explanation:
As per code snippet, the object a is supplied in method changeWebsite(). Like I said, the parameter itself is actually passed by value, where the copy of value a is passed to object b which was reference. As we can see that both variable had reference of same object initially. However, inside the method new object is created and reference assigned to b and just now important event occurred. Refer above image for clear explanation

Previously b was pointing to old object, if it was really reference then it should impact object a. However now a and b variables point to different object. Whatever the changes done inside method, it will not be reflected outside as object a still have old reference. And hence after method execution when we check the value of object a, it is not affected because of method execution.

Salesforce documentation says :

“In Apex, all primitive data type arguments, such as Integer or String, are passed into methods by value. This means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.
Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.”

Time for Best Practice

Because of behavior of parameters are passed as a value, it is always best practice to return object back to calling method. JUST DON’T ASSUME that object is passed by reference and whatever happened inside method will reflect calling method.

So code would become

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
a = changeWebsite(a); 
System.debug('Website - '+a.Website); 

public Account changeWebsite(Account b) { 
    //Create New Object and try to replace old 
    b = new Account();
    b.Website = 'www.salesforce.com'; 
    return b ;
}  

Creating Trigger on Attachment in Salesforce

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce and tagged as Tags , , with 8 Comments on Creating Trigger on Attachment in Salesforce

Example and tutorial on creating the trigger for attachment in salesforce which will not allow to upload the file in opportunity if file contains some predefined text

Lots of time i have seen the question on salesforce developer forum regarding the trigger on Attachment. As this is not a straightway, still we can have trigger on Attachment.

We cannot create trigger on Attachment from the standard salesforce UI however we will have to use the eclipse.

Before insert Trigger on Attachment using eclipse in Salesforce
Before insert Trigger on Attachment using eclipse in Salesforce

While creating the trigger, we have to keep some important points. We should be very careful while writing the trigger on Attachment, as it will be used by any standard or custom Object and therefore the criteria to run trigger for “particular object only” must be handled carefully. Continue reading “Creating Trigger on Attachment in Salesforce”

Dynamic Approval Process in Salesforce using Apex and Trigger

This article explain the Automatic submission of Approval process using Apex and trigger. It include Automatic submission, approval as well as rejection of record completely using Apex and trigger.

Although this is very common approach and lots of articles are around on this topic, still I want to delineate the topic in other way. This topic covers complete scenarios for the approval process based on the Apex class.

Agenda of this article:

  1. Automatically submit the record for approval on the basis of field value.
  2. Automatically select the next Approver.
  3. Approve / Reject the record on the basis of field.

Assumptions:

  • Opportunity Object is used.
  • Approval Process is already set on the Opportunity.
  • Field “Next_Approver” will decide that who is going to approve the record.
  • There are three steps in the approval process.
  • There is no test class written and no check for mandatory fields needed for the trigger, as I have considered positive scenarios only.

Important URLS:

API of Approval Process classes:

  1. Apex process
  2. Apex ProcessRequest
  3. Apex_ProcessResult
  4. Apex_ProcessSubmitRequest
  5. Apex_ProcessWorkitemRequest

Steps of Standard approval process defined:

Approval Process Steps
Approval Process Steps

Continue reading “Dynamic Approval Process in Salesforce using Apex and Trigger”

Getting record from other Salesforce organization OR communication between multiple salesforce organization

Calling Rest Web service using Apex – Getting record from other Salesforce organization or Account or communication between multiple salesforce organization

In this article, I will explain the code which can be used for connecting and getting the records from different or multiple salesforce organization using Apex and REST Service.

Note : Here is new article to connect two salesforce instances in just 5 lines of code using Named Credentials.

To start first we will need to authorize below two URL which can be accessed from salesforce environment.
This can be done from “Setup | Administration Setup | Security Controls | Remote Site Settings”

  1. https://www.salesforce.com
  2. https://ap1-api.salesforce.com

Salesforce Remote Site Setting
Salesforce Remote Site Setting

Continue reading “Getting record from other Salesforce organization OR communication between multiple salesforce organization”

Email Services in Salesforce with simple example

Author posted by Jitendra on Posted on under category Categories Apex, Configuration, Force.com, Salesforce and tagged as Tags , with 38 Comments on Email Services in Salesforce with simple example

Complete tutorial of Email services in Salesforce with simple step by step example

What is an Email service in Salesforce?

Email services are automated processes that use Apex classes to process the contents, headers, and attachments of inbound email.

You can associate each email service with one or more Salesforce-generated email addresses to which users can send messages for processing.
The general template to create the apex class for the email services is:

How Email Services works in Salesforce
How Email Services works in Salesforce

Continue reading “Email Services in Salesforce with simple example”

Latest Salesforce Interview Questions – Part 4 – Related to Dynamic Apex

Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC

This part of the interview question mainly focus on the dynamic Apex feature of the salesforce.com .

30 : What is the dynamic Apex?
Ans :

Dynamic Apex enables developers to create more flexible applications by providing them with the ability to “Access sObject and field describe information”, “Write Dynamic SOQL Queries”, “Write Dynamic SOSL Queries” and “Dynamic DML”.


31 : How to get the list of all available sobject in salesforce database using Apex (Dynamic Apex)?
Ans:

Continue reading “Latest Salesforce Interview Questions – Part 4 – Related to Dynamic Apex”

Getting started with Apex debug logs in salesforce

Introduction to debug logs in salesforce with step by step procedure and screenshot, Difference between Debug log and System log in Salesforce

For the newbie and intermediate developers, most of the time it becomes very necessary to log the program at some stage / line and look into those that what actually is happening with the code.

In salesforce it is very easy to log and track the apex program. To log the apex code use below line of code:

System.debug(“Your message”);
or
System.debug(Logginglevel,”Your Message”);

Log levels available in Apex are (listed from lowest to highest) :

  • ERROR
  • WARN
  • INFO
  • DEBUG
  • FINE
  • FINER
  • FINEST

Continue reading “Getting started with Apex debug logs in salesforce”