+ Start a Discussion
Sunny SSunny S 

Automate Email Notification To Not Logged In Users

Hi friends, need very very urgent help please !

Requirement: 
Is there a way to automate email notification alert to get triggered for user who have not logged in for past 20 days?

Basically, an email should automatically get fired to notify users about being not logging in for XYZ days.

I tried working with WFR and PB but failed to get over, as there won't be any action on these user accounts to trigger the automation.

I am sure that this could be acheived using Apex, but my lack of experince with coding is the biggest hurdle. 

Can anyone please please help me in getting this sorted step by step please, as this has come up as a very urgent piece of work, and failing to do this could get me fired.

Thanks in advance !
Best Answer chosen by Sunny S
Danish HodaDanish Hoda
Hi Sunny,
Not a proble, we are here to help :)

Create an Apex class like below, this will be called a Batch class:
global class SearchAndReplace implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
	    DateTime dt= System.now().addDays(-20);
        String strQuery = 'SELECT Id, LastLoginDate FROM User WHERE LastLoginDate <='+dt.format('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ');
		return Database.getQueryLocator(strQuery);
   }

   global void execute(Database.BatchableContext BC, List<Lead> scope){
     for(sobject s : scope){
		// Use MassEmailMessage/SingleMEailMessage to send emails to the users
     }
    }

   global void finish(Database.BatchableContext BC){
   }
}

For sending mails to users, refer https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_mass.htm

All Answers

Danish HodaDanish Hoda
Hi Sunny,

You can achieve this by creating a batch and use query below in your start() method, the result (list of users) returned would be used to send MassEmailMessage in the execute() method:
 
DateTime dt= System.now().addDays(-20);
        String str = 'SELECT Id, LastLoginDate FROM User WHERE LastLoginDate <='+dt.format('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ');
        database.query(str);

 
Sunny SSunny S
Hi Danish, thanks a lot for your reply.

Please pardon me in advance for my kiddish query... but what would be the steps to follow your instructions here, in order to acheive the result?

Step 1: Should i create an Apex class ?
Step2: And use this query where ? 

trust me i have never worked on Developer Console and have zero coding knowledge.
Sunny SSunny S
Hi Danish, also what would be the :
1: Instance Variable here?
2: Constructor ?
3: Start () method --- can write your advised query
4: Excute () method

Please save me from getting fired !

Thanks a lot
 
Danish HodaDanish Hoda
Hi Sunny,
Not a proble, we are here to help :)

Create an Apex class like below, this will be called a Batch class:
global class SearchAndReplace implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
	    DateTime dt= System.now().addDays(-20);
        String strQuery = 'SELECT Id, LastLoginDate FROM User WHERE LastLoginDate <='+dt.format('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ');
		return Database.getQueryLocator(strQuery);
   }

   global void execute(Database.BatchableContext BC, List<Lead> scope){
     for(sobject s : scope){
		// Use MassEmailMessage/SingleMEailMessage to send emails to the users
     }
    }

   global void finish(Database.BatchableContext BC){
   }
}

For sending mails to users, refer https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_mass.htm
This was selected as the best answer
Sunny SSunny S
Thanks so much Danish, your advice worked wonders !
Realy apprecaite your time and support in getting me through. kind regards!