Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Make trailing slash optional on domain, Properties optional. #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ScheduleReportPackage/src/classes/ExportReport.cls
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public with sharing class ExportReport {
HttpRequest req = new HttpRequest();
req.setEndpoint(endPoint);
req.setMethod('GET');
req.setTimeout(30000);
//if session id is provided add to the cookie so the webservice is authenticated
if (sid!=null) req.setHeader('Cookie','sid='+sid);
return req;
Expand Down Expand Up @@ -108,6 +109,8 @@ public with sharing class ExportReport {
if (un == null || pw == null) {
throw new customException('Login credentials not defined in custom setting');
}
if (sfDomain.lastIndexOf('/') != sfDomain.length() - 1)
sfDomain += '/';
//login to salesforce using web callout to scrape for the session id
String authURL = sfDomain + '?un=' + un + '&pw=' + pw;
String sid = handleRedirectRequest(authURL);
Expand All @@ -116,17 +119,31 @@ public with sharing class ExportReport {
}

public blob getReportAsCSV(Id reportId, TestPath path) {
return this.getReportAsCSV(reportId, null, path);
}

public blob getReportAsCSV(Id reportId, String queryStringParams, TestPath path) {
String sid = getSessionId();

//url format to return an exported report in csv format
String uri = sfDomain + reportId + '?export=1&enc=UTF-8&xf=csv';
if (queryStringParams != null)
uri += queryStringParams;
return Blob.valueOf(getReport(uri, 'text/csv; charset=UTF-8', sid, path));
}

public String getReportAsHTML(Id reportId, TestPath path) {
return this.getReportAsHTML(reportId, null, path);
}

public String getReportAsHTML(Id reportId, String queryStringParams, TestPath path) {
String sid = getSessionId();

//url format to return report without headers
String uri = sfDomain + reportId + '?isdtp=nv';
if (queryStringParams != null)
uri += queryStringParams;
System.debug('uri: ' + uri);
String body = getReport(uri, 'text/html; charset=UTF-8', sid, path);
if (body.indexOf('<div class="bGeneratedReport') > 0) {
//return just the body of the report output
Expand Down
4 changes: 2 additions & 2 deletions ScheduleReportPackage/src/classes/Properties.cls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public with sharing class Properties {

private static String getStringValue(String key) {
String retVal;
String retVal = '';

try {
Properties__c prop = Properties__c.getValues(key);
Expand All @@ -13,7 +13,7 @@ public with sharing class Properties {
return retVal;
}
private static Boolean getBooleanValue(String key) {
Boolean retVal;
Boolean retVal = false;

try {
if (getStringValue(key).equals('true')) {
Expand Down
18 changes: 12 additions & 6 deletions ScheduleReportPackage/src/classes/ScheduleReportExport.cls
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public with sharing class ScheduleReportExport {

public void runScheduledReports() {
//return all jobs that are active and in date
List<Scheduled_Report_Export__c> schReports = [SELECT Id, Name, OwnerId, Owner.Email, Report_Id__c, Frequency__c, Day_of_the_week__c, Preferred_Time_of_Day__c, Send_to_me__c, Email_Template_Id__c, Save_Result__c
List<Scheduled_Report_Export__c> schReports = [SELECT Id, Name, OwnerId, Owner.Email, Report_Id__c, Frequency__c, Day_of_the_week__c, Preferred_Time_of_Day__c, Send_to_me__c, Email_Template_Id__c, Save_Result__c, Query_String_Params__c
FROM Scheduled_Report_Export__c
WHERE Active__c = true AND
(Start_Date__c <= TODAY AND End_Date__c >= TODAY)];
Expand All @@ -38,7 +38,7 @@ public with sharing class ScheduleReportExport {

public void runSingleReport(Id scheduledReportId) {
//running a single report ignores all frequency checks and gets marked as manual
List<Scheduled_Report_Export__c> schReports = [SELECT Id, Name, OwnerId, Owner.Email, Active__c, Report_Id__c, Frequency__c, Day_of_the_week__c, Preferred_Time_of_Day__c, Send_to_me__c, Email_Template_Id__c, Save_Result__c
List<Scheduled_Report_Export__c> schReports = [SELECT Id, Name, OwnerId, Owner.Email, Active__c, Report_Id__c, Frequency__c, Day_of_the_week__c, Preferred_Time_of_Day__c, Send_to_me__c, Email_Template_Id__c, Save_Result__c, Query_String_Params__c
FROM Scheduled_Report_Export__c
WHERE Id = :scheduledReportId LIMIT 1];

Expand Down Expand Up @@ -67,7 +67,7 @@ public with sharing class ScheduleReportExport {

if (jobId != null) {
//executes future method one at a item for each job, each job has own future call
doExport(UserInfo.getSessionId(), jobId, schReport.Id, schReport.Report_Id__c, schReport.Name, getToAddresses(schReport), schReport.Email_Template_Id__c, schReport.Save_Result__c);
doExport(UserInfo.getSessionId(), jobId, schReport.Id, schReport.Report_Id__c, schReport.Query_String_Params__c, schReport.Name, getToAddresses(schReport), schReport.Email_Template_Id__c, schReport.Save_Result__c);
} else {
system.debug('JOB_NOT_CREATED');
throw new customException('JOB_NOT_CREATED');
Expand Down Expand Up @@ -208,7 +208,7 @@ public with sharing class ScheduleReportExport {
*/

@future (callout=true)
public static void doExport(String sid, Id jobId, Id scheduledEmailId, Id reportId, String reportName, Set<String> toAddresses, String emailTemplateId, Boolean saveResult) {
public static void doExport(String sid, Id jobId, Id scheduledEmailId, Id reportId, String queryStringParams, String reportName, Set<String> toAddresses, String emailTemplateId, Boolean saveResult) {

try {
//checks that an action is defined - eiter email or save results
Expand All @@ -218,11 +218,13 @@ public with sharing class ScheduleReportExport {

ExportReport report = new ExportReport();
//return the blobcsv file to be attached and saved
Blob reportCSV = report.getReportAsCSV(reportId, null);
System.debug('Getting Report as CSV: ' + reportId);
Blob reportCSV = report.getReportAsCSV(reportId, queryStringParams, null);

//runs the report without headers and scrapes the results to include in body of emails
//report scrapping will be added to end of any template
String reportHTML = report.getReportAsHTML(reportId, null);
System.debug('Getting Report as HTML: ' + reportId);
String reportHTML = report.getReportAsHTML(reportId, queryStringParams, null);

//sends email with attachment and report scrapping
sendReportAsEmail(reportCSV, reportName, reportHTML, toAddresses, emailTemplateId);
Expand Down Expand Up @@ -254,6 +256,9 @@ public with sharing class ScheduleReportExport {
String[] ccAddresses = new String[] {complianceEmail};

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSenderDisplayName('');
mail.setReplyTo('[email protected]');
mail.setSaveAsActivity(false);
mail.setToAddresses(arrToAddresses);
//added option to define compliance email address so they can monitor external sends
if (ccCompliance) mail.setCcAddresses(ccAddresses);
Expand Down Expand Up @@ -313,6 +318,7 @@ public with sharing class ScheduleReportExport {
for (Scheduled_Report_Export__c scheduledEmail : scheduledEmails) {
Scheduled_Report_Export_Job__c job = new Scheduled_Report_Export_Job__c();
job.Scheduled_Report_Export__c = scheduledEmail.Id;
job.Query_String_Params__c = scheduledEmail.Query_String_Params__c;
job.Type__c = ExecutionMethod;
job.Submitted_Date__c = Datetime.now();
jobs.add(job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private class ScheduleReportExport_test {

Id reportId = schReports.Report_Id__c;

ScheduleReportExport.doExport('123', jobId, schReports.Id, reportId, schReports.Name, null, null, false);
ScheduleReportExport.doExport('123', jobId, schReports.Id, reportId, '', schReports.Name, null, null, false);

Test.StopTest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,96 @@ private class scheduledReportControllerExtension_test {
recipients = ext.getRecipients();
system.assertEquals(0, recipients.size());
}

static testMethod void test_buildParamList() {
//Create test data
String queryString = '&pv0=value+1&pv1=value+2';

//Invoke functionality
Test.startTest();
List<scheduledReportExportControllerExtension.QueryStringParam> paramList
= scheduledReportExportControllerExtension.buildQueryStringParamList(queryString);
Test.stopTest();

//Check results
System.assertEquals(2, paramList.size());
System.assertEquals('pv0', paramList.get(0).key);
System.assertEquals('value+1', paramList.get(0).value);
System.assertEquals('pv1', paramList.get(1).key);
System.assertEquals('value+2', paramList.get(1).value);
}

static testMethod void test_deserializeParamList() {
//Create test data
List<scheduledReportExportControllerExtension.QueryStringParam> paramList
= new List<scheduledReportExportControllerExtension.QueryStringParam>();
paramList.add(new scheduledReportExportControllerExtension.QueryStringParam('key1', 'value 1'));
paramList.add(new scheduledReportExportControllerExtension.QueryStringParam('key2', 'value+2'));


//Invoke functionality
Test.startTest();
String actualResult = scheduledReportExportControllerExtension.deserializeParamList(paramList);
Test.stopTest();

//Check results
String expectedResult = '&key1=value 1&key2=value+2';
System.assertEquals(expectedResult, actualResult);
}

static testMethod void test_addParam() {
//Create test data
Scheduled_Report_Export__c schReport = createTestScheduledReportExport('Daily', null, null);
PageReference pr = Page.editScheduledReportExport;
ApexPages.StandardController sc = new ApexPages.StandardController(schReport);
scheduledReportExportControllerExtension ext = new scheduledReportExportControllerExtension(sc);
Test.setCurrentPageReference(pr);

List<scheduledReportExportControllerExtension.QueryStringParam> paramList
= new List<scheduledReportExportControllerExtension.QueryStringParam>();
paramList.add(new scheduledReportExportControllerExtension.QueryStringParam('key1', 'value 1'));
paramList.add(new scheduledReportExportControllerExtension.QueryStringParam('key2', 'value+2'));


//Invoke functionality
Test.startTest();
ext.queryStringParamAddValue = 'value 1';
ext.addQueryStringParam();
ext.queryStringParamAddValue = 'value 2';
ext.addQueryStringParam();
Test.stopTest();

//Check results
String expectedResult = '&pv0=value+1&pv1=value+2';
System.assertEquals(expectedResult, ext.schReport.Query_String_Params__c);
}

static testMethod void test_removeParam() {
//Create test data
Scheduled_Report_Export__c schReport = createTestScheduledReportExport('Daily', null, null);
PageReference pr = Page.editScheduledReportExport;
ApexPages.StandardController sc = new ApexPages.StandardController(schReport);
scheduledReportExportControllerExtension ext = new scheduledReportExportControllerExtension(sc);
Test.setCurrentPageReference(pr);

List<scheduledReportExportControllerExtension.QueryStringParam> paramList
= new List<scheduledReportExportControllerExtension.QueryStringParam>();
paramList.add(new scheduledReportExportControllerExtension.QueryStringParam('key1', 'value 1'));
paramList.add(new scheduledReportExportControllerExtension.QueryStringParam('key2', 'value+2'));


//Invoke functionality
Test.startTest();
ext.queryStringParamAddValue = 'value 1';
ext.addQueryStringParam();
ext.queryStringParamAddValue = 'value 2';
ext.addQueryStringParam();
ext.queryStringParamRemoveKey = 'pv0';
ext.removeQueryStringParam();
Test.stopTest();

//Check results
String expectedResult = '&pv0=value+2';
System.assertEquals(expectedResult, ext.schReport.Query_String_Params__c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<behavior>Required</behavior>
<field>Scheduled_Report_Export__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Query_String_Params__c</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
Expand Down Expand Up @@ -96,8 +100,6 @@
<relatedList>RelatedNoteList</relatedList>
</relatedLists>
<showEmailCheckbox>false</showEmailCheckbox>
<showHighlightsPanel>true</showHighlightsPanel>
<showInteractionLogPanel>true</showInteractionLogPanel>
<showRunAssignmentRulesCheckbox>false</showRunAssignmentRulesCheckbox>
<showSubmitAndAttachButton>false</showSubmitAndAttachButton>
<summaryLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<behavior>Edit</behavior>
<field>Day_of_the_week__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Query_String_Params__c</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
Expand Down Expand Up @@ -138,8 +142,6 @@
<sortOrder>Asc</sortOrder>
</relatedLists>
<showEmailCheckbox>false</showEmailCheckbox>
<showHighlightsPanel>true</showHighlightsPanel>
<showInteractionLogPanel>true</showInteractionLogPanel>
<showRunAssignmentRulesCheckbox>false</showRunAssignmentRulesCheckbox>
<showSubmitAndAttachButton>false</showSubmitAndAttachButton>
<summaryLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<deploymentStatus>Deployed</deploymentStatus>
<description>History of Scheduled Report Export Jobs</description>
<enableActivities>false</enableActivities>
<enableEnhancedLookup>false</enableEnhancedLookup>
<enableFeeds>false</enableFeeds>
<enableHistory>false</enableHistory>
<enableReports>false</enableReports>
Expand Down Expand Up @@ -73,6 +74,16 @@ IF( Results_Sent__c , IMAGE(&apos;/img/func_icons/util/mailFrontEnv16.gif&apos;,
<type>Text</type>
<unique>false</unique>
</fields>
<fields>
<fullName>Query_String_Params__c</fullName>
<description>Parameters to put on the query string when requesting the report to export.</description>
<externalId>false</externalId>
<label>Query String Params</label>
<length>255</length>
<required>false</required>
<type>Text</type>
<unique>false</unique>
</fields>
<fields>
<fullName>Results_Saved__c</fullName>
<defaultValue>false</defaultValue>
Expand Down
Loading