Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //a controller class to get incidents for display on VF page and then
- //anonymise the incidents and related contacts
- public class incidentAnonController {
- public Incident__c firstDate{get; set;}
- public Incident__c lastDate{get; set;}
- public incidentAnonController(ApexPages.StandardController controller){
- Integer dy = System.today().day();
- Integer mo = System.today().month();
- Integer yr = System.today().year();
- firstDate = new Incident__c(Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-6));
- lastDate = new Incident__c(Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-12));
- }
- //method to anonymise relevant records
- public void anonymise() {
- //list of incidents older than 6 months. These will be anonymised.
- List<Incident__c> results = [SELECT Id, Incident_name__c, Date_of_incident__c, Name, Garda_Name__c
- FROM Incident__c WHERE Date_of_Incident__c <= :firstDate.Date_Of_Incident__c AND
- Date_of_Incident__c >= :lastDate.Date_Of_Incident__c];
- //anonymise incidents that are older than 6 months
- for (Incident__c inc : results) {
- inc.Incident_name__c = inc.Name;
- inc.Garda_Name__c = '';
- inc.Pulse_ID__c = '';
- }
- update results;
- @isTest
- public class incidentAnonTest {
- public static testMethod void incidentAnon() {
- Integer dy = System.today().day();
- Integer mo = System.today().month();
- Integer yr = System.today().year();
- //create an incident
- Incident__c inc1 = new Incident__c();
- inc1.Date_Of_Incident__c = Date.newInstance(yr, mo, dy).addMonths(-7);
- inc1.Incident_name__c = 'Incident 1';
- inc1.Garda_Name__c = 'Test Garda';
- insert inc1;
- PageReference pageRef = Page.AnonymiseIncidents;
- Test.setCurrentPage(pageRef);
- ApexPages.StandardController sc = new ApexPages.StandardController(inc1);
- incidentAnonController testInc = new incidentAnonController(sc);
- inc1 = [SELECT Id, Garda_Name__c, Date_of_Incident__c, FROM Incident__c WHERE Id = :inc1.Id];
- testInc.anonymise();
- System.debug(inc1.Date_Of_Incident__c);
- System.debug(inc1.Garda_Name__c);
- System.assertEquals(TRUE, String.isBlank(inc1.Garda_Name__c));
- }
- <apex:page standardController="Incident__c" extensions="incidentAnonController" docType="html-5.0">
- <apex:form >
- <apex:pageBlock title="Notes on Usage">
- <apex:pageBlockSection >
- //some content
- </apex:pageBlockSection>
- </apex:pageBlock>
- <apex:pageBlock title="Date Ranges for Anonymisation">
- <apex:pageBlockSection >
- <apex:inputField value="{!firstDate.Date_Of_Incident__c}" label="From (Default 6 Months ago)"/>
- <apex:inputField value="{!lastDate.Date_Of_Incident__c}" label="To (Default 1 year ago)"/>
- </apex:pageBlockSection>
- </apex:pageBlock>
- <apex:pageBlock title="List of Incidents">
- <apex:pageBlockTable value="{!incidents}" var="inc">
- <apex:column headerValue="Incident Number">
- <apex:outputLink value="/{!inc.id}">{!inc.Name}</apex:outputLink>
- </apex:column>
- <apex:column value="{!inc.Incident_Name__c}"/>
- <apex:column value="{!inc.Date_Of_Incident__c}"/>
- </apex:pageBlockTable>
- <apex:pageBlockButtons >
- <apex:commandButton action="{!anonymise}" value="Anonymise"/>
- <apex:commandButton onclick="location.reload();" value="Update List"/>
- </apex:pageBlockButtons>
- </apex:pageBlock>
- </apex:form>
Add Comment
Please, Sign In to add comment