2

I have the following apex class:

public with sharing class AssignPermissionSet{
List<PermissionSetAssignment> p=new List<PermissionSetAssignment>();
private PermissionSet per;
private User u;
private Account acct;
private Boolean insertPermissionSet=false;
private Boolean deletePermissionSet=false;
public AssignPermissionSet(ApexPages.StandardController stdController)
{
Account account1 = (Account)stdController.getRecord();
RecordType rt=[SELECT Id,Name FROM RecordType where Name='Account Group'];
try
{
per=[SELECT Id,Name FROM PermissionSet where Name='Key_Acc_Manager_Custom_Attributes'];
}
catch(Exception e)
{
System.debug('The following exception has occurred:' + e.getMessage());
}

u=[select id,name,(Select Id from PermissionSetAssignments Where PermissionSetID = :per.id)  from user where id =:UserInfo.getUserId() AND IsActive = true];
if (Test.isRunningTest())//This will execute in test class
{
acct=account1;
//This condition will bypass account recordtype while executing test class
if(acct.KeyAccountManager__c!= null && acct.KeyAccountManager__c==u.Id )
{
 insertPermissionSet=true;
}
else 
{
deletePermissionSet = true;

}
}
else
{  
acct = [Select Id,Name,KeyAccountManager__c from Account where Id=:account1.Id AND RecordTypeId=:rt.Id];//this will execute on UI..
//This condition will run only for Accout Group
acct = [Select Id,Name,KeyAccountManager__c from Account where Id=:account1.Id];
if(acct.KeyAccountManager__c!= null && acct.KeyAccountManager__c==u.Id && acct.RecordType.Name=='Account Group')
{
 insertPermissionSet=true;
}
else 
{
deletePermissionSet = true;

}
}


}

public void getPermissionSet()
{
    if(u.PermissionSetAssignments.size() == 0 && insertPermissionSet)
    {  
    p.add( new PermissionSetAssignment(AssigneeId = u.id,PermissionSetId = per.Id) );
    insert p; 
    }
    else if(u.PermissionSetAssignments.size()>0 && deletePermissionSet)
    {
    PermissionSetAssignment per1 = [SELECT AssigneeId,PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId=:u.id AND PermissionSetId =: per.id];
    delete per1;
    }
}

}

This class assigns the permission to the user based on the certain condition &I have to remove the permission after certain interval of time for this I am trying to write a scheduler class like this:

global class schedulePermission implements Schedulable 
{

//PermissionSet per1=[SELECT Id,Name FROM PermissionSet where Name='Key_Acc_Manager_Custom_Attributes'];
//User u1=[select id,name,(Select Id from PermissionSetAssignments Where PermissionSetID = :per1.id)  from user where id =:UserInfo.getUserId() AND IsActive = true];
global static void scheduleP()
{
schedulePermission sp=new schedulePermission ();
String CRON_EXP = '0 5 * * * ? ';
system.schedule('Schedule Job',CRON_EXP,sp);
}
global void execute(SchedulableContext sc)
{
AssignPermissionSet  APS=new AssignPermissionSet();
APS.getPermissionSet();
}

}

but when I call the apex class AssignPermissionSet APS=new AssignPermissionSet(); in the scheduler class then it is giving me an error:

Constructor not defined: [AssignPermissionSet].()

Can somebody help me what syntax i have missed her &how to call the apex class and its method which deletes the permssion? Really Appreciate your suggession on this...

  • 3
    or you construct instance of class and call regular method of it or you create static method of class and call that without instance. Problem is that you are not providing account to your controller – kurunve May 30 '16 at 15:09
  • 1
    Dhananjay, when you paste code into a post, you need to select it and click on the preformatted text icon { } which moves every line over by four spaces for you so it will display properly. – crmprogdev May 30 '16 at 15:12
  • 2
    This is because you only have a constructor that expects an argument AssignPermissionSet(ApexPages.StandardController stdController). As kurunve suggests, best to modify the controller class so it is simpler to use from the schedulable. – Keith C May 30 '16 at 15:14
  • @ Keith C @kurunve just wanted to confirm whether it is affected to my existing functionality.The main reason that I am writing schedule class is because when the permission set is assign to the user it will remove only in the certain condition but when I goto the list view of account then The permission is still assign to the user which cause delete enable for the standard user on list view which I want to avoid which should not impact on other record because the permission set that i am assigning has modify all permission on account object. – Dhananjay Patil May 30 '16 at 15:23
  • Separation of Concerns would suggest you create a common service class that does the permissions work and that class is called by both the async class and the controller class. Suppose you wanted to have Process Builder (invocable methods) execute the permissions service? or a REST service? or a web service? – cropredy May 30 '16 at 21:15
1

the only constructor your Account controller extension has is

public AssignPermissionSet(ApexPages.StandardController stdController)

so it requires an stdController as input. Therefore

schedulePermission sp=new schedulePermission ();

will not work. There are 2 solutions:
- you can create another constructor in your controller extension which will work without input parameter
- if you really do not want to touch the ControllerExtenstion, you can create dummy standard Controller Record in your Scheduler class as following:

Account acc= new Account();
ApexPages.StandardController stdAcc = new ApexPages.StandardController(acc);
AssignPermissionSet  APS=new AssignPermissionSet(stdAcc);

The real problem, however, as it is mentioned in comments - there is no connection between your Scheduler and ControllerExtension. As a result -Scheduler will have no idea which Account to process, I used New Account just to show you syntax for your current problem.

| improve this answer | |
  • Thanks Everyone for your suggession....@Andrei as per ur suggestion I have created another constructor and also created a new method inwhich I am querying the user and permission and call that method within constructors..I have run the schedule job on developer console and it is working as expected but how do I call the schedule class? As permy understanding when i click on schedule apex button under apex class then it works when I have to run the job on monthly/weekly basis..but can call my schedule class so that it calls after every 15 mins? – Dhananjay Patil May 31 '16 at 8:54
  • you can do that easily with execute anonymous , see here : salesforce.stackexchange.com/questions/37333/… – Andrei.Z May 31 '16 at 15:44

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.