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 Client Account, please contact your Salesforce Administrator');
}
}
}
}









share|improve this question






























    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 Client Account, please contact your Salesforce Administrator');
    }
    }
    }
    }









    share|improve this question


























      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 Client Account, please contact your Salesforce Administrator');
      }
      }
      }
      }









      share|improve this question















      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 Client Account, please contact your Salesforce Administrator');
      }
      }
      }
      }






      apex trigger account record-type delete






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked Nov 28 at 18:24









      Kevin K

      312




      312






















          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');
          }
          }





          share|improve this answer




























            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');
            }

            }

            }
            }





            share|improve this answer



















            • 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
              Nov 30 at 13:33










            • @KevinK I updated code to optimize the performance.
              – Pranay Jaiswal
              Nov 30 at 13:37











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "459"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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');
            }
            }





            share|improve this answer

























              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');
              }
              }





              share|improve this answer























                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');
                }
                }





                share|improve this answer












                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');
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 at 18:31









                Sebastian Kessel

                8,68552136




                8,68552136
























                    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');
                    }

                    }

                    }
                    }





                    share|improve this answer



















                    • 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
                      Nov 30 at 13:33










                    • @KevinK I updated code to optimize the performance.
                      – Pranay Jaiswal
                      Nov 30 at 13:37















                    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');
                    }

                    }

                    }
                    }





                    share|improve this answer



















                    • 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
                      Nov 30 at 13:33










                    • @KevinK I updated code to optimize the performance.
                      – Pranay Jaiswal
                      Nov 30 at 13:37













                    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');
                    }

                    }

                    }
                    }





                    share|improve this answer














                    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');
                    }

                    }

                    }
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 30 at 13:36

























                    answered Nov 28 at 18:32









                    Pranay Jaiswal

                    12.1k32251




                    12.1k32251








                    • 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
                      Nov 30 at 13:33










                    • @KevinK I updated code to optimize the performance.
                      – Pranay Jaiswal
                      Nov 30 at 13:37














                    • 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
                      Nov 30 at 13:33










                    • @KevinK I updated code to optimize the performance.
                      – Pranay Jaiswal
                      Nov 30 at 13:37








                    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
                    Nov 30 at 13:33




                    Thank you very much - this is working great. Should I be worried about the CPU cost?
                    – Kevin K
                    Nov 30 at 13:33












                    @KevinK I updated code to optimize the performance.
                    – Pranay Jaiswal
                    Nov 30 at 13:37




                    @KevinK I updated code to optimize the performance.
                    – Pranay Jaiswal
                    Nov 30 at 13:37


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?