-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'mass delete' button on Log__c list views (#123)
* Added MassDelete button on Log__c list views to display VF page LogMassDelete * New extension class LogMassDeleteExtension handles the deletion logic. It queries UserRecordAccess to make sure that only Log__c records that the user has permission to delete will be included - any other records selected in the list view are ignored * Added details to README about new 'mass delete' button
- Loading branch information
Showing
16 changed files
with
277 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions
63
nebula-logger/main/log-management/classes/LogMassDeleteExtension.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//------------------------------------------------------------------------------------------------// | ||
// This file is part of the Nebula Logger project, released under the MIT License. // | ||
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. // | ||
//------------------------------------------------------------------------------------------------// | ||
|
||
/** | ||
* @group log-management | ||
* @description Manages mass deleting `Log__c` records that have been selected by a user on a `Log__c` list view | ||
*/ | ||
public with sharing class LogMassDeleteExtension { | ||
private ApexPages.StandardSetController controller; | ||
|
||
/** | ||
* @description LogMassDeleteExtension description | ||
* @param controller controller description | ||
* @return return description | ||
*/ | ||
public LogMassDeleteExtension(ApexPages.StandardSetController controller) { | ||
if (Schema.Log__c.SObjectType.getDescribe().isDeletable() == false) { | ||
String deleteAccessError = 'You do not have access to delete logs records'; | ||
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, deleteAccessError)); | ||
} | ||
|
||
this.controller = controller; | ||
} | ||
|
||
/** | ||
* @description Filters the list of selected `Log__c` records to only include records that the current user can delete (based on object-level access) | ||
* @return return The matching `Log__c` records that the current user has access to delete | ||
*/ | ||
public List<Log__c> getDeletableLogs() { | ||
// The UserRecordAccess object is weird - RecordId is not an actual ID field, so you can't filter using `List<SObject>` or `List<Id>`, you have to use strings | ||
// So, here's some code that would be unnecessary if RecordId were a polymorphic ID field instead | ||
List<String> logIds = new List<String>(); | ||
for (Log__c selectedLog : (List<Log__c>) this.controller.getSelected()) { | ||
logIds.add(selectedLog.Id); | ||
} | ||
|
||
// Get the list of record IDs that the current user can delete | ||
List<Id> deletableLogIds = new List<Id>(); | ||
for (UserRecordAccess recordAccess : [ | ||
SELECT RecordId | ||
FROM UserRecordAccess | ||
WHERE UserId = :UserInfo.getUserId() AND RecordId IN :logIds AND HasDeleteAccess = TRUE | ||
]) { | ||
deletableLogIds.add(recordAccess.RecordId); | ||
} | ||
|
||
// Get the logs + any fields shown in the VF page | ||
return [SELECT Id, Name, LoggedBy__c, LoggedBy__r.Name, StartTime__c, TotalLogEntries__c FROM Log__c WHERE Id IN :deletableLogIds]; | ||
} | ||
|
||
public PageReference deleteSelectedLogs() { | ||
try { | ||
delete getDeletableLogs(); | ||
} catch (Exception ex) { | ||
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage())); | ||
} | ||
|
||
// The controller's method cancel() just returns the user to the previous page - it doesn't rollback any DML statements (like the delete above) | ||
return this.controller.cancel(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
nebula-logger/main/log-management/classes/LogMassDeleteExtension.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>51.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
nebula-logger/main/log-management/objects/Log__c/webLinks/MassDelete.webLink-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<WebLink xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<fullName>MassDelete</fullName> | ||
<availability>online</availability> | ||
<displayType>massActionButton</displayType> | ||
<height>600</height> | ||
<linkType>page</linkType> | ||
<masterLabel>Mass Delete</masterLabel> | ||
<openType>sidebar</openType> | ||
<page>LogMassDelete</page> | ||
<protected>false</protected> | ||
<requireRowSelection>true</requireRowSelection> | ||
</WebLink> |
64 changes: 64 additions & 0 deletions
64
nebula-logger/main/log-management/pages/LogMassDelete.page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<!------------------------------------------------------------------------------------------------// | ||
// This file is part of the Nebula Logger project, released under the MIT License. // | ||
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. // | ||
//---------------------------------------------------------------------------------------------- --> | ||
|
||
<apex:page standardController="Log__c" recordSetVar="logs" extensions="LogMassDeleteExtension" tabStyle="Log__c" lightningStyleSheets="true"> | ||
<apex:slds /> | ||
|
||
<script> | ||
// TODO revisit how toast message is shown - it needs to be triggered based on success/failure result of Apex method | ||
function showSuccessMessage() { | ||
setTimeout( | ||
sforce.one.showToast({ | ||
type : 'Success', | ||
title : 'Logs Successfully Deleted', | ||
message : '{!deletableLogs.size} record(s) were deleted' | ||
}), | ||
2000 | ||
); | ||
} | ||
</script> | ||
|
||
<div class="wrapper" style="height:100%"> | ||
<section role="alertdialog" tabindex="0" aria-labelledby="prompt-heading-id" aria-describedby="prompt-message-wrapper" class="slds-modal slds-fade-in-open slds-modal_prompt" aria-modal="true"> | ||
<div class="slds-modal__container"> | ||
<header class="slds-modal__header slds-theme_error slds-theme_alert-texture"> | ||
<h2 class="slds-text-heading_medium" id="prompt-heading-id">Delete {!deletableLogs.size} Logs</h2> | ||
</header> | ||
<div class="slds-modal__content slds-p-around_medium" id="prompt-message-wrapper"> | ||
<div class="slds-p-vertical_medium"> | ||
Are you sure that you want to delete these logs? | ||
</div> | ||
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped"> | ||
<thead> | ||
<tr> | ||
<th scope="col">{!$ObjectType.Log__c.Fields.Name.Label}</th> | ||
<th scope="col">{!$ObjectType.Log__c.Fields.LoggedBy__c.Label}</th> | ||
<th scope="col">{!$ObjectType.Log__c.Fields.StartTime__c.Label}</th> | ||
<th scope="col">{!$ObjectType.Log__c.Fields.TotalLogEntries__c.Label}</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<apex:repeat value="{!deletableLogs}" var="deletableLog"> | ||
<tr> | ||
<td><a href="{! '/' + deletableLog.Id}">{!deletableLog.Name}</a></td> | ||
<td><a href="{! '/' + deletableLog.LoggedBy__c}">{!deletableLog.LoggedBy__r.Name}</a></td> | ||
<td><apex:outputField value="{!deletableLog.StartTime__c}"/></td> | ||
<td>{!deletableLog.TotalLogEntries__c}</td> | ||
</tr> | ||
</apex:repeat> | ||
</tbody> | ||
</table> | ||
</div> | ||
<footer class="slds-modal__footer slds-theme_default"> | ||
<apex:form> | ||
<apex:commandButton action="{!cancel}" value="Cancel" styleClass="slds-button slds-button_neutral" /> | ||
<apex:commandButton action="{!deleteSelectedLogs}" onclick="showSuccessMessage();" value="Delete Logs" styleClass="slds-button slds-button_destructive" /> | ||
</apex:form> | ||
</footer> | ||
</div> | ||
</section> | ||
<div class="slds-backdrop slds-backdrop_open" /> | ||
</div> | ||
</apex:page> |
7 changes: 7 additions & 0 deletions
7
nebula-logger/main/log-management/pages/LogMassDelete.page-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexPage xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>51.0</apiVersion> | ||
<availableInTouch>false</availableInTouch> | ||
<confirmationTokenRequired>false</confirmationTokenRequired> | ||
<label>LogMassDelete</label> | ||
</ApexPage> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
nebula-logger/main/log-management/quickActions/Log__c.Manage.quickAction-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
nebula-logger/tests/log-management/classes/LogMassDeleteExtension_Tests.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//------------------------------------------------------------------------------------------------// | ||
// This file is part of the Nebula Logger project, released under the MIT License. // | ||
// See LICENSE file or go to https://github.com/jongpie/NebulaLogger for full license details. // | ||
//------------------------------------------------------------------------------------------------// | ||
|
||
@isTest | ||
private class LogMassDeleteExtension_Tests { | ||
|
||
@TestSetup | ||
static void setupData() { | ||
List<Log__c> logs = new List<Log__c>(); | ||
for (Integer i = 0; i < 10; i++) { | ||
Log__c log = new Log__c(TransactionId__c = 'TXN-' + i); | ||
logs.add(log); | ||
} | ||
insert logs; | ||
} | ||
|
||
@isTest | ||
static void it_should_return_deletable_logs() { | ||
List<Log__c> logs = [SELECT Id, Name FROM Log__c]; | ||
|
||
List<String> logIds = new List<String>(); | ||
for (Log__c selectedLog : logs) { | ||
logIds.add(selectedLog.Id); | ||
} | ||
|
||
List<Log__c> expectedDeletableLogs = new List<Log__c>(); | ||
for (UserRecordAccess recordAccess : [SELECT RecordId FROM UserRecordAccess WHERE UserId = :UserInfo.getUserId() AND RecordId IN :logIds AND HasDeleteAccess = true]) { | ||
expectedDeletableLogs.add(new Log__c(Id = recordAccess.RecordId)); | ||
} | ||
|
||
ApexPages.StandardSetController controller = new ApexPages.StandardSetController(logs); | ||
controller.setSelected(logs); | ||
|
||
PageReference pageReference = Page.LogMassDelete; | ||
Test.setCurrentPage(pageReference); | ||
|
||
Test.startTest(); | ||
|
||
LogMassDeleteExtension extension = new LogMassDeleteExtension(controller); | ||
List<Log__c> returnedDeletableLogs = extension.getDeletableLogs(); | ||
|
||
Test.stopTest(); | ||
|
||
System.assertEquals(expectedDeletableLogs.size(), returnedDeletableLogs.size()); | ||
} | ||
|
||
@isTest | ||
static void it_should_delete_selected_log_records() { | ||
List<Log__c> logs = [SELECT Id, Name FROM Log__c]; | ||
List<Log__c> logsToDelete = new List<Log__c>(); | ||
List<Log__c> logsToKeep = new List<Log__c>(); | ||
Integer numberToKeep = 3; | ||
for (Integer i = 0; i < logs.size(); i++) { | ||
if (i < numberToKeep) { | ||
logsToDelete.add(logs.get(i)); | ||
} else { | ||
logsToKeep.add(logs.get(i)); | ||
} | ||
} | ||
|
||
ApexPages.StandardSetController controller = new ApexPages.StandardSetController(logs); | ||
controller.setSelected(logsToDelete); | ||
|
||
PageReference pageReference = Page.LogMassDelete; | ||
Test.setCurrentPage(pageReference); | ||
|
||
Test.startTest(); | ||
|
||
LogMassDeleteExtension extension = new LogMassDeleteExtension(controller); | ||
extension.deleteSelectedLogs(); | ||
|
||
Test.stopTest(); | ||
|
||
// Verify that only the selected logs were deleted | ||
logsToDelete = [SELECT Id, IsDeleted FROM Log__c WHERE Id IN :logsToDelete ALL ROWS]; | ||
for (Log__c log : logsToDelete) { | ||
System.assertEquals(true, log.IsDeleted, log); | ||
} | ||
logsToKeep = [SELECT Id, IsDeleted FROM Log__c WHERE Id IN :logsToKeep ALL ROWS]; | ||
for (Log__c log : logsToKeep) { | ||
System.assertEquals(false, log.IsDeleted, log); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
nebula-logger/tests/log-management/classes/LogMassDeleteExtension_Tests.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>51.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |