Sunday 28 August 2016

Delete Duplicate Records Having Same External Id #Salesforce

Hey everyone, if you find a situation where there is duplicate data having a unique field in common, and you want to delete all and let the unique record be in the org, then this below apex is for you, I am using external Id field which is not unique, which is common in duplicate records.

We have many tools to handle duplicates on creation but few to remove duplicates.

So the scenario is I have below data

Account Name         External Id
Chris x                      12345
Chris x                      12345
Chris x                      12345

After running the code we will have

Account Name       External Id
Chris x                    12345


Here is the below code, you can do tweeks according to you scenario

      //Collections that we are using  
      Map<String, List<Id>> mapsOfAccountOfAccount = new Map<String, List<Id>>();  
      List<Account> accountToDelete = new List<Account>();  
      //Looping through the accounts records which has external ID  
      for(Account ac : [Select Id, External_Salesforce_Id__c From Task Where External_Salesforce_Id__c != null]) {  
           //Creating a map with External as key and list of duplicate records in values  
           if(mapsOfAccount.containsKey(ac.External_Salesforce_Id__c)) {  
                mapsOfAccount.get(ac.External_Salesforce_Id__c).add(ts.Id);  
           } else {  
                mapsOfAccount.put(ac.External_Salesforce_Id__c, new Set<Id>{ac.Id});  
           }       
      }  
      //Here we are populating a list with those records that we want to delete  
      for(String ky : mapsOfAccount.keySet()) {  
           integer i=0;  
           for(Id ids : mapsOfAccount.get(ky)) {  
                if(i>0)     {  
                     //Create and instance of the records to delete and add to list  
                     accountToDelete.add(new Account(Id = ids));  
                }  
                i++;  
           }  
      }  
      delete accountToDelete;  


Hope it helps someone
Thank you

Tuesday 9 August 2016

Description and Resolution of System.SObjectException: Field is not writeable

Was working on the apex, and found this error, its a System exception, so you cannot handle it easily until you resolved it.

So basically this error comes when you are trying to re-assign the parent record on child object, in a master detail relationship.

For the resolution you can do "Allow  reparenting" is one of the resolution by which in the apex you can re-assign the parent field.

You can go to the child object and on the master-detail field, and check Allow reparenting.




Thank you.!!

Tuesday 2 August 2016

How to set up MavensMate for Sublime Text #Salesforce

Sublime Text is basically a cross platform text and source code editor which is build using Python Application Programming Interface. Just the way we extend Eclipse’s functionality using force.com IDE plugin, we can use Sublime Text with a plugin called MavensMate.

MavensMate is Sublime Text plugin for force.com developers, this plugin allows you to perform operations like compile apex classes, trigger, built visualforce pages, deploy apex test to server.

Steps to Install Sublime Text:
Sublime Text can be installed from the Sublime website. It is available for:
1.    Windows 64 bit
2.    Ubuntu 64 bit
3.    Ubuntu 32 bit

Once, you have downloaded the exe file, Runthe file to Install Sublime Text in your System.

Now, go to All Programs and open Sublime Text 3 and there you go!!!
You have successfully installed Sublime Text in your System.


Now, you can use this Sublime Text as a Text Editor to view your code or modify it. It comes with a lot of different language syntax which makes it easy for developers to view their code in a specific language and modify it.



Now that we have installed Sublime Text and played around a bit to know how it works, we could install MavensMate to make it more useful for us (Salesforce Developers).

Let me give you a small background on what is MavensMate.

If you are a developer you must be knowing about Eclipse,not the one in which one heavenly body shadows the other one, but the Eclipse Software, which is extensively used in developing applications. Now, if you are a Salesforce Developer, you must be knowing the so called Force.com IDE plugin, which makes our Eclipse Force.com Platform specific. It helps us connect to our Salesforce Environment using Username and Password. After which we can push/pull changes to/from our Salesforce Instance.

You could compare Eclipse with Sublime Text and MavensMate with Force.com IDE.MavensMate is the open source alternative to the Force.com IDE and is compatible with Sublime Text.

Lets go ahead and install MavensMate for our Sublime Text.

First of all, open your Sublime Text editor and go to Preferences and search for Package Control.

If you could not find Package Control under Preferences, you would have to install it manually.

Steps to install Package Control :
1.    Go to View - Show Console
2.    Paste the appropriate Python code in the console. Click here for the link to the code
3.    Click on Enter

Once the code runs, again go back to Preferencesand search for Package Control



Steps to install MavensMate Plugin

1.    Go to Preferences - Package Control
2.    Choose Package Control: Install Package from the drop down 


3.    Search for MavensMate in the Search Box.


4.    Clicking on MavensMate would automatically run a Python script in the console and install the plugin.
5.    Once you have installed MavensMate, it is always good to Restart you Sublime Text.

6.    You could find the plugin access as a tab in your Sublime Text editor by the nameMavensMate. 


7.    Click on MavensMate - MavensMate API (mm) - Update MavensMate API (mm). This would run a python script to update and ensure, that your MavensMate plugin is up to date.


WHOLLAAA !!!! You have successfully installed MavensMate for your Sublime Text.

Additional Step (Required) Create your Project Workspace

To create a workspace for your projects,
1.    Create a Workspace folder in your local system and give it an appropriate name, say “MavensMate_Workspace” (This would be the folder where all your MavensMate project would be saved)
2.    Copy the folder path, we would need it in a while.
3.    Open Sublime Text - go to MavensMate - Settings - User
      


4.    Search for "mm_workspace" and paste the workspace folder path you copied earlier.
Note: use conventional Front Slash (\) in your path (for Windows). In case you are using any other operating system, you probably would want to use Back Slash (/)in your path.


Now that we have created a workspace for our MavensMate projects, lets go ahead and create our first Project.

Steps to Create New MavensMate Project:

1.    Open Sublime Text - Click on MavensMate - Project - New Project. (Make sure your Internet is working).
       

       
      2.    Clicking on New Project would take you to  a user interface where you can enter your Salesforce Instance Credentials (For which you want to create the Project).

3.    Enter the appropriate credentials - Choose the Salesforce Environment - Click onAdvanced to choose the Metadata which you want to have in your Project




4.    You can multi select the Metadata.
    
5.    Click on Refresh


6.    Select the Metadata components and click on Create Project


CONGRATULATIONS !!!! You have successfully created you first MavensMate Project in Sublime Text.

Some Cool Tips and Tricks in Sublime Text

Sublime Text comes with some cool tips and tricks to make User Experience rich, sleek and easy.
I would show you here a small example of one such trick.

Imagine a situation, where you have pasted some code in your text editor and working on it. While performing the modifications, you realise there is a particular variable name or a method name (OR any text for that matter) needs modification in some part of the pasted code / or the whole code. What would you generally do: Change the text at one place, copy it and then keep on find the former text at other places and change it with the later text (the one you copied).
Time consuming right!!!
Sublime Text makes it easier for you. You could double click on the text to be replaced at one place (preferably, the first time it has occurred), then keep on adding other instances using Ctrl + D, you could skip the current instance using Ctrl + K, Ctrl + D. Also you could deselect the current instance using Ctrl + U.
<picture>
There is a lot more to it.
Doesn’t it sound interesting !!! Try it out, it is fun!!!

HAPPY CODING!!!!

References:

Thursday 21 July 2016

Trailhead Superbadges for Salesforce Super heroes

Trailhead is only place where any one can go learn and play, I have seen many people who are good with salesforce, they find it so easy, trailhead has things for everyone for beginner, for intermediate and for experts, and this time to show off your expertise to everyone Trailhead has launched Superbadges


These badges are not so easy to get, and I loved spending time on trailhead for super badges.

There are four new Superbadges as of now and more are coming

First one is Apex Specialist, here you have to show yours skills of Apex like callouts, test classes etc. This badge will take around 8 hours, you will get to practice scheduling of apex class and writing test class for that.Its a great learning badge for the intermediate apex developers.



And the second one is Lightning experience, it is one of the best, you will get the lightning experience to get the hands on. Please try this one, you will feel good after adding this in your trailhead badge list.


Third one is for Admin Champions of Salesforce, you can practice your report and Dashboard skills over this badge.


And the last but not the least is for Salesforce Security



Sunday 10 April 2016

#Salesforce Certified Platform Developer II : Sharing Experience

Hi All,
After long time of 5 months I went for another certification and this one is a new one, didn't get allot of information about the experience of the this new salesforce certification, now I have 8 salesforce certs, I must say this one is made me nervous, but felt great when I cleared this one.



Here are few important things that should be looked into before appearing in the exam

Compound Field Considerations and Limitations

Address and geolocation compound fields are convenient and result in more concise, clear code. Here are some things to consider when using them in your apps.
Both address and geolocation compound fields have the following limitations.
  • Compound fields are read-only. To update field values, modify the individual field components.
  • Compound fields are accessible only through the SOAP and REST APIs. The compound versions of fields aren’t accessible anywhere in the Salesforce user interface.
  • Although compound fields can be queried with the Location and Address Apex classes, they’re editable only as components of the actual field. Read and set geolocation field components by appending “__latitude__s” or “__longitude__s” to the field name, instead of the usual “__c.”
Use convertCurrency() in a SOQL query to convert currency fields to the user’s currency. This action requires that the organization is multicurrency-enabled.
Use this syntax for the RETURNING clause:
convertCurrency(Amount)
For example,
FIND {test} RETURNING Opportunity(Name, convertCurrency(Amount))

If an org has enabled advanced currency management, dated exchange rates are used when converting currency fields on opportunities, opportunity line items, and opportunity history. With advanced currency management, convertCurrencyuses the conversion rate that corresponds to a given field (for example, CloseDate on opportunities). When advanced currency management isn’t enabled, the most recent conversion date entered is used.

Continuation :Make Long-Running Callouts from a Visualforce Page

Use asynchronous callouts to make long-running requests from a Visualforce page to an external Web service and process responses in callback methods. Asynchronous callouts that are made from a Visualforce page don’t count toward theApex limit of 10 synchronous requests that last longer than five seconds. As a result, you can make more long-running callouts and you can integrate your Visualforce pages with complex back-end assets.

An asynchronous callout is a callout that is made from a Visualforce page for which the response is returned through a callback method. An asynchronous callout is also referred to as a continuation.


Transaction Control

All requests are delimited by the trigger, class method, Web Service, Visualforce page or anonymous block that executes the Apex code. If the entire request completes successfully, all changes are committed to the database. For example, suppose a Visualforce page called an Apex controller, which in turn called an additional Apex class. Only when all theApex code has finished running and the Visualforce page has finished running, are the changes committed to the database. If the request does not complete successfully, all database changes are rolled back.
Sometimes during the processing of records, your business rules require that partial work (already executed DML statements) be “rolled back” so that the processing can continue in another direction. Apex gives you the ability to generate a savepoint, that is, a point in the request that specifies the state of the database at that time. Any DML statement that occurs after the savepoint can be discarded, and the database can be restored to the same condition it was in at the time you generated the savepoint.

DML Statements vs. Database Class Methods

Apex offers two ways to perform DML operations: using DML statements or Database class methods. This provides flexibility in how you perform data operations. DML statements are more straightforward to use and result in exceptions that you can handle in your code. 


Set Up Test Data for a Test Class

Test setup methods are defined in a test class, take no arguments, and return no value. The following is the syntax of a test setup method.
@testSetup static void methodName() {
}
Use test setup methods (methods that are annotated with @testSetup) to create test records once and then access them in any test method in the test class. Test setup methods are useful and can be time-saving when you need to create a common set of records that all test methods operate on or prerequisite data.

Best Practices for Improving Visualforce Performance

Defining Transient with variable and minimizing the view state, using lazy loading and  the minification of JavaScript and CSS,

InvocableMethod Annotation

Use the InvocableMethod annotation to identify methods that can be run as invocable actions.
Invocable methods are called with the REST API and used to invoke a single Apex method. Invocable methods have dynamic input and output values and support describe calls.
The following code sample shows an invocable method with primitive data types.

 public class AccountQueryAction {  
  @InvocableMethod(label='Get Account Names' description='Returns the list of account names corresponding to the specified account IDs.')  
  public static List<String> getAccountNames(List<ID> ids) {  
   List<String> accountNames = new List<String>();  
   List<Account> accounts = [SELECT Name FROM Account WHERE Id in :ids];  
   for (Account account : accounts) {  
    accountNames.add(account.Name);  
   }  
   return accountNames;  
  }  
 }  



Pagination Strategies 

When to use standardsetcontroller, or when to offset and SOQL, best that you can have about this topic is this post

Few more topics

1. Lightning components
2. Using Developer console.
3. Test class best practice
4. SOQL and SOSL
5. Apex charts
6. Rollup summary
7. Http callout and its testing

If you cover all these topics, then for sure you can clear this exam.
thanks