Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a SOQL query similar to the following:

SELECT Id, ContractNumber FROM Contract,
(SELECT Id, WhoId, WhatId, ActivityDate FROM ActivityHistories)
FROM Contract

Is it possible to get the name of the Salesforce user that created the activity record? Or am I going to be required to fetch a full list of users and do the mapping in my server-side code?

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

Absolutely.

 [SELECT Id, ContractNumber,  
 (SELECT Id, WhoId, WhatId, ActivityDate, createdby.name FROM ActivityHistories) 
 FROM Contract];

Fields on related objects (objects that you have a lookup to) can be referenced via dot notation in soql. You can find more information about this here:

Client applications need to be able to query for more than a single type of object at a time. SOQL provides syntax to support these types of queries, called relationship queries, against both standard objects and custom objects.

Relationship queries traverse parent-to-child and child-to-parent relationships between objects to filter and return results. They are similar to SQL joins. However, you cannot perform arbitrary SQL joins. The relationship queries in SOQL must traverse a valid relationship path as defined in the rest of this section.

You can use relationship queries to return objects of one type based on criteria that applies to objects of another type, for example, “return all accounts created by Bob Jones and the contacts associated with those accounts.” There must be a parent-to-child or child-to-parent relationship connecting the objects. You can’t write arbitrary queries such as “return all accounts and users created by Bob Jones.”

share|improve this answer
    
Perfect, thank you. –  Justin Helgerson Feb 22 '13 at 22:28
add comment

The user who created the record would be stored in the CreatedById lookup. As it's a standard lookup the syntax for retrieving a field (name as an example) from the related object would be CreatedBy.name.

So you could pull this by modifying your query to:

SELECT Id, ContractNumber, 
(SELECT Id, WhoId, WhatId, ActivityDate, CreatedBy.name FROM ActivityHistories)
FROM Contract
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.