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









share|improve this question









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.


























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









    share|improve this question









    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.






















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









      share|improve this question









      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






      share|improve this question









      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.











      share|improve this question









      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.









      share|improve this question




      share|improve this question








      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.






















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





          share|improve this answer




























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

            }





            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
              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











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


            }
            });






            Kevin K is a new contributor. Be nice, and check out our Code of Conduct.










             

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





            share|improve this answer

























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





              share|improve this answer























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





                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 8 hours ago









                Sebastian Kessel

                8,64052136




                8,64052136
























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

                    }





                    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
                      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















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

                    }





                    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
                      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













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

                    }





                    share|improve this answer












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

                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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














                    • 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










                    Kevin K is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    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.















                     


                    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

                    Entries order in /etc/network/interfaces

                    新発田市

                    Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)