SkyVisualEditor’s Search Template is a useful template to create search pages within Salesforce. You can add more color to it by using Apex Class Extensions. Through this blog, I would like to help you understand more about the Search Template by introducing 3 useful Apex Class Extensions.
#1: Automatically Set the Search Criteria
If you want to search updated data in the past seven days, users are asked to select From/To dates by hand. You can use the Apex Class Extension to automatically set the search criteria if the conditions are roughly fixed. This type of extension could be used to set any kind of field in the Search Template.
Let’s see the sample code:
public with sharing class DefaultConditionExtender extends SkyEditor2.Extender{
SearchPage extension {get;set;}
public DefaultConditionExtender(SearchPage ext){
this.extension = ext;
}
// Override the “init” method and set the initial criteria for the search criteria
public override void init(){
// Set the Start Date of the range specification
extension.Componet6_from.Date__c = Date.today() – 7;
// Set the End Date of the range specification
extension.Componet6_to.Date__c = Date.today();
// Set the Operator for field specification
extension.Component9_op = SkyEditor2.TextHolder(‘ge’);
// Set the criteria value for the field specification
extension.Component9_val.Text__c = ‘10000’;
}
}
This sample code conducts the following two things:
Sets the range specification of the Date field called Component6 to search seven days ago to today.
Sets the value of “more than 10000″ to a Numeric search field called Component9.
#2: Automatically Run the Initial Search
The code in Sample #1 will set the initial criteria of the search, but it will not run automatically. Let’s make the search run automatically when the page is opened.
public with sharing class DefaultConditionExtender extends SkyEditor2.Extender{
SearchPage extension {get;set;}
public DefaultConditionExtender(SearchPage ext){
this.extension = ext;
}
public override void init(){
// Run search
extension.doSearch();
}
}
To run the initial search, call the page controller’s “doSearch” method at the very end of the overridden init() method. To use Sample #1 and #2 together you will simply add one line of code “extension.doSearch();” in the last row of the init() method given in Sample #1. This will allow you to set the default search criteria values, then run the search. If you simply use Sample #2 as it is currently shown, your search will run upon page load, but no filters will have been applied to the search itself.
#3: Select Records From the Search Result And Save As A PDF
So far, I have introduced some samples to do customization on the search criteria of the template. Now, let’s do some on the search results. I would like to show you the way to select the search result records and save them as an Attachment to the record data.
public with sharing class SavePDFToAttachmentExtender extends SkyEditor2.Extender{ SearchPage extension {get;set;}
public SavePDFToAttachmentExtender(SearchPage ext){
this.extension = ext;
}
public PageReference savePdf(){
List<Id> saveRecords = new List<Id>();
for(Component3Item i: Component3.items){
// Add Record ID to the List if the record is saved and also has a check in the checkbox Column.
if(i.selected && i.record.Id != null){
saveRecords.add(i.record.Id);
}
}
if(saveRecords.size() > 0){
String filename = DateTime.now().format(‘YYYY/mm/DD HH:mm:SS’);
List<Attachment> attachmentList = new List<Attachment>();
for(Id i:saveRecords){
try{
// orderSheetSample is the name of the PDF page created in advance
PageReference pdf = Page.orderSheetSample;
// Give ID parameter to PageReference and retrieve contents.
pdf.getParameters().set(“id”,i);
Blob content = pdf.getContent();
// Create a File Record attachment with the retrieved contents.
Attachment attach = new Attachment();
attach.Name = filename;
attach.Body = pdf;
attach.ContentType = ‘application/pdf';
attach.ParentId = i;
attachmentList.add(attach);
}catch(Exception ex){}
}
try{
insert attachmentList;
SkyEditor2.Messages.addInfoMessage(‘Saved as PDF’);
}catch(DmlException ex){}
}
return null;
}
}
This sample code conducts the following 3 things:
Extract the Record ID for the records that are checked in the Data Table List (Selected Property)
Give a parameter setting to the ID extracted in Step 1. Retrieve the content of the PDF page by getContent() method.
Create a record by storing the PDF content into an Attachment file.
In Closing
I hope the 3 examples will help you create more flexible search pages. Apex Class Extensions will bring about outputs that could not be met without their use. If you would like to learn more about the Apex Class Extensions, you can look at the reference below:
Commenti