function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bannubannu 

Too many email invocations

hi all,

i am not able to send emails more than 10

here is my cose with out using batch class can we send emails or else please tell me how to use batch class??

 

public class Weekly_Plan
{
   
    
    
    public Weekly_Plan(ApexPages.StandardController controller)
    {

    }
   /*public String LoggedInUSer = UserInfo.getUserName();    
    public String getUserName()
    {
        return UserInfo.getFirstName()+' '+UserInfo.getLastName();
    }*/    
    public Weekly_Plan__c WeeklyPlan = new Weekly_Plan__c();
    
    public Weekly_Plan__c getuser()
    {
        return WeeklyPlan;
    }
    public void setuser(String name)
    {
        WeeklyPlan.Assinged_To__c = name;
    }
    
    public Weekly_Plan__c getTodate()
    {
        return WeeklyPlan;
    }
    public void setTodate(Date d)
    {
        WeeklyPlan.To_Date__c = d;
    }
    
    public Weekly_Plan__c getFromdate()
    {
        return WeeklyPlan;
    }
    public void setFromdate(Date d)
    {
        WeeklyPlan.From_Date__c = d;
    }
    
    public Plan_Info__c PlanInfo = new Plan_Info__c();
    
    public Plan_Info__c getLocation()
    {
        return PlanInfo;
    }
    public void setLocation(String s)
    {
        PlanInfo.Location__c = s;
    }
    
    public Plan_Info__c getDescription()
    {
        return PlanInfo;
    }
    public void setDescription(String s)
    {
        PlanInfo.description__c = s;
    }
    
    public Plan_Info__c getPlanDate()
    {
        return PlanInfo;
    }
    public void setPlanDate(Date d)
    {
        PlanInfo.Date__c = d;
    }
    
    public List<InnerPlans> PlanInfoList = new List<InnerPlans>();
    public Integer i = 0;
    public void AddPlans()
    {
        if(PlanInfo.Location__c==null || PlanInfo.Description__c==null|| PlanInfo.Date__c==null)
        {
           ApexPages.Message msgAddr = new ApexPages.Message(ApexPages.severity.Error,'Please Enter the Required Fields');
           ApexPages.addMessage(msgAddr);
           
        }
        else
        {
            InnerPlans InnerRef = new InnerPlans();
            InnerRef.plans = PlanInfo;
            InnerRef.UniqueId = i;
            
            PlanInfoList.add(InnerRef);
           
            i++;
            PlanInfo = new Plan_Info__c();
        }
    }
    
    public List<InnerPlans> getPlanInfoList()
    {
        return PlanInfoList;
    }
    
    public class InnerPlans
    {
        public Plan_Info__c plans{get;set;}
        public Integer UniqueId{get;set;}
    }
    public boolean getDisplayPlans()
    {
        if(PlanInfoList.size()>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void DeletePlan()
    {
    Integer k = 0;
    boolean b = true;
        for(InnerPlans IP : PlanInfoList)
        {
            if(IP.UniqueId==Integer.valueOf(System.CurrentPageReference().getParameters().get('DelRec')))
            {
                
                b=false;
                break;
            }
            k++;
        }
        if(!b)
        {
            PlanInfoList.remove(k);
        }
    }
     
    
     
  public Weekly_Plan()
  {    
        
  }   
    public PageReference Submit()
    {
         if(WeeklyPlan.To_Date__c==null || WeeklyPlan.From_Date__c==null)
         {
           ApexPages.Message msgAddr = new ApexPages.Message(ApexPages.severity.Error,'Please Enter the Required Fields');
           ApexPages.addMessage(msgAddr);
           return null;
         }
         else
         {
            //WeeklyPlan.Assinged_To__c = UserInfo.getUserId();
            insert WeeklyPlan;
            List<Plan_Info__c> insertPlanInfo = new List<Plan_Info__c>();
            List<Task> TaskList = new List<Task>();
            for(InnerPlans IPs : PlanInfoList)
            {
            Task t=new Task();
                    IPs.Plans.Weekly_Plan__c = WeeklyPlan.id;
                    insertPlanInfo.add(IPs.Plans);
                    t.OwnerId=WeeklyPlan.Assinged_To__c;
                    t.to_date__c=WeeklyPlan.To_Date__c;
                    t.From_date__c=WeeklyPlan.From_Date__c;
                    t.Description=IPs.Plans.Description__c;
                    t.Subject=IPs.Plans.Location__c;
                    t.ActivityDate=IPs.Plans.Date__c;
                    TaskList.add(t);
                    
            }
            insert insertPlanInfo;
            insert TaskList;
            SendEmail();
            /*      
                  
                    insert t;
            */                   
            return new PageReference('/apex/WeeklyPlanView_VF?Id='+WeeklyPlan.id);
        }
        
    }  
   
    public void SendEmail()
    {
        String assignedTo = '';
        Date date_details=null;
        String subject='';
        String description='';
        String Email_deet='';
        String Name_det='';
        
        List<Plan_Info__c> detail_weeklyplan=[select Date__c,Description__c,Location__c,Weekly_Plan__r.Assinged_To__c from Plan_Info__c where Weekly_Plan__c =: WeeklyPlan.id limit 1];
        System.debug('iii'+detail_weeklyplan);
        
        for(Plan_Info__c p : detail_weeklyplan)
        {
            assignedTo=p.Weekly_Plan__r.Assinged_To__c;
            System.debug('uuu'+assignedTo);
            date_details=p.Date__c;
            subject=p.Location__c;
            description=p.Description__c;
            
            List<User> user_Details=[select Name,Email from User where id =: assignedTo];
            System.debug('idid'+user_Details);
            
            for(User u : user_Details)
            {
                Name_det=u.Name;
                Email_deet=u.Email;
                System.debug('eeee'+Email_deet);
            if(Email_deet!=null)
            {
                Messaging.SingleEmailMessage emaill = new Messaging.SingleEmailMessage();
                string [] toaddress= New string[]{Email_deet};
                emaill.setSubject('Weekly Plan Info');
                
                emaill.setHtmlBody('<html>' + '<h4>'+'Hello      ' +Name_det+ ','+'</h4>' + '<br/>' + 'Weekly Plan Information'+','+ '<br/>'+'<br/>' +'<body align="center">'+'<table border="1" align="center">' + '<th><u>Date</u></th>' + '<th><u>Subject</u></th>' +'<th><u>description</u></th>'+ ' <tr align="center">'+'<td>'+date_details+'</td>'+'<td>'+subject+'</td>'+'<td>'+description+'</td>'+'</tr>' + '</table>' + '</body>' + '</html>');
                emaill.setToAddresses(toaddress);
                Messaging.sendEmail(New Messaging.SingleEmailMessage[]{emaill});
            }
            }
    }
    
  }  
    public PageReference Cancel()
    {
        return new PageReference('/a0E/o');
    }
}

RadnipRadnip
You can't send more than 10 emails in one transaction, is a limitation in Salesforce. Either queue the messages or use an external email provider, there are loads listed in appExchange.
bannubannu

how it is possible.

actually some of them providing the batch apex class inorder to send bulk emails.

but i am not able to understand that and cant use that code to my code .

 

so if you can, please help.

RadnipRadnip
Yes you could use a batch, but if you can't use that then I would use anther service, or create a web service on heroku or something so you can send emails to be processed t that.
bannubannu

can u tell me the other service.

as i was new to salesforce i mean fresher..

suggest some code please so that i can understand how to do and what to do.

thank you