I am creating a button that gives some users the ability to delete opportunities. The big caveat is that they need to be able to delete the opps where the owner is in the same role.
I changed my sharing on the class, so it's public without sharing class OpportunityController
however, when the user tries to delete a record owned by someone in the same role, they get insufficient privilages!
Why doesn't without sharing fix this? Also, what's the workaround?
public without sharing class OpportunityController {
public String oppId;
public Opportunity opp;
public OpportunityController(ApexPages.StandardController controller) {
this.oppId = ApexPages.currentPage().getParameters().get('id');
string url = ApexPages.currentPage().getUrl() ;
this.opp = [select id from opportunity where id = :this.oppId];
}
public pageReference custDelete(){
String kurt = '005a0000007Eg1m';
String boyden = '005a0000008V2et';
boolean isSysAdmin = false;
String sysAdmin = userInfo.getProfileId();
if ('00e30000000bulF' == sysAdmin.substring(0,15)){
isSysAdmin = true;
}
PageReference p;
if( userinfo.getuserid()!= kurt && userinfo.getuserid() != boyden && !isSysAdmin){
system.debug('dont delete');
// insuff priv
return null;
}else{
system.debug('delete');
p = new pageReference('/'+this.opp.id);
delete this.opp ;
return p;
}
}
userInfo.getUserId()
will return 18 character IDs which you are comparing against 15 character Ids – cropredy Jul 7 '14 at 16:38