AddThis Feed Button

Recent Items

I was configuring DataLoader to export a list of Opportunities, and I went to select the “Owner Name”. However, only OwnerID is available on an Opportunity.

“No problem!” I think to myself, as I go and create a Custom Field with a formula equal to Owner.Alias.

“What?” I say in surprise. “It won’t let me access a field on the Owner object!”

NoLink

Mmm. This is strange. Then a Google Search reveals an 18-month old Ideas request to Make “Owner Id” Look up fields available for formulas.

Oh dear.

Well, that’s a shame, but it’s easily solved! I created:

  • A field called Owner_Link__c of type Lookup(User)
  • A Trigger to copy OwnerId to Owner_Link__c when the Owner is changed
  • A test for the Trigger

Trigger:

trigger Update_OwnerLink_on_Owner_Update on Opportunity (before update, before insert) {

  // When 'Owner' field is changed, update 'OwnerLink' too

	// Loop through the incoming records
	for (Opportunity o : Trigger.new) {

		// Has Owner chagned?
		if (o.OwnerID != o.Owner_Link__c) {
			o.Owner_Link__c = o.OwnerId;
		}
	}
}

Test:

public with sharing class TriggerTest_OwnerLink {

	static TestMethod void testOwnerLink() {

		// Grab two Users
		User[] users = [select Id from User limit 2];
		User u1 = users[0];
		User u2 = users[1];

		// Create an Opportunity
		System.debug('Creating Opportunity');
		Opportunity o1 = new Opportunity(CloseDate = Date.newInstance(2008, 01, 01), Name = 'Test Opportunity', StageName = 'New', OwnerId = u1.Id);
		insert o1;

		// Test: Owner_Link should be set to user 1
		Opportunity o2 = [select id, OwnerId, Owner_Link__c from Opportunity where Id = :o1.Id];
		System.assertEquals(u1.Id, o2.OwnerId);
		System.assertEquals(u1.Id, o2.Owner_Link__c);

		// Modify Owner
		o2.OwnerId = u2.Id;
		update o2;

		// Test: Owner_Link should be set to user 2
		Opportunity o3 = [select id, OwnerId, Owner_Link__c from Opportunity where Id = :o2.Id];
		System.assertEquals(u2.Id, o3.OwnerId);
		System.assertEquals(u2.Id, o3.Owner_Link__c);
	}
}

This then gave me a new Owner object on my Opportunity on which I could create Formulas:

Result

I could also use it in the DataLoader by referring to Owner_Link__r.Alias.

Hooray!

Easy to solve, but it’s a shame it was necessary.

The Bottom Line

  • Formulas can’t access Opportunity.Owner fields
  • Create a ‘shadow’ field to hold Owner and populate it via a Trigger

Today I had to add 32 Checkbox fields to my Contact object. I could have done this by manually creating each field, but the thought of clicking through over 100 screens didn’t take my fancy. So, I thought I’d take the Force.com Metadata API for a spin.

The last time I played with Metadata, I did it via the command-line Metadata Migration Tool . This time I decided to do it via the IDE.

Fortunately, there’s plenty of information available on this topic, the best of which are:

First I updated my Force.com IDE extensions in Eclipse (always make sure you’re working on the most recent version!). One nice feature I see they’ve added is the ability to select exactly which objects are copied down to the IDE. I simply ticked ‘Contacts’ and it appeared in my Package Explorer.

Then, I just did a Copy & Paste job on the metadata, changing the labels for each field.

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fields>
        <fullName>BI_Owns_Active_Bamboo__c</fullName>
        <defaultValue>false</defaultValue>
        <label>Entity Owns Active Bamboo</label>
        <type>Checkbox</type>
    </fields>
    <fields>
        <fullName>BI_Owns_Active_Clover__c</fullName>
        <defaultValue>false</defaultValue>
        <label>Entity Owns Active Clover</label>
        <type>Checkbox</type>
    </fields>
etc.

I then hit Save and the fields magically appeared in Salesforce.com — I was even somewhat naughty and did it straight into my Production instance (although best practice would be to do it in the Sandbox and then copy across / deploy the change).

Okay, I’ve still got a bit of work to do if I want to add the Checkboxes to my Page Layout, but at least the laborious part is over — the rest is all Drag & Drop!

The Bottom Line

  • The latest Force.com IDE facility lets you choose Standard Objects without having to play with package.xml
  • Creating lots of fields can be done quickly via the IDE, rather than the normal Force.com UI

I had a moment of panic today when searching for the Deploy to Server menu option in Eclipse today.

Prior to Summer ’08, the Force.com IDE had this command available when right-clicking an code file. Now it only appears if right-clicking the src folder.

My thanks to a post on the Salesforce Community forum for answering this one!

It reminds me of a time I was visiting a friend with a 4-year old son. The dining chairs had been moved and their son became distressed, saying that the chairs were all wrong. “Not wrong,” replied his mother, “just different.”

The Bottom Line

  • Don’t panic!