Revoke access to delete a specific Account record type
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}
up vote
6
down vote
favorite
I have this apex trigger that revokes access to users from deleting all Accounts. I was wondering how to improve it to ignore or allow certain record type. The use case is, I want to allow users to delete 'prospect' records but not allow them to delete 'client' accounts. I'm not a developer and this is a pet project - I got this far from a code a found online for revoking access from deleting email-messages.
global class PreventClientAccountDelete{
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
apex trigger account record-type delete
New contributor
add a comment |
up vote
6
down vote
favorite
I have this apex trigger that revokes access to users from deleting all Accounts. I was wondering how to improve it to ignore or allow certain record type. The use case is, I want to allow users to delete 'prospect' records but not allow them to delete 'client' accounts. I'm not a developer and this is a pet project - I got this far from a code a found online for revoking access from deleting email-messages.
global class PreventClientAccountDelete{
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
apex trigger account record-type delete
New contributor
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have this apex trigger that revokes access to users from deleting all Accounts. I was wondering how to improve it to ignore or allow certain record type. The use case is, I want to allow users to delete 'prospect' records but not allow them to delete 'client' accounts. I'm not a developer and this is a pet project - I got this far from a code a found online for revoking access from deleting email-messages.
global class PreventClientAccountDelete{
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
apex trigger account record-type delete
New contributor
I have this apex trigger that revokes access to users from deleting all Accounts. I was wondering how to improve it to ignore or allow certain record type. The use case is, I want to allow users to delete 'prospect' records but not allow them to delete 'client' accounts. I'm not a developer and this is a pet project - I got this far from a code a found online for revoking access from deleting email-messages.
global class PreventClientAccountDelete{
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
apex trigger account record-type delete
apex trigger account record-type delete
New contributor
New contributor
edited Nov 28 at 18:48
Jayant Das
10.6k2522
10.6k2522
New contributor
asked Nov 28 at 18:24
Kevin K
312
312
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
Kevin,
You want to obtain the recordtype involved first, you can do it this way
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
Then, you can add the following clause to the if statement
currentAccount.RecordTypeId != prospectRecordTypeId
Your code will end up like this
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(currentAccount.RecordTypeId != prospectRecordTypeId && profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Client Account, please contact your Salesforce Administrator');
}
}
add a comment |
up vote
1
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(clientRecordTypeId == currentAccount.RecordTypeId){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
2
One should not put a describe call in a loop. There is an associated CPU cost scales with the number of record types and fields an object has.
– sfdcfox
Nov 28 at 19:13
Thanks @sfdcfox. I never thought to describe calls are so resource intensive. I remember there was a limit on a number of describe calls, but they were lifted a few years back.
– Pranay Jaiswal
Nov 28 at 19:16
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
Nov 28 at 21:04
Thank you very much - this is working great. Should I be worried about the CPU cost?
– Kevin K
2 days ago
@KevinK I updated code to optimize the performance.
– Pranay Jaiswal
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Kevin,
You want to obtain the recordtype involved first, you can do it this way
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
Then, you can add the following clause to the if statement
currentAccount.RecordTypeId != prospectRecordTypeId
Your code will end up like this
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(currentAccount.RecordTypeId != prospectRecordTypeId && profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Client Account, please contact your Salesforce Administrator');
}
}
add a comment |
up vote
4
down vote
Kevin,
You want to obtain the recordtype involved first, you can do it this way
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
Then, you can add the following clause to the if statement
currentAccount.RecordTypeId != prospectRecordTypeId
Your code will end up like this
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(currentAccount.RecordTypeId != prospectRecordTypeId && profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Client Account, please contact your Salesforce Administrator');
}
}
add a comment |
up vote
4
down vote
up vote
4
down vote
Kevin,
You want to obtain the recordtype involved first, you can do it this way
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
Then, you can add the following clause to the if statement
currentAccount.RecordTypeId != prospectRecordTypeId
Your code will end up like this
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(currentAccount.RecordTypeId != prospectRecordTypeId && profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Client Account, please contact your Salesforce Administrator');
}
}
Kevin,
You want to obtain the recordtype involved first, you can do it this way
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
Then, you can add the following clause to the if statement
currentAccount.RecordTypeId != prospectRecordTypeId
Your code will end up like this
Id prospectRecordTypeId = Account.SObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Prospect').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(currentAccount.RecordTypeId != prospectRecordTypeId && profileName !='System Administrator' && profileName !='Integration Administrator'){
currentAccount.addError('You can not delete a Client Account, please contact your Salesforce Administrator');
}
}
answered Nov 28 at 18:31
Sebastian Kessel
8,65052136
8,65052136
add a comment |
add a comment |
up vote
1
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(clientRecordTypeId == currentAccount.RecordTypeId){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
2
One should not put a describe call in a loop. There is an associated CPU cost scales with the number of record types and fields an object has.
– sfdcfox
Nov 28 at 19:13
Thanks @sfdcfox. I never thought to describe calls are so resource intensive. I remember there was a limit on a number of describe calls, but they were lifted a few years back.
– Pranay Jaiswal
Nov 28 at 19:16
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
Nov 28 at 21:04
Thank you very much - this is working great. Should I be worried about the CPU cost?
– Kevin K
2 days ago
@KevinK I updated code to optimize the performance.
– Pranay Jaiswal
2 days ago
add a comment |
up vote
1
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(clientRecordTypeId == currentAccount.RecordTypeId){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
2
One should not put a describe call in a loop. There is an associated CPU cost scales with the number of record types and fields an object has.
– sfdcfox
Nov 28 at 19:13
Thanks @sfdcfox. I never thought to describe calls are so resource intensive. I remember there was a limit on a number of describe calls, but they were lifted a few years back.
– Pranay Jaiswal
Nov 28 at 19:16
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
Nov 28 at 21:04
Thank you very much - this is working great. Should I be worried about the CPU cost?
– Kevin K
2 days ago
@KevinK I updated code to optimize the performance.
– Pranay Jaiswal
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(clientRecordTypeId == currentAccount.RecordTypeId){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
You have to add an extra if clause, to check the record being deleted is not client record type.
public static void PreventClientAccountDelete(Account pAccount, Account pOldAccount) {
//Client Account deletion is only allowed for administrator
Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId();
String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
for(Account currentAccount : pOldAccount) {
//Check if current user is not a system administrator
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(clientRecordTypeId == currentAccount.RecordTypeId){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
}
}
edited 2 days ago
answered Nov 28 at 18:32
Pranay Jaiswal
11.7k32051
11.7k32051
2
One should not put a describe call in a loop. There is an associated CPU cost scales with the number of record types and fields an object has.
– sfdcfox
Nov 28 at 19:13
Thanks @sfdcfox. I never thought to describe calls are so resource intensive. I remember there was a limit on a number of describe calls, but they were lifted a few years back.
– Pranay Jaiswal
Nov 28 at 19:16
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
Nov 28 at 21:04
Thank you very much - this is working great. Should I be worried about the CPU cost?
– Kevin K
2 days ago
@KevinK I updated code to optimize the performance.
– Pranay Jaiswal
2 days ago
add a comment |
2
One should not put a describe call in a loop. There is an associated CPU cost scales with the number of record types and fields an object has.
– sfdcfox
Nov 28 at 19:13
Thanks @sfdcfox. I never thought to describe calls are so resource intensive. I remember there was a limit on a number of describe calls, but they were lifted a few years back.
– Pranay Jaiswal
Nov 28 at 19:16
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
Nov 28 at 21:04
Thank you very much - this is working great. Should I be worried about the CPU cost?
– Kevin K
2 days ago
@KevinK I updated code to optimize the performance.
– Pranay Jaiswal
2 days ago
2
2
One should not put a describe call in a loop. There is an associated CPU cost scales with the number of record types and fields an object has.
– sfdcfox
Nov 28 at 19:13
One should not put a describe call in a loop. There is an associated CPU cost scales with the number of record types and fields an object has.
– sfdcfox
Nov 28 at 19:13
Thanks @sfdcfox. I never thought to describe calls are so resource intensive. I remember there was a limit on a number of describe calls, but they were lifted a few years back.
– Pranay Jaiswal
Nov 28 at 19:16
Thanks @sfdcfox. I never thought to describe calls are so resource intensive. I remember there was a limit on a number of describe calls, but they were lifted a few years back.
– Pranay Jaiswal
Nov 28 at 19:16
1
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
Nov 28 at 21:04
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
Nov 28 at 21:04
Thank you very much - this is working great. Should I be worried about the CPU cost?
– Kevin K
2 days ago
Thank you very much - this is working great. Should I be worried about the CPU cost?
– Kevin K
2 days ago
@KevinK I updated code to optimize the performance.
– Pranay Jaiswal
2 days ago
@KevinK I updated code to optimize the performance.
– Pranay Jaiswal
2 days ago
add a comment |
Kevin K is a new contributor. Be nice, and check out our Code of Conduct.
Kevin K is a new contributor. Be nice, and check out our Code of Conduct.
Kevin K is a new contributor. Be nice, and check out our Code of Conduct.
Kevin K is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f240865%2frevoke-access-to-delete-a-specific-account-record-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown