Sales force Platform Encryption API – APEX

In this blog, I am going to explain how to use the TenantSecret object in salesforce to generate the tenant secret key for Platform encryption.

We are going to build the visualforce page that used to create tenant secret key and view all the existing tenant secret key. Visualforce page looks as shown below

The Page controller is shown below

controller :-
public class TenantController {

public TenantSecret secretKey {get;set;}
public TenantController(ApexPages.StandardController controller){
this.secretKey = new TenantSecret();
}

public PageReference insertNewSecret(){
try{
system.debug(‘Key’+secretKey );
insert secretKey ;
}catch(Exception e ){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
ApexPages.addMessage(myMsg);
}
return null ;
}

public List<TenantSecret> getKeyHistory(){

return [Select Id ,
Description ,
Source ,
Type ,
version,
status
from TenantSecret ];

}

}

Visual force page is shown below.

<apex:page standardController=”TenantSecret” extensions=”TenantController” docType=”html-5.0″ applyhtmltag=”true”
showheader=”true” sidebar=”false” standardstylesheets=”false”>

<apex:stylesheet value=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css&#8221; />

<style>
.headerStyle{
background-color: PowderBlue!important;
background-image: none !important;
color: Black!important;
font-size:100% !important;
text-align:center;
font-family: Candara, Calibri, Segoe, ‘Segoe UI’, Optima, Arial, sans-serif;
font-size: 14px;
font-variant: normal;
font-weight: normal;
line-height: 17px;
}

.tdcls{
background-color:#f8f8ff!important;
background-image: none !important;
color: Black!important;
font-size:100% !important;
text-align:center;
font-family: Candara, Calibri, Segoe, ‘Segoe UI’, Optima, Arial, sans-serif;
font-size: 14px;
font-variant: normal;
font-weight: normal;
line-height: 17px;
}

</style>

<apex:form styleClass=”headerStyle”>
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlock >
<apex:pageBlockSection columns=”3″>

<apex:inputField value=”{!secretKey.Description}” style=”width:80%;height:140%” />
<apex:inputField value=”{!secretKey.Type}” style=”width:40%;height:140%” />
<apex:commandButton value=”Save New Key” action=”{!insertNewSecret}”
style=”background:#4682b4;color:white;border-radius:7px” reRender=”existingkeys” />

</apex:pageBlockSection>

</apex:pageBlock>

<apex:pageBlock >
<apex:outputPanel id=”existingkeys”>

<table class=”table table-bordered table-inverse”>
<thead>
<tr>
<th class=”headerStyle”>Version </th>
<th class=”headerStyle”>status</th>
<th class=”headerStyle”>Type</th>
<th class=”headerStyle”>Description</th>
<th class=”headerStyle”>Source</th>
</tr>
</thead>
<tbody>
<apex:repeat value=”{!KeyHistory}” var=”key”>
<tr>
<td class=”tdcls”>{!key.Version} </td>
<td class=”tdcls”>{!key.status}</td>
<td class=”tdcls”>{!key.Type}</td>
<td class=”tdcls”>{!key.Description}</td>
<td class=”tdcls”>{!key.Source}</td>
</tr>
</apex:repeat>
</tbody>

</table>
</apex:outputPanel>

</apex:pageBlock>

</apex:form>

</apex:page>
Github URL for the code:-
https://github.com/rajamohanvakati/Platform-Encryption-

Visualforce Image Uploader

In this post, I am going to explain how to implement visualforce page image uploader which looks similar to the salesforce standard file upload features.

The final look of the Visualforce page looks as shown below. After uploading the image it will be added to the contact Attachments section. It is going to allow the only certain type of image types.

 

Understanding Controller.

The controller is having the two variables names imagesTypes, notAllowedTypes which are used to support the Images types that are allowed to upload, restrict as shown below.

private Set<String> imagesTypes = new Set<String> {‘image/jpeg’, ‘image/pjpeg’, ‘image/png’, ‘image/x- png’, ‘image/gif’};
private Set<String> notAllowedTypes = new Set<String> {‘application/octet-stream’ , ‘image/bmp’};
The below logic is used to performs the upload logic

public PageReference uploadAction(){
PageReference thePage = new PageReference( ‘/’+ parentId );
thePage.setRedirect( true );
if( this.validate() ){
return ( this.saveCurrentPicture() ) ? thePage : null;
}
else{
this.newAttach = new Attachment();
return null;
}
}
public Boolean saveCurrentPicture(){
Savepoint sp = Database.setSavepoint();
try{
this.newAttach.parentId = this.parentId;
this.newAttach.name = ‘Contact Picture’;
insert this.newAttach;
return true;
}
catch( Exception e ){
this.error += ERROR_NO_SAVE+'<br/>’;
Database.rollback( sp );
return false;
}
}
How to validate the image types?

below piece of logic is used to validate allowed image types that are uploaded.

private Boolean validate(){
Boolean ret = true;
this.error = ”;
if( !imagesTypes.contains( newAttach.ContentType ) ){
this.error += ERROR_IMG_TYPE+'<br/>’;
ret = false;
}
return ret;
}
Understanding Visauflorce Page logic.

Most important logic in the visualforce page is validating the image name length. the following javascript logic validates
the Image name length on upload Command Button Click.
function verifyNameLength(){
var textVal = document.getElementsByName(‘uploadImagePage:uploadAttachment:newAttach:inputFile:file’)[0].value;

if( textVal != “” ) {
var browser = navigator.userAgent;
var fileName = textVal;

if( browser.search( ‘MSIE’ ) > 0 ) {
var index = textVal.lastIndexOf( ‘\\’ );
fileName = textVal.substring( index +1);
}

if( fileName.length < 70) {
return true;
};
};
if( textVal == “” ) {
alert( ‘Please select an image to upload’ );
} else {
alert( ‘The file name must be 70 characters maximum’ );
}
document.getElementById(‘uploadImagePage:uploadAttachment’).reset();
return false;
}

The above logic is invoked from the Upload button to throw an error for length more than 70.

<apex:commandButton id=”Accept” action=”{!uploadAction}” value=”Upload” onclick=”return verifyNameLength();”></apex:commandButton>
Complete URL for the GitHub source code

https://github.com/rajamohanvakati/FileUpload