Month: May 2015

Implementing Singleton Design Pattern in Apex – Use Case I

This post is part of the series – Design Pattern in ApexSingleton Design Pattern is pretty popular and one of the simplest design pattern available. I will start with the statement from “The Gang of Four” as it describes Singleton Pattern as  –“Ensure a class has only one instance, and provide a global point of access to it.”In this post, I will explain why we need to have Singleton Design Pattern in Apex and how to implement the same.Sometimes it is very much required to have only instances of the class – Logger, Window Manager etc. Typically, these type of objects where only one instance is sufficient to handle the requirements, are called Singleton.With Singleton Design Pattern, you can –Ensure that only one instance of class is getting created.Provide a global point of access to the object.There are many ways to implement Singleton Pattern in apex. Let’s start with few case scenarios:Use Case:Consider the below requirement -Whenever a new account is getting inserted to Salesforce, it should be synced with MDM(Master Data Management) through integration. But if MDM system is down, then Salesforce should not got for synchronization.Let’s start implementing the requirement -Custom Settings – Integration – This will be used to identify whether MDM System is up and running. If MDM System is down, Admin will uncheck the value and then Salesforce will not try for synchronization. Below is how...

Read More

How to handle DUPLICATE_USERNAME error while performing deployment

Recently, while doing deployment through change set, I faced an issue -System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.Another user has already selected this username.Please select another.: [Username]”The username used in test classes were definitely unique, but still my test classes were failing with the below error message. After spending some quite amount of time, I have understood the problem. So thought of putting this in my blog hoping that it may help someone in future.So here it goes -Error message -System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.Another user has already selected this username.Please select another.: [Username]”Reason -Usernames are always shared across instances, but not across environments. It means if a user is having username as test@test.com in any sandbox, then this same username you can’t use in any other sandbox instances (CS*), but you can user the same username in production sandboxes (NA*, EU*, AP*). Uniqueness is always checked during deployment when tests are run. So an insert from test class will also fail if the username is already registered in another org in the same environments. So we need to make sure the uniqueness in maintained in test classes.Resolution -To be 100% sure that the username used in test classes are unique, you should follow the below approach to define the username –    String orgId =...

Read More

“The Power of One” in Salesforce Formula Field

Most administrators think formula field is a way to do calculation in the pages such as finding out the final price after deduction, calculate date operation etc. But in Salesforce, formula field can be utilised to do more powerful stuffs. In today’s post, I will explain the “The Power of One” in Salesforce Formula Field.Business Case -Company “Universal Bank” is using Salesforce’s out of the box contact and activity standard object to track the each contact’s activities. Now a contact can have multiple activities. So the requirement is to run a report which will tell the total number of distinct contacts having some activities. Implementation -Now if you run a report, you will get something like this below -Now if you see the report is showing grand total as 12 as we have many contacts having more than one activities. But our requirement is to show total number of distinct contacts having some activities.And here comes “The Power of One”. This will help us to achieve our requirement. First we need to create a formula field in Contact object as shown below -Point to be noted – the value of the formula is field is 1 and that is why the name is “The Power of One”. Cool.Now add this field in the report and do Summarize by this field using Sum option. Picture is shown below.One done, save and run...

Read More

Importance of Equals and Hashcode in Apex

Recently while doing code review, I found that all developers should have fundamental understanding of “Why we should implement always Equals and Hashcode in Apex?”In this post, I am going to share my knowledge with few examples to understand the importance of Equals and Hashcode in Apex.Apex provides you an option to define an equal method in your custom types. This Equals method along with Hashcode is must when you are going to use your custom types in Sets and as Map keys. I would recommend to make this approach (Implementation of Equals and Hashcode) as best practice. Equality operator (==) is going to use the Equals operator if it is defined.Why we need this Equals method?Let me start with one example to show you the problem if Equals method is not implemented. Below is the custom type I am having -Now below is the test class to compare two instances of MyCustomType. Both the instances of MyCustomType is having myField value set to 10. So my expectation is that when I will compare both the instances, they should return true. Let’s see what will happen. Below is the test class.But unfortunately, below is what you will get -You see our test class is failing. Do you know the reason? Let me tell you the reason. The reason for this comparison to fail is that I don’t have the Equals method in...

Read More

Salesforce Summer 15 New Feature || Now we can use Location and Distance Variables in SOQL and SOSL Queries

With Salesforce Summer’15 release, now we can use location and distance variable in SOQL and SOSL queries.To know more about location and distance variable, I will request to check Salesforce’s documentation @Location-Based SOQL QueriesNow let me explain this new feature with a small code snippet.In my account object, I have created one custom field of type Geolocation with name Head Office Location (API Name: Head_Office_Location__c).Below is the code snippet -As you can see in the above code what I am trying to find all the accounts whose head office is located within 1 miles from my current location. To achieve this, I have used my current location’s latitude and longitude as apex bind variable in SOQL query.The way DISTANCE and GEOLOCATION function work are given below -DISTANCE(mylocation1, mylocation2, ‘unit’)GEOLOCATION(latitude, longitude)Now after Summer’15, you can replace any of the parameters used in the above two functions by apex bind variable.So to me, it is really a good feature. What do you guys think? Please provide your feedback....

Read More

Earlier Posts

  • 2024 (27)
  • 2023 (47)
  • 2022 (25)
  • 2021 (35)
  • 2020 (41)
  • 2019 (13)
  • 2018 (16)
  • 2017 (14)
  • 2016 (18)
  • 2015 (74)
  • 2014 (20)