0

i am trying to add custom error through trigger as if profile is system admin user cannot able to delete account. everything is working fine but i am not getting that custom error i had written, i am not going inside if block please suggest what changes i have to make .


public Static void Profiledel  ( List <Account>  alist){

        system.debug('Data in Alist'+alist);

Map<Id, Profile> Mlist = new map<Id, Profile>([Select Id, Name From Profile]);

        system.debug(Mlist);

        for(Account aa : alist){

            system.debug('in for');

            **system.debug(Mlist.values().Name);

            If(Mlist.values().Name=='System Administrator'){

                system.debug('in if'+Mlist.values().Name);

                aa.adderror('you cannot delete this record');
            }** 
        }

3 Answers 3

2

Welcome to Salesforce Stack Exchange (SFSE)!

Caspar answered the question that needed to be answered, but to answer the question you asked - about not getting inside your if block:

Mlist is a map

Documentation states that the .values() method for the Map class

Returns a list that contains all the values in the map.

[Emphasis added by me.]

So your statement: Mlist.values().Name is attempting to get the Name value from a List of Profiles. That will not work - unless there is only one Profile in your Mlist (which is highly unlikely since, for some reason, you retrieve every Profile your org into the Map [which is another issue with your code, but Caspar's answer takes care of that]).


I am actually surprised that you are not seeing a

System.QueryException: List has more than 1 row for assignment to SObject

error on this line: system.debug(Mlist.values().Name);


So like you are doing with the List of Accounts - iterating over them in a for loop - you could iterate over the Map values and find the "System Administrator" one...

for (Profile prof : Mlist.values()) {
    if prof.Name = 'System Administrator' {
        // do something
    }
}

...but that is very clunky.

Better would be to limit your query...

Map<Id, Profile> Mlist = new map<Id, Profile>([SELECT Id, Name FROM Profile WHERE Name = 'System Administrator' LIMIT 1]);

...but since you never use the Map as a Map, there is no need for that, either.


Caspar's answer is the route you should go. I added this answer to try to explain some of the foundational/underlying issues that are in the code the way that it is.

2

I'm not 100% sure what you are trying to do, but try this:

Profile p = [SELECT Id, Name FROM Profile WHERE Id = :UserInfo.getProfileId() LIMIT 1];
Boolean isSysAdmin = (p.Name == 'System Administrator');
if (isSysAdmin){
  for(Account aa : alist){
    aa.adderror('you cannot delete this record');
  }
}

You only need to establish if the user is a Sys Admin, if so, you can set an error to all the accounts.

Why can't the Sys Admin delete accounts?

If you really need this, you could clone the System Admin profile and set all System Admins to the cloned version - then remove Account delete privs.

4
  • 2
    Boolean isSysAdmin = (p.Name == 'System Administrator' ? true : false); can be just Boolean isSysAdmin = p.Name == 'System Administrator'. Boolean comparisons are already Boolean.
    – sfdcfox
    Jul 2 at 13:38
  • Good point, changed. Jul 2 at 19:53
  • 1
    Whilst that is true, the first time I ever came across that in someone's code it took me quite some time to figure out what it was doing. And only then most likely because I web searched and found it mentioned here. To me It's harder to quickly/easily read.
    – Moonpie
    Jul 4 at 1:28
  • That is the main reason I write longer form code - including this example Jul 4 at 1:34
0

My Understanding is We need to restrict the deletion of Account for System Administrator

If you want still use Map, look into the below code changes

public Static void Profiledel(List < Account > alist) { 
   
        system.debug('Data in Alist' + alist);
        //Why we need to check all profiles instead of System Administrator
        Map < Id, Profile > sysAdminProfileMap = new map < Id, Profile > ([SELECT Id, Name
                                                                           FROM Profile
                                                                           WHERE Name = 'System Administrator'
                                                                          ]);
        
        Id loggedInUserProfileId = UserInfo.getProfileId();
        
        system.debug('sysAdminProfileMap => ' + sysAdminProfileMap.keySet() + ' loggedInUserProfileId => ' + loggedInUserProfileId);
        
        for (Account aa: alist) {
            
            system.debug('in for');
            
            If(sysAdminProfileMap.containsKey(loggedInUserProfileId)) {
                
                aa.addError('you cannot delete this record');
                break;
            }
        }
    }

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.