Revoke access to delete a specific Account record type
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}
up vote
5
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
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
5
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
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
5
down vote
favorite
up vote
5
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
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 7 hours ago
Jayant Das
10.3k2522
10.3k2522
New contributor
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 8 hours ago
Kevin K
262
262
New contributor
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Kevin K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
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
0
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId()==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
7 hours ago
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
7 hours ago
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
5 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
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
3
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
3
down vote
up vote
3
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 8 hours ago
Sebastian Kessel
8,64052136
8,64052136
add a comment |
add a comment |
up vote
0
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId()==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
7 hours ago
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
7 hours ago
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
5 hours ago
add a comment |
up vote
0
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId()==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
7 hours ago
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
7 hours ago
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
5 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
You have to add an extra if clause, to check the record being deleted is not client record type.
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId()==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.
if(profileName !='System Administrator' && profileName !='Integration Administrator'){
if(Schema.SObjectType.Account.getRecordTypeInfosByName().get('client').getRecordTypeId()==currentAccount.RecordTypeId){
currentAccount.addError('You can not delete a Clieant Account, please contact your Salesforce Administrator');
}
}
answered 8 hours ago
Pranay Jaiswal
11.4k31951
11.4k31951
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
7 hours ago
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
7 hours ago
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
5 hours 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
7 hours ago
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
7 hours ago
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
5 hours 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
7 hours ago
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
7 hours ago
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
7 hours ago
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
7 hours ago
1
1
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
5 hours ago
Yes, they were made unlimited, but they still cost CPU time, so we should always cache results for performance.
– sfdcfox
5 hours 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.
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