Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the name of the Lead Owner in a Lead custom formula field?

Tags:

salesforce

I've got an application that reads Lead records from Salesforce via the API and I want to link the Lead Owner field to an attribute in the application. The Lead Owner field doesn't up in the list of available fields but all the custom fields do.

So, my first attempt at a solution was to create a custom field that displayed the Lead Owner name. In the SF formula editor, as far as I can tell, it doesn't display the actual data field but instead displays the ID string. Which is pretty meaningless in the context that I need it for.

alt text http://skinny.fire-storm.net/forposting/insertfield.JPG

Is there a way that we can get at the data in the object that the ID string references?

alt text http://skinny.fire-storm.net/forposting/havewant.JPG

I have the RED BOX but need the GREEN BOX.

EDIT: I can't change the application code that calls the API. I can only change salesforce. So, this is more of a salesforce superuser / formula-writer question, not a question about writing code that calls the SF API.

like image 683
Jordan Hudson Avatar asked Dec 22 '22 03:12

Jordan Hudson


1 Answers

Salesforce allows access to related data through what they call relationship queries. Instead of joining, you specify the query like this:

System.debug([SELECT Owner.Name FROM Lead WHERE Id = '00QS00000037lvv'].Owner.Name);

Try running that in the system log, just replace the lead ID with one that you're looking at.

When accessing the data through the API, the principle is the same, your proxy objects should allow you to access Lead.Owner.Name.

EDIT:

I agree with eyescream, since you can't change the application code, creating an Apex trigger would be the best way to go here. Here's some code to get you started:

trigger Lead_UpdateOwner on Lead(before insert, before update)
{
    Map<Id, String> ownerMap = new Map<Id, String>();
    for (Lead lead : Trigger.new)
    {
        ownerMap.put(lead.OwnerId, null);
    }

    if (ownerMap.size() > 0)
    {
        for (User[] users : [SELECT Id, Name FROM User WHERE Id IN :ownerMap.keySet()])
        {
            for (Integer i=0; i<users.size(); i++)
            {
                ownerMap.put(users[i].Id, users[i].Name);
            }
        }
        for (Lead lead : Trigger.new)
        {
            lead.OwnerName__c = ownerMap.get(lead.OwnerId);
        }
    }
}

lead.OwnerName__c would need to be the name of your custom field on the lead object that will hold the owner name. Type Text, length 121.

like image 117
Adam Butler Avatar answered May 25 '23 15:05

Adam Butler