Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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.

 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.

1 Answer

0 votes
by (32.1k points)
edited by

Salesforce enables access to related data through what they call relationship queries

Rather than joining, you specify the query like this:

Since you can't modify the application code, generating 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.

Are you willing to pursue a career in Salesforce, here's an opportunity for you Salesforce certification provided by intellipaat!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...