+ Start a Discussion
RahulRahul 

I have to send Email Alerts to those users whose Account has no Activity for more than than 15 days.

I have to send Email Alerts to users where no Account Activity greater than 15 days. I have a Field of No activity since which calculates it. It gives the count which I have made based on formula. Now my requirement is I need to send Email Alerts to salesperson where No activity since greater than 15. My logic is i have  created a text field Named Activitystatus field and created a work flow rule based on that text field named Activitystatus  . In the rule I have given criteria that  when Activitystatus equals NoActivity, send an Email Alert to Users.Now, Using the trigger i want to make the Activity status as No Activity for those accounts  where No activity since greater than 15. When Activity status Equals NoActivity the Workflow Rule will fire Automatically.

I have  used this Logic but its not Working.

trigger NoActivityAccount on Account(before insert) {
    
    List<Account>  objvrList  =[select Id,Activity_status__c from Account where No_Activity_since__c > 15];
    boolean flagetoUpdate = false ; 
    for(Account ac :trigger.new){
        if(ac.name != null)
        {
           flagetoUpdate = true ;   
        }
        
    }
    
    if(flagetoUpdate){
        for(Account acc : objvrList) {
            acc.Activity_status__c ='No Activity' ; 
        }
        update objvrList ;
    }
    
}
 
Best Answer chosen by Rahul
Gururaj BGururaj B
If you want to send email alert to all the existing inactive accounts then you need to run one time job something like below where i have assumed the email to be sent to the account owner. And then implment the process builder as i explained in my previous reply.
 
list<account> acc = [select Id,name,Owner.id,owner.email from account where No_Activity_since__c>15];
messaging.SingleEmailMessage myemail = new messaging.SingleEmailMessage();

for(account a:acc)
{
    list<string> emailid = new list<string>();
emailid.add((string)a.owner.email);
    myemail.setToAddresses(emailid);    
    myemail.setSubject('Inactive Account, '+a.name);
	myemail.setHtmlBody('Your Account is inactive for more than 15 days<br/>Account Id: '+a.id+'<br/> Account Name: '+a.name);
	messaging.sendemail(new list<messaging.SingleEmailMessage>{myemail});
}

 

All Answers

Gururaj BGururaj B
First of all i dont think any account will have no activity >15 the moment its created. So your trigger is executed only when a new account is created.(as you are using before insert only)
Secondly you are creating one more redundant field Activity_Status__c which is used just to indicate that Account is been inactive for 15 days which you can already get from your formula field.
Thirdly you can accomplish this entire requirement in process builder(Workflow) alone without the trigger.

Build process builder on Account when any update happens on account record. with criteria to check No_Activity_since__c>15 and then add immediate action to send email.

If it  helped you to solve your problem please mark as best answer
RahulRahul
I tried to achieve this using workflow but this was only working when a user Edited the record. I have not tried using process builder. We have old records in the system where there is no activity more than 15 days some has more than 25 days. So for those old records we cannot edit it then workflow fires. We want to automatically know by Email alert, which are those old  accounts where there is no activity for 15 days or more from a list of 2000+ accounts
Gururaj BGururaj B
If you want to send email alert to all the existing inactive accounts then you need to run one time job something like below where i have assumed the email to be sent to the account owner. And then implment the process builder as i explained in my previous reply.
 
list<account> acc = [select Id,name,Owner.id,owner.email from account where No_Activity_since__c>15];
messaging.SingleEmailMessage myemail = new messaging.SingleEmailMessage();

for(account a:acc)
{
    list<string> emailid = new list<string>();
emailid.add((string)a.owner.email);
    myemail.setToAddresses(emailid);    
    myemail.setSubject('Inactive Account, '+a.name);
	myemail.setHtmlBody('Your Account is inactive for more than 15 days<br/>Account Id: '+a.id+'<br/> Account Name: '+a.name);
	messaging.sendemail(new list<messaging.SingleEmailMessage>{myemail});
}

 
This was selected as the best answer