Cloning Records in Apex

When you clone an sObject in Apex, it copies all the fields populated in that Apex object, not necessarily all fields on the record.  Let’s say you have a Lead with FirstName, LastName, Company,  LeadSource and Status populated.  If you do the following, the clone will not have LeadSource and Status cloned from the original record.  It will use the default values for Leads. That is because those fields were not queried into the object.

/* query lead and then clone it */
lead l = [select id, firstname, lastname, company from lead where id = '00Q3000000aKwVN' limit 1][0];
lead l2 = l.clone(false, true);
insert l2;

If you are cloning records, it can be frustrating to keep it updated as you add new fields to Salesforce. I generally want all new fields to be cloned too. I’ll code to any exceptions if needed. To help with this, I wrote myself a handy method to build me a SOQL statement and obtain all the writable fields.

public with sharing class Utils{ 

    // Returns a dynamic SOQL statement for the whole object, includes only creatable fields since we will be inserting a cloned result of this query
    public static string getCreatableFieldsSOQL(String objectName, String whereClause){
        
        String selects = '';
        
        if (whereClause == null || whereClause == ''){ return null; }
        
        // Get a map of field name and field token
        Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap();
        list<string> selectFields = new list<string>();
        
        if (fMap != null){
            for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)
                Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)
                if (fd.isCreateable()){ // field is creatable
                    selectFields.add(fd.getName());
                }
            }
        }
        
        if (!selectFields.isEmpty()){
            for (string s:selectFields){
                selects += s + ',';
            }
            if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));}
            
        }
        
        return 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE ' + whereClause;
        
    }

}

So if I want to clone the Lead I describe above, I’d do the following and this will ensure that I will clone all the fields on the Lead. Since the method only adds Creatable fields to the SOQL, I don’t have to worry about trying to set a formula field or system field and generating an error.

/* query lead and then clone it */
String soql = Utils.getCreatableFieldsSOQL('lead','id=\'00Q3000000aKwVN\'');
lead l = (Lead)Database.query(soql);
lead l2 = l.clone(false, true);
insert l2;

Comments (11) comments feed

Auto Run Radial Searches

Oftentimes, you want to integrate a specific map search into your business processes. For example, suppose your Inside Sales team is reviewing Leads and needs to notify your closest partner of the lead information. Geopointe is the tool to help you know who your closest partner is and this entire process becomes even easier when being creative with the available URL Parameters for the Map page.

See this example in action in the video below.

The image below is a copy of the button configuration.

Comments off comments feed

Static Maps in Visualforce

Geopointe includes a Visualforce Component that allows you to create Static Maps on your Visualforce Pages.  A Static Map results in an image file (png, jpg or gif) that you can configure and place on your Visualforce Pages or embedded in Page Layouts.  The nice thing about Static Maps is that they do not slow down your pages from loading.  Rather, you are using the component to intelligently build an image URL.

Geopointe comes with some examples with the “embeddedMap…” pages.  These are pre-configured Visualforce Pages using the static map component to show you the location of a record.

We added a Static Map page on the website to act as the official documentation home for this functionality.  With Geopointe installed, you also get a documentation in your Component Reference regarding the geopointe:staticMap component.

Below are some examples showing off the functionality.

<geopointe:StaticMap mapProvider=”goog” width=”500″ height=”300″ useIconLabels=”true” iconColor=”green” locationIDs=”a07A000000ATQjhIAH,a07A0000006O0qc,a07A0000006NKq2,Evanston|IL,OakPark|IL”/>

<geopointe:StaticMap mapProvider=”mq” width=”800″ height=”200″ useIconLabels=”true” iconColor=”purple” locationIDs=”a07A000000ATQjhIAH,a07A0000006O0qc,a07A0000006NKq2,Evanston|IL,OakPark|IL” mapType=”hyb”/>

Comments off comments feed

Want to help with Info Center?

The short story…

I’d like an independent developer to “co-own” the Info Center app with Arrowpointe.  This dev will help take it to the next level.  This dev will do this in the name of getting some exposure for themselves and also as a way to learn force.com technologies.  The result is a publicly facing deliverable.  Info Center will remain free.

The dev should consider this a side project (i.e. no pay) done in the interest of proving an app to the community and learning some things along the way.  If you are a developer and have wanted to get out there and do something on AppExchange, this is a good opportunity to get some experience for very little risk (you are only spending time).

The longer story…

Info Center is an app I created back in 2006.  To use it, an admin works in objects called Messages, FAQs and Links adding data for the purpose of communicating it to end users.  It was originally created for consulting projects I worked on as a place where the admin could answer FAQs for users as the deployment took off.  End users are given the “Info Center” tab, which renders all this data for them in a nice, easy to read format.

It has not changed in function or technology since 2006 and it could use an update.  For example, the Info Center tab below currently uses a S-Control to render it.

My focus is now on Geopointe and I have no time to work on Info Center.  I’ve been meaning to update it for a while now, but never got around to it.  Therefore, I wanted to see if anyone in the community (ideally an independent developer) wants to take on Info Center 2.0 and we’ll see what comes of it.

It still gets 10-20 installs per month and will provide a good way for someone trying to “make it” in the force.com world to gain some credibility. May as well help with an app that has a bit of momentum already, right?

Info Center was a project I assigned myself in 2006 in order to learn s-Controls and the API.  Having my deliverable be public facing went a long way towards making it polished, teaching me about the AppExchange and the nuances of delivering a app for others to use. I’d like to pass that experience along to someone else.

There is no timeline for this.  I am first looking for the right person who’d like to take this on.  If it takes months to complete, that’s fine.  As long as the person is committed to the task at hand.

At its simplest, I’d at least like to have the Info Center tab render using Visualforce.  I could envision a mix of Visualforce, Apex and jQuery being the technologies used. If delivering these technologies in an AppExchange app appeals to you, consider taking this on.

If interested, comment on this post and we’ll take it from there.  Please only comment if you are genuinely interested and feel you’d be able to see it through to the finish.  If you want to contact me privately, you can do so here.

Comments (2) comments feed

Using a colon in the Report Name

I posted this in the Dreamforce Chatter app and it got a nice response, so I thought I’d blog about it here too.

It’s subtle tip for you perfectionists out there that makes reports look just a tad more professional. If you add a colon (e.g. Orders: Aging) to your report name, it will display the report name on 2 separate lines when viewing the report. Use this to create a naming convention for your reports like (category: detailed description) or for long report names so they display nicely.

I don’t think this is an actual, documented feature from Salesforce, but it’s been this way for years.

Look at the difference between naming a report “Orders – Aging” and “Orders: Aging”. It’s a subtle difference, but it’s something I use regularly and thought I’d pass along.

Comments (6) comments feed

Next entries » · « Previous entries