4

I need to create simple Report with Lead/Contact data. How can I do it via REST/SOAP API? REST is preferred.

4

Ask and you shall receive. See Introducing the Salesforce Reports and Dashboards REST API, and more specifically Get Report Data without Saving Changes to or Creating a Report (emphasis mine):

Get Report Data without Saving Changes to or Creating a Report

Run a report without creating a report or changing an existing one by making a POST request to the query resource. Get report data without filling up your org with unnecessary reports.

Example

Get report data using the query resource.

/services/data/v37.0/analytics/reports/query

Include report criteria as reportMetadata in the POST request body. This POST request gets data about Opportunities:

{
"reportMetadata" : {
    "aggregates" : [ "RowCount" ],
    "chart" : null,
    "crossFilters" : [ ],
    "currency" : null,
    "description" : null,
    "detailColumns" : [ "OPPORTUNITY_NAME", "TYPE", "LEAD_SOURCE", "AMOUNT", "CLOSE_DATE", "NEXT_STEP", "STAGE_NAME", "PROBABILITY", "FISCAL_QUARTER", "AGE", "CREATED_DATE", "FULL_NAME", "ROLLUP_DESCRIPTION", "ACCOUNT_NAME" ],
    "developerName" : "OpportunityReport",
    "division" : null,
    "folderId" : "00DD000000086ujMAA",
    "groupingsAcross" : [ ],
    "groupingsDown" : [ ],
    "hasDetailRows" : true,
    "hasRecordCount" : true,
    "historicalSnapshotDates" : [ ],
    "id" : "00OD0000001leVCMAY",
    "name" : "Matrix",
    "reportBooleanFilter" : null,
    "reportFilters" : [ ],
    "reportFormat" : "MATRIX",
    "reportType" : {
      "label" : "Opportunities",
      "type" : "Opportunity"
    },
    "scope" : "organization",
    "showGrandTotal" : true,
    "showSubtotals" : true,
    "sortBy" : [ ],
    "standardDateFilter" : {
      "column" : "CLOSE_DATE",
      "durationValue" : "CUSTOM",
      "endDate" : null,
      "startDate" : null
    },
    "standardFilters" : [ {
      "name" : "open",
      "value" : "all"
    }, {
      "name" : "probability",
      "value" : ">0"
    } ]
  }
}

The Report section of the documentation (specifically the PATCH Request Body section) goes into great detail about how to structure the JSON. It looks like the same structure.

Also, another useful endpoint listed in the Describe section of the documentation can help you inspect existing reports you want to mimic if you're stuck.

 /services/data/<latest API version>/analytics/reports/<report ID>/describe

Here is a basic example I got working for Lead data. Set the folderId to your own Organization Id.

{
    "reportMetadata": {
        "aggregates": ["RowCount"],
        "chart": null,
        "crossFilters": [],
        "currency": null,
        "description": null,
        "detailColumns": ["OWNER", "FIRST_NAME", "LAST_NAME", "TITLE", "COMPANY", "LEAD_SOURCE", "RATING", "STREET", "EMAIL"],
        "developerName": "LeadReport",
        "division": null,
        "folderId": "00D290000000SuCEAU",
        "groupingsAcross": [],
        "groupingsDown": [],
        "hasDetailRows": true,
        "hasRecordCount": true,
        "historicalSnapshotDates": [],
        "id": "",
        "name": "Matrix",
        "reportBooleanFilter": null,
        "reportFilters": [],
        "reportFormat": "MATRIX",
        "reportType": {
            "label": "Leads",
            "type": "LeadList"
        },
        "scope": "user",
        "showGrandTotal": true,
        "showSubtotals": true,
        "sortBy": [],
        "standardDateFilter": {
            "column": "CREATED_DATE",
            "durationValue": "CUSTOM",
            "endDate": null,
            "startDate": null
        },
        "standardFilters": []
    }
}
  • 1
    I saw it by I don't understand how to create new report only with lead(s), contact(s) – toprysk Jul 15 '16 at 16:31
  • I added an example for Lead and more detail that should help you on your way to further research. @toprysk – Adrian Larson Jul 15 '16 at 19:23
  • Can You please show your code. I still have a problem. I can't even clone my test report. Here is my code: string requestUri = authClient.InstanceUrl + "/services/data/" + authClient.ApiVersion + "/analytics/reports?cloneId=00O4B000000JsMOUA0"; var content = new StringContent("{ \"reportMetadata\" : {\"name\":\"clone test API " + DateTime.Now.Ticks + "\"} }"); HttpResponseMessage response = await client.PostAsync(requestUri, content); string result = await response.Content.ReadAsStringAsync(); – toprysk Jul 18 '16 at 17:03
  • What do you mean show my code? I have extensively demonstrated the JSON involved. Why are you even cloning anything? The text I added emphasis to here again since it's relevant: Run a report without creating a report or changing an existing one by making a POST request to the query resource. – Adrian Larson Jul 18 '16 at 17:06
  • I didnt clone any code, Im just trying to clone existed report (template) and then I will run it with my 'reportMetadata'. At the moment I have next problem in the response after I sent post request: "[{\"errorCode\":\"METHOD_NOT_ALLOWED\",\"message\":\"HTTP Method 'POST' not allowed. Allowed are GET\"}]" – toprysk Jul 19 '16 at 10:29

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.