Skip to content

Instantly share code, notes, and snippets.

@goravseth
Created June 24, 2020 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goravseth/48b5b41d7acb26b4ba5d2e0b28022cb0 to your computer and use it in GitHub Desktop.
Save goravseth/48b5b41d7acb26b4ba5d2e0b28022cb0 to your computer and use it in GitHub Desktop.
global class License_Upgrade implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC){
// Query all active users with Profile : 'Base Programs'
String query ='Select UsersId,Users.ProfileId,Users.UserPermissionsMarketingUser,Users.Name, LastModifiedDate,LoginType,createddate,SessionType From AuthSession Where LoginType = \'Application\' and SessionType = \'UI\' and createddate = today and lastmodifieddate = today Order by lastmodifieddate';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<AuthSession> AuthSesuserLst){
Profile p = [SELECT Id, Name FROM Profile WHERE Name= 'Base Programs' LIMIT 1];
Profile oldProfile = [SELECT Id, Name FROM Profile WHERE Name= 'Ashoka Platform User' LIMIT 1];
for(AuthSession uRec : AuthSesuserLst)
{
long crdate = uRec.createddate.getTime();
long lmdate = uRec.LastModifiedDate.getTime();
long diff = (lmdate - crdate)/(1000*60) ;
system.debug( ' time difference ' + diff );
if(diff < 4 && oldProfile.Id == uRec.Users.ProfileId ){ //p.id != uRec.Users.ProfileId &&
system.debug('user ' + uRec.users.name);
ProfileUpdate.updateProfile(p.id, urec.UsersId);
}
}
}
global void finish(Database.BatchableContext BC){
CronTrigger schClass = [SELECT Id,CronJobDetail.Name,CronJobDetail.Id,State FROM CronTrigger where CronJobDetail.Name='Upgrade license - every 5 minutes'AND State !='COMPLETE'];
System.debug('schClass ' + schClass);
system.abortJob(schClass.Id);
DateTime scheduleTime = Datetime.now().addMinutes(5) ;
system.debug('scheduleTime ' + scheduleTime);
String hour = String.valueOf(scheduleTime.hour() );
String min = String.valueOf(scheduleTime.minute() );
String ss = String.valueOf(scheduleTime.second());
system.debug('Datetime.now() ' + Datetime.now());
//parse to cron expression
String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';
system.debug('nextFireTime ' + nextFireTime);
Schedule_License_Upgrade sch = new Schedule_License_Upgrade();
System.schedule('Upgrade license - every 5 minutes', nextFireTime, sch);
}
}
/* public static Boolean checkIteration = true;
@future
public Static void upgradeMethod(){
// profile to upgrade
Profile p = [SELECT Id, Name FROM Profile WHERE Name= 'Base Programs' LIMIT 1];
Profile oldProfile = [SELECT Id, Name FROM Profile WHERE Name= 'Ashoka Platform User' LIMIT 1];
for(AuthSession uRec : [Select UsersId,Users.ProfileId,Users.UserPermissionsMarketingUser,Users.Name, LastModifiedDate,LoginType ,createddate ,SessionType From AuthSession
Where LoginType = 'Application' and SessionType = 'UI' and createddate = today and lastmodifieddate = today Order by lastmodifieddate])
{
long crdate = uRec.createddate.getTime();
long lmdate = uRec.LastModifiedDate.getTime();
long diff = (lmdate - crdate)/(1000*60) ;
system.debug( ' time difference ' + diff );
if(diff < 4 && oldProfile.Id == uRec.Users.ProfileId ){ //p.id != uRec.Users.ProfileId &&
system.debug('user ' + uRec.users.name);
ProfileUpdate.updateProfile(p.id, urec.UsersId);
}
}
try{
// Update userLst; // Update User records
}
catch(dmlException e){
System.debug('error ' + e);
}
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment