0

I execute the following query to get activity history for an account

  SELECT
    (SELECT Id, IsTask, ActivityDate, ActivitySubtype, Subject, Owner.Name,PrimaryWhoId,WhatId,PrimaryWho.Name, What.Name from ActivityHistories
      ORDER BY ActivityDate DESC NULLS LAST, LastModifiedDate DESC LIMIT #{limit})
  FROM Account where id = 

This works only when WhoPrimaryId exists - SharedActivities is enabled. I use REST API. If Shared Activities is not enabled then this query fails with error. Is there a way to determine what query to use efficiently ?

4
  • Hi, which particular error do you see ?
    – Patlatus
    Commented May 8, 2019 at 13:23
  • I get that PrimaryWhoId doesn't exist
    – GorillaApe
    Commented May 8, 2019 at 13:33
  • Have you tried to use dynamic SOQL?
    – Patlatus
    Commented May 8, 2019 at 13:33
  • @GorillaAge I have posted an answer which answers your question, please accept my answer as accepted if my answer helped you. Thanks.
    – Patlatus
    Commented May 10, 2019 at 11:51

1 Answer 1

0

To implement this in REST API, you need two calls.

The first call to determine if PrimaryWhoId is available

Call /services/data/v45.0/sobjects/ActivityHistory/describe

and check response fields attributes and check if PrimaryWhoId is present in the list.

Then if PrimaryWhoId is present in the fields map, execute query

 SELECT
    (SELECT Id, IsTask, ActivityDate, ActivitySubtype, Subject, Owner.Name,PrimaryWhoId,WhatId,PrimaryWho.Name, What.Name from ActivityHistories
      ORDER BY ActivityDate DESC NULLS LAST, LastModifiedDate DESC LIMIT #{limit})
  FROM Account where id = 

otherwise execute

 SELECT
    (SELECT Id, IsTask, ActivityDate, ActivitySubtype, Subject, Owner.Name,WhoId,WhatId,Who.Name, What.Name from ActivityHistories
      ORDER BY ActivityDate DESC NULLS LAST, LastModifiedDate DESC LIMIT #{limit})
  FROM Account where id = 

Example of response

{
  "actionOverrides" : [ ],
  "activateable" : false,
  "childRelationships" : [ ],
  "compactLayoutable" : false,
  "createable" : false,
  "custom" : false,
  "customSetting" : false,
  "deletable" : false,
  "deprecatedAndHidden" : false,
  "feedEnabled" : false,
  "fields" : [ 

{
    "aggregatable" : true,
    "aiPredictionField" : false,
    "autoNumber" : false,
    "byteLength" : 18,
    "calculated" : false,
    "calculatedFormula" : null,
    "cascadeDelete" : false,
    "caseSensitive" : false,
    "compoundFieldName" : null,
    "controllerName" : null,
    "createable" : false,
    "custom" : false,
    "defaultValue" : null,
    "defaultValueFormula" : null,
    "defaultedOnCreate" : false,
    "dependentPicklist" : false,
    "deprecatedAndHidden" : false,
    "digits" : 0,
    "displayLocationInDecimal" : false,
    "encrypted" : false,
    "externalId" : false,
    "extraTypeInfo" : null,
    "filterable" : true,
    "filteredLookupInfo" : null,
    "formulaTreatNullNumberAsZero" : false,
    "groupable" : true,
    "highScaleNumber" : false,
    "htmlFormatted" : false,
    "idLookup" : false,
    "inlineHelpText" : null,
    "label" : "Name ID",
    "length" : 18,
    "mask" : null,
    "maskType" : null,
    "name" : "WhoId",
    "nameField" : false,
    "namePointing" : true,
    "nillable" : true,
    "permissionable" : false,
    "picklistValues" : [ ],
    "polymorphicForeignKey" : true,
    "precision" : 0,
    "queryByDistance" : false,
    "referenceTargetField" : null,
    "referenceTo" : [ "Contact", "Lead" ],
    "relationshipName" : "Who",
    "relationshipOrder" : null,
    "restrictedDelete" : false,
    "restrictedPicklist" : false,
    "scale" : 0,
    "searchPrefilterable" : false,
    "soapType" : "tns:ID",
    "sortable" : true,
    "type" : "reference",
    "unique" : false,
    "updateable" : false,
    "writeRequiresMasterRead" : false
  },

You need to check if there is a field with name attribute PrimaryWhoId.

You must log in to answer this question.

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