All about Upsert and External ID in Dataloader and Apex – Videos

You may be wondering that there are tons of articles available for upsert operation and what is need of one more blog post. I wanted to note everything important about upsert operations using datalaoder and Apex with identified gotchas at single place, and that’s why its here ūüôā .

External ID is field in Salesforce to say that this field is primary key in external Database which can be used to identify that external record exists in Salesforce or not ? There are many benefits of external key like :

  • It can be used to identify if record exists or not and record automatically inserted or updated using upsert operation
  • Upsert operation is supported by Dataloader, Apex and API
  • Any field marked as external id is indexed and can be used in SOQL or report filter
  • You can create Parent child record in¬†single statement in Apex

Using Upsert operation to create/update child record and relate to existing parent record with help of External Id

Lets assume that you have list of contacts and Account in external SQL Server database and you want to import those Account and¬†contact weekly. It is possible that you have already imported contacts previously¬†so don’t want to duplicate records¬†if already created in Salesforce. To solve this problem we can use external Id on Contact and use upsert operation on dataloader. Let’s assume external Id field on Contact is “SQL_Server__Id__c” (I am assuming that Account records in Salesforce also have external Id and records are already imported. Same below¬†steps can be used to import Account record as well).

Apex based record sharing in Salesforce

Last update : May 4 2020

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.

Before you proceed, few points to note about Apex based sharing

  • Share table available only when Organization Wide Default [OWD] sharing is not Public Read Write
  • Apex sharing reasons can be created only for custom objects
  • As Apex Sharing Reason not available for Standard objects only way to create Apex based sharing for Standard objects are using row cause Manual
  • As row cause is Manual for Standard objects, Apex based sharing would be lost once owner is changed [Because its behavior of Manual Sharing]
  • In case of custom object & custom Apex Sharing Reason, sharing would not be lost when owner changed
  • Read more in detail from official documentation

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”¬Ě.

Salesforce Toolkit for PHP

In a web development PHP has prooved itself as one of the major contributor in the world. There are lots of websites which are built over the PHP and the best example is facebook. PHP is very powerfull, easy to learn and the best thing is that it is open source.

Following the popularity of PHP, salesforce has also started providing support to PHP and delivered the Toolkit for it. Using this toolkit we can connect with salesforce and perform all API operations supported like Insert, update, delete, retrieve etc.

Download Salesforce PHP Toolkit Example – Zip

Salesforce Apex Code talk – Important Questions and Answers

Hello everyone,
On 24th April 2012, There was Apex code chat conducted by the Salesforce. So i thought to share important question and answers of that chat. You can read complete chat here.

Here are some of the (edited) highlights from this episode’s chat transcript.

Question: Is there a way in Apex to send an email and replace the default from email address (
Ans: Yes, using either the setSenderDisplayName or setOrgWideEmailAddressID methods on the SingleEmailMessage or MassEmailMessage classes.

Question: Which fields are accessible in a record without performing a SOQL query? For example if my trigger is on the Contact object, are fields on the parent Account object (like Account Description) accessible without performing an explicit SOQL query?
Ans:¬†You can use to access all non-null fields of the object on which the trigger is defined. You have to perform an explicit SOQL query to access any fields on associated parent or child records. For example, if the trigger is defined on Contact, you can only access the Id of the parent Account record (via AccountId) in You’d need to perform a separate SOQL query to access any other field on the parent Acccount record (e.g. Description, Industry etc.).

Dynamic Approval Process in Salesforce 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.


  • 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

Salesforce Interview Questions – Part 5

40. What is Master Detail relationship and look up relationship in Salesforce?
Master Detail relationship is the Parent child relationship. In which Master represents Parent and detail represents Child. If Parent is deleted then Child also gets deleted. Rollup summary fields can only be created on Master records which will calculate the SUM, AVG, MIN of the Child records.
Look up relationship is something like “has-a” (Containership) relationship. Where one record has reference to other records. When one record is deleted then there is no impact on other records.

41. Can we convert the lookup relationship to Master Detail relationship?
We can convert the lookup relationship to master detail relationship if and only if all the existing record has valid lookup field.

42. In How many way we can invoke the Apex class?

  1. Visualforce page
  2. Trigger
  3. Web Services
  4. Email Services

Getting record from other Salesforce organization 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”¬Ě


Salesforce Remote Site Setting
Salesforce Remote Site Setting

Email Services in Salesforce with simple 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