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 ?

  • 6
    Two thoughts. 1. ORDER BY Contact.LastName, Contact.FirstName not Name. 2. Are there two Campaigns named "2015 Michigan Regional Conference"? – Charles Koppelman Oct 16 '15 at 17:37
  • I was able to confirm that there is only one 2015 Michigan Regional Conference. Good point on the Name ordering. – salesforce-hacker Oct 16 '15 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. – Daniel Ballinger Oct 21 '15 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. – salesforce-hacker Apr 17 '17 at 16:08
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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