Posts Tagged: force.com


11
May 11

Thoughts on auto-number names

Good stuff is coming for the Summer ’11 release of the Force.com platform! My personal favorites: dynamic components and javascript remoting, these 2 features will save a lot of headaches when working with Visualforce and I’m already thinking of several pages that I could optimize by using them.

Anyway, every new release comes with very interesting features but there’s still a feature that I’d wish Salesforce could improve and it has to do with auto-number names for records. Why? Well, because these records are hard to find. There are cases where you definitely don’t need to setup a special name for records and any identifier will work but I’ve come across several cases in which users need to select a record that has an auto-number name through a lookup dialog and always end up with complaints about the search functionality for these records. This could be solved by telling users to use a naming convention for the record and avoid auto-number names but it’s hard to assure naming conventions will be followed and since the name field is a required field you cannot set it through a trigger or workflow unless you make some workarounds with Visualforce.

These situations usually happen when we have an object in which the record name is hard to define since it may describe several things  and these  usually end up being records of junction objects. For example, let’s look at the following situation with the well-know Recruiting app.

 

 

We have a junction object called Job Application and we name it JA-{00000}, everything works fine at first but as our application continues to grow we might end up creating an object such as “Interview” that we must link to a “Job Application”. If you wanted to create an Interview record directly from the “Interview” tab and link it to a Job App through the lookup dialog, well… you’ll need to know the auto-number name for the Job Application or hope it comes up in the recently viewed records, although Salesforce gives us the option to set the columns for the lookup dialogs there’s no easy way to search within the list for this type of records.

I think any of the following options could help a lot in this situation:

1.- Search by other fields within lookup dialogs, not just name fields

The good:

Minimum impact and it would involve a small amount of configuration tasks since we would probably just need to set some of the fields as external ids to create indexes so that we can search on these.

The bad:

There might be some cases where we might need to create workflow rules for concatenation purposes.

We won’t be able to display more info of the record in the auto-number name.

2.- Give the option for removing the required option from the name field

The good:

Gives us the flexibility to set the name through workflows or apex triggers (for more complex situations).

This could be useful for objects in which you might want to create a name depending on the record type, the name field could be hidden from a specific layout and then filled in the background through workflows or triggers.

The bad:

If no name is defined, we might end up with records having the Id of the record as a name and that is a bad thing.

3.- Define an auto-number as a formula field

The good:

Gives us a lot of flexibility for setting up the record name without using any workflow or apex trigger.

The bad:

Probably hard for the platform since it would need to construct an index based on a formula field and I guess that if this is possible it could hurt in performance.

In my opinion, option 1 is probably the best since it seems like the one that has less impact but I wish the platform could offer the flexibility of option 2. I’d  appreciate any feedback on this from everyone out there, there are already  some ideas related to this subject so please vote for them so that we can see this solved in a future release.

Here’s a list of some of the ideas related to auto-number fields:

Saludos!

4
Feb 10

Force.com Apex Merge/Replace Class ;)

Hello again!

I just developed an Apex class for performing replace/merge operations on any kind of object, and yes any kind of relations as well! , so it doesn’t matter if you have master-detail relationships that can’t be modified, objects tied in a master-detail relationship will be cloned, deleted and inserted with the new values to simulate a merge operation!

To use the Merge class  just create an ObjectMerge instance, passing as an argument of the constructor your current record:

ObjectMerge oMerge = new ObjectMerge(SourceObject);

And to peform the merge operation just use the replaceRecord method and thats it!, you can specify if you want to delete the source record after the merge operation is done.

oMerge.replaceRecord(MergeToThisObjectID,DeleteSourceObject);

You can limit the number of records that will be processed in the merge operation for Master-Detail relations  to avoid hitting governor limits. The ObjectMerge class will let you know if there are any objects left, and if this is the case just continue performing the merge operation.

Force.com provides a merge DML operation but it’s only available for some of their standard objects, this class can help you get around that. There are still some limitations with this class due to governor limits in the number of describe calls but unless you have a large amount of objects related to the replaced/merged record it should work pretty well.

For now, the Object Merge class only merges/replaces custom objects related to your record, but this can be changed  in the class , however keep in mind that no more than 10 relations can be processed due to describe calls limitations so be careful when removing the custom objects limitation because there can be many Non-Custom objects related to your record.

I’ll leave a link in this post to the files but for now here’s an example of how to use the ObjectMerge class in a VisualForce controller that merges contacts, if you have custom objects tied a contact A and want to merge contact A to Contact B you will see how the ObjectMerge class changes all of the objects from contact A to contact B then deletes contact A. Remember, you can try it with any custom object ;)   Enjoy!

Download the files from here –> ObjectMerge

public class MergeController{
    
    //This is just an Aux Object to get a Lookup Field in the VisualForce Page
    public Contact MergeAux{get;set;}
    //Declare an instance of the Object Merge Class
    private ObjectMerge oMerge;
    //Save the current contact in the controller (optional)
    private final Contact currentContact;
        
    public MergeController(ApexPages.StandardController controller){

        //Initialize all of our objects
        currentContact = (Contact)controller.getRecord();    
        MergeAux =new Contact();
        oMerge = new ObjectMerge(currentContact);        
        
    }
    
    //Merge Action
    public PageReference doMerge(){

        try{
     
            //Run the merge operation!, you can specify a second argument to delete the record after merge or not
            oMerge.replaceRecord(MergeAux.ReportsToId,false);
            
            //Do corresponding actions if merge operation was successful
            if(oMerge.bAppliedReplace){
                PageReference ref = new PageReference('/'+MergeAux.ReportsToId);
                ref.setRedirect(true);
                return ref;
            }else{
                return null;


        }catch(Exception e){
            
            ApexPages.addMessages(e);
            return null;    
        }
        
    }   

}

//And the Visual Force page will look something like this:

<apex:page standardController="Contact" extensions="MergeController">
     
     <apex:form >
         <apex:pageBlock >
             <apex:pageMessages id="messages">
             </apex:pageMessages>
             
             <apex:pageBlockButtons >
                 <apex:commandButton value="Merge" action="{!doMerge}" rerender="messages"/>
             </apex:pageBlockButtons>
             <apex:pageBlockSection columns="1">
                 <apex:outputField value="{!Contact.Name}"/>
                 <apex:inputField value="{!MergeAux.ReportsToId}"/>                
                 
             </apex:pageBlockSection>
         </apex:pageBlock>
     </apex:form>
     
</apex:page>

(NOTE: There’s a bug with the code viewer plug-in, please ignore the second viewer)


25
Jan 10

Interesting ways of architecting Apex Triggers

Hello everyone!

I came across a blog post at gokubi.com (http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers) about some interesting ways of architecting Apex Triggers.

We have developed a lot of logic in Apex Triggers and ended with some really nasty code in some scenarios; this post got me thinking about new ways to architect triggers using a more object-oriented approach. So just to follow the thoughts in this post I’ll show you what I did to organize our triggers and it has been working pretty good so far.

NOTE: Please refer to the code section below

//Step 1

I started by creating an Apex Class to replicate some of the triggers functionality…

//Step 2

The next step will be to start coding our Apex Class that will perform all of the logic in our triggers, so let’s say the we are building a trigger for My_Object__c custom object.

//Step 3

And finally, we just need to create the Apex Triggers that will just initialize our Trigger class and initialize an instance of the corresponding Apex Class that will handle all of the logic.

And that’s it! Just remeber to cast the the collections correctly before accessing the collections from the ExtTrigger class because we can’t work with generic sobject collections.

Some of the benefits that I’ve seen by organizing Apex Triggers in this way are the following:

  • 100% Test Coverage in Apex Triggers (although all of the Test Coverage should be done in the handler Apex Class)
  • Since we can call functions to perform specific actions in Apex Classes, we end up with more organized code, it’s easier to read in the sense that you can just go to see the calls inside the “onBeforeInsert” section for example..
  • Avoid the max number of characters governor limit:
    • Maximum number of characters for a class: 100,000
    • Maximum number of characters for a trigger: 32,000

Thanks to everyone that posted in gokubi’s blog post any feedback on this is appreciated :)


17
Jan 10

Force.com Spring ’10 Release

Salesforce’s Spring ’10 is packed with some really nice enhancements, new interface, code scheduler, no limits in collections, and more!

For more information go to: http://developer.force.com/releases/release/Spring10

Here’s a small demo of how code scheduler works:


27
Jul 09

Force.com platform as a service

For the last 4-5 months i’ve been working with the force.com platform, more specifically with the on-demand CRM salesforce.com. In these few months working in this platform, i’ve seen some major improvements that i think will establish force.com as a major platform for the development of web applications, among these: Visualforce, Web-services (which have been around for some time), integration with Google and APIs provided for other frameworks such as Adobe Flex/Air.

This platform as a service concept is new for me and I think it offers great advantages in matters of development & deployment time,acquisition and maintenance costs of hardware & software. And now, with Visualforce, it gives developers the ability to follow the Model-View-Controller Design Pattern in order to have data models, business logic and user interface in different layers.

I’ll post more information about how to work on each of these layers and maybe show some code of examples that i come to write in this platform.

For more info, check out salesforce.com


23
Apr 09

Autocomplete component

Hello everyone, just wanted to share with the community this custom component I made and give something back for the help I’ve received :)

The purpose of the component is to enable autocomplete in lookup fields. I used the autocomplete js created by Jim Roos:
(http://www.jimroos.com/2007/05/ajax-autocomplete.html) but made some modifications to it so that it could interact with an Apex controller among some other things…

So my idea was that if you were making a VF page that had an inputfield that was related to a lookupfield you would just insert this autocomplete component to that inputfield.

Something like this:

<apex:inputField value=”{!Contact.accountid}” id=”accname” styleClass=”cField”>

<c:autocomplete ObjectName=”Accounts” InputId=”{!$Component.accname}”

AutoCompleteId=”accACid” ClassName=”autocomplete300″/>

</apex:inputField>

The component has 4 parameters:

The name of the object or custom object that the inputfield relates to (new objects must be added inside the apex classes since i had some problems constructing a dynamic query).
The InputId which is used to relate the component to the input field
The id for the Component
A classname parameter that basically just defines the width of the suggestions menu.

Here’s a screenshot of how it looks like in action:


Here’s a link to the file containing the required files:

AutoCompleteComponent