Generate and Return Salary Slip PDF via Custom API in Dynamics 365 Finance & Operations
Content:
In this blog, I will share how I implemented a custom API in Dynamics 365 Finance & Operations to generate a Salary Slip (PDF) for employees and return the file details as a response.
Business Requirement
The requirement was to generate a Salary Slip PDF for an employee, save it on the server, and send the following information back to the API response:
-
Employee ID
-
File Path (server directory)
-
File Name
-
Full Path (complete physical path)
Response Contract Code
[DataContractAttribute]
public class SalarySlipResponse
{
str filePath;
str fileName;
str fullPath;
str employeeId;
[DataMemberAttribute("FilePath")]
public str parmFilePath(str _filePath = filePath)
{
filePath = _filePath;
return filePath;
}
[DataMemberAttribute("FileName")]
public str parmFileName(str _fileName = fileName)
{
fileName = _fileName;
return fileName;
}
[DataMemberAttribute("FullPath")]
public str parmFullPath(str _fullPath = fullPath)
{
fullPath = _fullPath;
return fullPath;
}
[DataMemberAttribute("EmployeeId")]
public str parmEmployeeId(str _employeeId = employeeId)
{
employeeId = _employeeId;
return employeeId;
}
}
Service Method Code
public SalarySlipResponse generateSalarySlip(SalarySlip_Contract _contract)
{
SRSReportRunController controller;
SRSPrintDestinationSettings settings;
str fileName, filePath, folderPath, employeeId, payPeriodCode;
SalarySlipResponse response;
try
{
controller = new SRSReportRunController();
controller.parmReportName(ssrsReportStr(PRSalarySlip, DesignKFC));
controller.parmReportContract().parmRdpContract(_contract);
settings = controller.parmReportContract().parmPrintSettings();
settings.printMediumType(SRSPrintMediumType::File);
settings.fileFormat(SRSReportFileFormat::PDF);
settings.overwriteFile(true);
// Values from contract
employeeId = _contract.parmEmployeeId();
payPeriodCode = _contract.parmPayPeriodCode();
// File name = EmployeeId + PayPeriod
fileName = strFmt("%1-%2-SalarySlip.pdf", employeeId, payPeriodCode);
folderPath = @"C:\AOSService\PackagesLocalDirectory\UploadDocs\";
filePath = folderPath + fileName;
settings.fileName(filePath);
// Run report
controller.runReport();
// Build response
response = new SalarySlipResponse();
response.parmFilePath("//PDFfile/");
response.parmFileName(fileName);
response.parmFullPath(filePath);
response.parmEmployeeId(employeeId);
return response;
}
catch (Exception::Error)
{
error("Error generating SalarySlip Report.");
return null;
}
}
Sample API Response
{
"EmployeeId": "000025",
"FilePath": "//PDFfile/",
"FileName": "000025-Jun2025-SalarySlip.pdf",
"FullPath": "C:\\AOSService\\PackagesLocalDirectory\\UploadDocs\\000025-Jun2025-SalarySlip.pdf"
}
🔹 Benefits
-
Automatically generates and saves Salary Slip as PDF.
-
Provides API consumers with full file details.
-
Flexible to extend (for Base64 content, email attachments, etc.).
Comments
Post a Comment