Skip to content

Instantly share code, notes, and snippets.

@forcethesales
Last active April 14, 2021 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forcethesales/65e501397d9ad320a7cb07ca0516d67e to your computer and use it in GitHub Desktop.
Save forcethesales/65e501397d9ad320a7cb07ca0516d67e to your computer and use it in GitHub Desktop.
Custom Metadata Type in VF Controller
//Controller by Jessie Rymph included with unmanaged package Unsubscribe Link from AppExchange & Salesforce Labs.
//the controller tells the visualforce page jrsl_ul_UnsubscribeFinish to display text if a custom metadata type says "Yes" to a confirmation email
//April 9, 2021
public without sharing class jrsl_ul_UnsubscribeController {
private final jrsl_ul_Unsubscribe_Link__mdt uLmDt;
public string displayText {get; set;}
public boolean booly {get; set;}
public jrsl_ul_UnsubscribeController(){
//query for the unsubscribe link custom metadata type with the masterlabel "unsubscribe".
try{
uLmDt = [SELECT jrsl_ul_Send_Confirmation_Email__c
FROM jrsl_ul_Unsubscribe_Link__mdt
WHERE MasterLabel = 'Unsubscribe'];
if (uLmDt.jrsl_ul_Send_Confirmation_Email__c=='Yes')
{
booly = true;
displayText ='Thank you. You have successfully unsubscribed. You will receive a confirmation email shortly.';
system.debug('The confirmation email is turned on.');
}
//if the Send confirmation email field is set to no or none, include no additional text on the VF page.
else
{displayText = 'Thank you. You have successfully unsubscribed.';
booly = false;
system.debug('The confirmation email is turned off.');
}
}
catch(Exception e) {
System.debug('The cmdt jrsl_ul_Unsubscribe_Link__mdt is missing a record whose masterlabel is Unsubscribe. Here is the error:' + e.getMessage());
displayText= 'ERROR: There is an error with the Unsubscribe Link package. The Custom Metadata Type jrsl_ul_Unsubscribe_Link__mdt is missing a record whose masterlabel is Unsubscribe.';
}
}
}
//test by Jessie Rymph included with unmanaged package Unsubscribe Link from AppExchange & Salesforce Labs.
// April 13, 2021
@isTest
public class jrsl_ul_UnsubscribeControllerTest {
@isTest public static void testMyController () {
Test.startTest();
system.debug('If the test fails with the error "List has no rows for assignment to SObject", then you are missing a Custom Metadatatype of Unsubscribe Link with MasterLabel Unsubscribe. This is for the unmanaged package Unsubscribe Link.');
//Look for Custom Metadata Type
jrsl_ul_Unsubscribe_Link__mdt uLmDt = [SELECT jrsl_ul_Send_Confirmation_Email__c
FROM jrsl_ul_Unsubscribe_Link__mdt
WHERE MasterLabel = 'Unsubscribe'];
//instantiate the controller
jrsl_ul_UnsubscribeController controller = new jrsl_ul_UnsubscribeController();
//If the boolean is true, then display that they will receive an email.
IF(controller.booly== true)
{system.assertEquals('Thank you. You have successfully unsubscribed. You will receive a confirmation email shortly.',
controller.displayText);
system.debug('The unsubscribe cmdt is set to yes, send the email. So the visualforce page says you will receive a confirmation email shortly');
}
else if (controller.booly == false)
//If the boolean is set to false, do not mention the email.
{system.assertEquals('Thank you. You have successfully unsubscribed.',
controller.displayText);
system.debug('The unsubscribe cmdt is set to no, do not send the email. So no text appears about the email.');}
else
//Otherwise, display an error to show that the Unsubscribe Link mdt is missing.
{system.assertEquals('ERROR: There is an error with the Unsubscribe Link package. The Custom Metadata Type jrsl_ul_Unsubscribe_Link__mdt is missing a record whose masterlabel is Unsubscribe.', controller.displayText);}
test.stopTest();
}
}
<!-- by Jessie Rymph included with unmanaged package Unsubscribe Link from AppExchange & Salesforce Labs.-->
<!-- April 9, 2021-->
<!-- The custom controller adds additional text if the organization wants to send a confirmation email.-->
<!-- The Unsubscribe Link flow finishes at this page.-->
<apex:page controller="jrsl_ul_UnsubscribeController" showHeader="false" applyHtmlTag="true" applyBodyTag="false" standardStylesheets="false" lightningStylesheets="true">
<p></p>
<apex:pageMessages ></apex:pageMessages>
<span style="font-family: Arial Unicode MS; text-align:center; font-size: 14pt;">{!displayText}
</span>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment