Showing posts with label apex. Show all posts
Showing posts with label apex. Show all posts

Saturday 26 July 2014

Call Apex Class from Custom Button (Javascript) in Salesforce

05:34


Most of the times, we end up needing to do more complex logic, but from the simplicity of the custom button on the page layout. This post aims at giving you a running start to this.

Lets get straight into it, by this we're assuming you know how to





Sunday 17 November 2013

Best Practices for writing Apex Test classes in Salesforce

15:26


Here are some best practices that I felt need to be followed while writing test classes in Salesforce , Do add your thoughts too


Wednesday 9 October 2013

Describe all the fields for all SObjects

13:58
In one of the requirements I came across, I needed to get the list of all the fields across all objects. I wanted to make sure that I did in the best possible way, But couldn't find any straight answers. So here's the code I came up with. Its simple and straight-forward and if you find a better way to do this let me know in the comments. Thank you

To start off, we need the list of SObjects that we are going to run this for. Ideally we should have the list of SObjects stored in a Custom setting, this is a one time thing and could be done using anonymous apex execution. Following is the code to do just that..

List<string> SObjectList = new List<string>();

for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){
   String name = objTyp.getDescribe().getName();
   // Exclude all the unwanted Sobjects e.g. History, Share etc..

 if(!name.containsignorecase('history') && !name.containsignorecase('tag')&&
    !name.containsignorecase('share') && !name.containsignorecase('feed')){      
      SobjectList.add(name);
  }
   // Insert into your custom settings
  }
Now that we've got the entire list of SObjects, lets do a Global Describe. Before we move on there is a gotcha here, as you know there is a governor limit on the number of Describes in an execution context (As of Winter '14 its 100 in both asynchronous and Synchronous) . I'll go ahead and suggest that its best if you could either do it in multiple executions or use a limit to control the number of SObjects processed.

 
      SObjectFieldsMap = new Map<string,Set<string>> ();
      Map<String, Schema.SObjectType> GlobalDescribe = new Map<String, Schema.SObjectType>();
      // Get the Global Describe which contains details for all objects
      GlobalDescribe =Schema.getGlobalDescribe();

      // Now we loop through our pre-compiled list of SObjects and get the describes for it
      for(String sObj:SObjectList)
      {
       // Populate the Map, with Sobject => list of fields
       if(SObjectFieldsMap.get(Sobj)==null && GlobalDescribe.get(sObj)!=null)
        SObjectFieldsMap.put(sObj,new Set<string>());

        if(SObjectFieldsMap.get(sObj)!=null) // Some Objects may not have Describes 
        SObjectFieldsMap.get(sObj).addAll(GlobalDescribe.get(sObj).getDescribe().fields.getMap().keyset());
      }
      
Well thats pretty much it. Hope this helps, let me know if you need anymore clarifications on this or if you've got some thoughts on this. Happy Coding!

Monday 30 September 2013

REGEX in Salesforce

13:00
What is a RegEx?

Regular Expression in computing terms is a pattern that is used to search. It has its own syntax and semantics. To learn more on what RegEx is visit --> here
Great now we learnt what REGEX is, to build your own regex visit --> buildregex.com

So how does all this play in salesforce apex ? There are numerous use cases for using regex and checking patterns e.g. Checking if string is in Email Format.

There are 2 Major Classes to check if regex matches. Pattern Class and Matcher Class
in short, the pattern class is used to store a type of regex and once you have a pattern stored, We use the Matcher Class to identify if a specified string matches the pattern.

Here's the code for Matching String to Email pattern In salesforce

public static Boolean checkEmailFormat(String email) {
        String emailRegEx = '[a-zA-Z0-9\\.\\!\\#\\$\\%\\&\\*\\/\\=\\?\\^\\_\\+\\-\\`\\{\\|\\}\\~\'._%+-]+@[a-zA-Z0-9\\-.-]+\\.[a-zA-Z]+';
        Pattern MyPattern = Pattern.compile(emailRegex);
        Matcher MyMatcher = MyPattern.matcher(email);
        Boolean result = MyMatcher.matches();
        return result;
    }

This is a self explanatory code, the Pattern is first compiled and stored, then we check the "email" parameter matches the regex, the result (true/false) is returned to the calling method. Wanna learn more ? --> Check SFDC Documentation

Hope this helped  ! Let me know your thoughts or if there are some neat regex that you'd like to share in the comments . Happy Coding !

Wednesday 11 September 2013

Email Case Team Members on Case Comment

14:40

This is going to be a short blog for a requirement I came across, But didn't find any blogs out there addressing this. So lets get right to it, I need to notify all the case team members when a case comment is logged, be it contact, users etc.


Here's what we'll need


  • Trigger on Case Comment 
  • A Helper Class (To keep the trigger clean)
  • Test Class ( Don't forget to test your code)
To make things look clean we'll keep most of the code to the helper class,

Saturday 11 May 2013

<apex:actionStatus>

03:10
Action status is usually used to show the status of an Ajax Process to which it is related to. It can be as simple as showing text during the update to as fancy as greying out the page and showing a loading gif. your imagination (and HTML CSS skills) are the limits

Wednesday 24 April 2013

How to activate Summer ’13 Preview on sandbox

10:18
Summer 13The salesforce.com Summer ‘13 release is quickly approaching and soon you'll be able to take advantage of exciting new features and functionality! If you are a Force.com Sandbox customer, you have the opportunity to get early access to Summer ‘13 in your Sandbox and test new customization and features (the chance to get your feet wet) before your production organization is upgraded.




Tuesday 23 April 2013

Escalate Question to Case

18:11

AnswersThis is a very useful need at times requested by clients.  “Escalate Question to Case” / “Create Case from a question” / “Convert Question to case” in either customer portal , Partner portal, or any self serve portal.

In the answers portal, when we are required to Escalate the question to a case so that the executives can handle it. Salesforce provides this functionality internally but its a little tricky to turn it on and in this post I want to address this.

Thursday 14 March 2013

Visualforce To Excel

02:11
In this short post I want to explain how we can generate an excel output from a VF page.  We already know to how to generate a PDF from VF page. (Read more to find out how). One of the common requirements is to convert a VF page with data into an Excel sheet, here are some things you may want to keep in mind
  1. Use a separate VF page for export. It is advisable to use a separate VF page to export off, reason being, usually the VF page in which data resides contains buttons, links images etc. and that is not something you usually want in your excel
  2. Use <apex:dataTable> : this gives you a consistent output and works for both windows and MAC. (Thank you Pratyush Kumar  for the Info)
  3. Proper validation : Before the export page is reached, make sure all validations to ensure data will be present is  a good practice

Wednesday 23 January 2013

Reload Standard Detail page from Inline VF

07:58
 
visualforceI just faced a small block, Pretty simple once you've figured it out, Thought of sharing it and saving time for us in the future. Most of the time we require to reload the Standard Page following an  action on the inline VF Page. The solution to this was not apparently available to me when I needed it. Usually after clicking on a link or button we need to refresh the standard (parent page) the VF page is in. Read more on how to add inline VF Pages

Problem: A VF page is inline on a standard detail Page, On click of button inside VF Page, Reload the standard Detail Page once action completes in the Inline VF page

Thursday 10 January 2013

How to write Batch Class in Salesforce.com Apex

10:00

batchApexIn order for us to write batch classes in apex, let us 1st understand what is batch class , when and where it should be used? This is an extensive blog going into details.
Main sections of this post are
  1. What is batch Apex
  2. When to use batch apex
  3. Advantages of using batch apex
  4. Batch Apex Governor limits
  5. Sample Code

Tuesday 8 January 2013

Spring ‘13 Features Release Preview

01:09

spring13Salesforce.com Spring ‘13 is a debated release where some say there are not many features being released like the usual salesforce releases while some say that Spring ‘13 packs really good and needed features. I’ll let you be the judge of that.
All of what I can say is , it is exciting as we are all waiting to see what is packed in this release and can’t wait to get our hands on it. Below are the videos salesforce released on their YouTube channel some time ago.
you can also find the release notes here : http://shivd.me/Spring13Release
The Spring 13 Release Preview of all features : http://shivd.me/Spring13ReleasePreview
and you could join us for the webinar here: http://shivd.me/Spring13Webinar
You could sign up for the Pre-Release ORG here : http://bit.ly/getspring13

Sunday 6 January 2013

Run Batch Class Hourly

16:34
This is a use case that we all come across very often, Schedule a batch class every hour to clean up data or to send out batch emails to case team members (Which I’ll blog about later).
There are three main steps involved in this
  1. Write a Batch class with the required logic
  2. Write a Scheduled Apex which calls the above Batch Class
  3. Schedule the class from the developer console by executing anonymous apex