11

The following SOQL query is designed to return CampaignMember information by associated Contact.Name. The first time the SOQL query is run, it returns a list where two subsets of the CampaignMembers is sorted in alphabetical order (i.e. 'A,B,C,D,A,B,C,D' instead of 'A,A,B,B,C,C'.

SELECT New_TFA_School__c, Name, Id, Status, Referral__c, CreatedDate, Lead.Name, Lead.Primary_City__c, Lead.Primary_State__c, Lead.Primary_Country__c, Lead.Phone, Lead.Email, Lead.Graduation_Year__c, Contact.Name, Contact.Undergrad_School_Name__c, Contact.Graduation_Year__c, Contact.Email, Contact.Phone, Contact.MailingCity, Contact.MailingState, Contact.MailingCountry FROM CampaignMember WHERE CampaignID IN (SELECT Id FROM Campaign WHERE Campaign.Name = '2015 Michigan Regional Conference') ORDER BY Contact.Name

After running the query again, or refreshing the grid in the developer console the results appear in the correct order. I've confirmed this behavior in the JSON output from an associated REST endpoint, so I know it's not just a display issue in the Developer Console. I can't figure out why the the SOQL query would behave inconsistently if the underlying data hasn't changed ?

4
  • 6
    Two thoughts. 1. ORDER BY Contact.LastName, Contact.FirstName not Name. 2. Are there two Campaigns named "2015 Michigan Regional Conference"? Oct 16, 2015 at 17:37
  • I was able to confirm that there is only one 2015 Michigan Regional Conference. Good point on the Name ordering. Oct 16, 2015 at 18:29
  • 1
    Is it reproducible? If so, try making a test case using SeeAllData=true. The test case could assert that the records are correctly ordered. Then run the test case multiple times to see how consistent the problem is. Oct 21, 2015 at 8:01
  • The problem occurs the first time the query is run each day. On subsequent queries the result return in sorted order. The first query per day returns a partially sorted list. The is not affected by whether I'm using Contact.LastName, Contact.FirstName or Contact.Name to sort. The problem isn't what I'm sorting by per se- it's that the sort isn't working about once a day. Apr 17, 2017 at 16:08

2 Answers 2

1

Salesforce contact Name field is actually merge of Salutation + First Name + Last Name, thats why you should write exact field name which one you want to order.

enter image description here

Salesforce Field List

enter image description here

-1

As @Charles mentioned, the Name field on the Contact object is made up of Salutation, First Name and Last Name.

Try using contact.firstname or contact.lastname to sort according to your requirement.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .