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...
{ }which moves every line over by four spaces for you so it will display properly. – crmprogdev May 30 '16 at 15:12AssignPermissionSet(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