REST API - Generate Report

Hi

Is there any way to generate a PDF copy of a report?

We would like to call our invoices based on rest api e.g if invoice number is: 339832 it will produce that invoice as a pdf.

Thanks
Aaron.

Yes you can do this. Running the report is fairly straightforward by calling RunDirect method on ARInvoiceSvc. The tricky thing is locating the rendered report and then decoding the file. I wrote a BAQ to get the report (looking for unique values from TaskNote and TaskDescription but you may want something more robust).

BAQ Retrieval Example:

select top (1)  
	[SysTask].[SysTaskNum] as [SysTask_SysTaskNum],
	[SysRptLst].[RptData] as [SysRptLst_RptData]
from Ice.SysTask as SysTask
inner join Ice.SysRptLst as SysRptLst on 
	SysTask.Company = SysRptLst.Company
	and SysTask.SysTaskNum = SysRptLst.SysTaskNum
where (SysTask.Company = @CurrentCompany  and SysTask.TaskDescription = @inTaskDesc  and SysTask.TaskNote = @inTaskNote)
order by SysTask.SysTaskNum Desc

PDF Retrieval Example:

var myinvfname = string.Format("ARInvForm_{0}.pdf", InvoiceNumber);

byte[] dataBytes = System.Convert.FromBase64String(myobj.SysRptLst_RptData);
result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(dataBytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
result.Content.Headers.ContentDisposition.FileName = myinvfname;
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
3 Likes

Hi, Aaron.

I am not sure which version of Epicor ERP you are currently running, but, assuming this is the latest (10.2.600.x), have you considered using Epicor Functions to achieve this? Seems like you have a repeatable logic block that you want executed either on a schedule or on demand via REST. Functions are perfect for this.

To illustrate the approcah, I have taken the liberty of creating a quick function that will print and return report PDF (as MIME/base64 encoded string) for supplied AR invoice number.

The code inside the function is fairly straightforward:

this.CallService<Erp.Contracts.ARInvFormSvcContract>(rpt =>
{
  var ts = new Erp.Tablesets.ARInvFormTableset();
  rpt.GetDefaults(ref ts);
  var param = ts.ARInvFormParam.First();
  var taskId = "AutoPrint " + Guid.NewGuid();
  param.TaskNote = taskId; // to match with sysrptlst
  param.InvoiceNum = arInvoiceNum;
  param.AutoAction = "SSRSGenerate";
  rpt.RunDirect(ts);
  
  base64Pdf = Convert.ToBase64String(Db.SysRptLst.First(row => row.Company == this.CompanyID && row.RptNote == taskId).RptData);
});

Of note is a little hack I am using here - I am supplying a GUID as task note and matching the report from resultant SysRptLst based on that. This way I eliminate the possibility of pulling in someone else’s report. Strinctly speaking the SysRptLst matching on company is not really necessary in this scenario, but it is a good practice to always do company clause match if table has it.

I am also attaching the function library itself so you have an idea of the references, input and output. The same principle can be applied to other printing operations.

Hope this helps. Just remember - functions are only callable in Rest API v2 so you will need to supply API Key when calling REST (whcih is a good practice to do anyway for additional security)

ReportLib.zip (10.6 KB)

3 Likes

That’s awesome, thank you.

I was able to get a SSRS Report using the API for that specific invoice.

Now to integrate this into PHP on our website :+1:

1 Like

On my database RptData is always blank. Is that a configuration issue?

Hi Aieat!

I’m testing your function, in most of the cases I get an error 400, related to Execution Timeout Expired, I think is related to API Rest timeout settings, because I already seting up the SSRS timeout acording to KB0047758
Its there a way to increase time execution in Rest Calls?

Same error in both Rest Help page and Postman: