Mar 21, 2011

Issue With Ajax In IE9 (No such interface supported)


Hi All,


Today I found something very interesting and would like to share with you all.


I was facing error "No such interface supported" when I hit button on my custom page in IE9, but it was working properly on IE8. On click of this button controller method is called using AJAX, after that page messages is rendered. Here is the visualforce code :
<apex:page controller="IE9IssueController">
    <apex:form >

        <apex:pageMessages id="PM" />

        <apex:pageBlock title="IE9 Issue Page">

            <apex:pageBlockButtons >
                <apex:commandButton value="Hit" action="{!Hit}" reRender="PM"/>
            </apex:pageBlockButtons>
            
            <apex:outputLabel value="Hit the button, if info message displays that means action function is completed successfully else problem occured."/>
                        
        </apex:pageBlock>
    </apex:form>
</apex:page>

And here is my apex code :
public class IE9IssueController
{
    public IE9IssueController()
    {
    }
    
    public void Hit()
    {
        Apexpages.addMessage(new Apexpages.Message(ApexPages.Severity.INFO, 'You clicked the button!'));
    }
}
Now I was unable to find the exact problem but was able to get the work around of it. This line put the browser in IE8 mode and page message get rendered properly in IE9 also.
Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
Thanks

Mar 7, 2011

How to Send More than 10 E-mails


Hi All,


This is about how to send more than 10 E-mails. As we know we have a governor limit of sending 10 emails in a process so this is a workaround to send more than E-mails. We can do this by using batch class.


Lets say we have to send all System Administrators details of case logged by them today in a particular format. If suppose we have 15 System Administrators in our organisation, all of them logged 2-3 cases in a day and in the end of the day we want to send the details of case logged only by them in an Email. So here is the code :

global class Batch_CaseEmail implements Database.Batchable<sObject>,Database.Stateful
{
   Map<Id , List<Case>> userCaseMap {get; set;}
   List<Case> allCaseLoggedToday {get; set;}
   
   global Batch_CaseEmail()
   {
       //Map to maintain user id and cases logged by them today
       userCaseMap = new Map<Id, List<Case>>() ;
       
       //All sales rep (System admins)
       List<User> salesRep = new List<User>() ;
       salesRep = [select id , name , Email , ManagerId from User where Profile.Name = 'System Administrator'] ;
       
       //All sales rep ids
       List<Id> salesIds = new List<Id>() ;
       for(User ur : salesRep)
       {
           salesIds.add(ur.Id) ;
       }
      
       //All cases logged today by sales rep
       allCaseLoggedToday = new List<Case>() ;
       allCaseLoggedToday = [select Id, CaseNumber,CreatedById, Owner.name , account.Name , contact.name from Case where CreatedDate = TODAY AND CreatedById in : salesIds] ;
   }

   global Database.QueryLocator start(Database.BatchableContext BC)
   {
      //Creating map of user id with cases logged today by them
      for(Case c : allCaseLoggedToday)
      {
          if(userCaseMap.containsKey(c.CreatedById))
          {
              //Fetch the list of case and add the new case in it
              List<Case> tempList = userCaseMap.get(c.CreatedById) ;
              tempList.add(c);
              //Putting the refreshed case list in map
              userCaseMap.put(c.CreatedById , tempList) ;
          }
          else
          {
              //Creating a list of case and outting it in map
              userCaseMap.put(c.CreatedById , new List<Case>{c}) ;
          }
      }

      //Batch on all system admins (sales rep)
      String query = 'select id , name , Email from User where Profile.Name = \'System Administrator\'';
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope)
   {
       
      for(Sobject s : scope)
      {
          //Type cast sObject in user object
          User ur = (User)s ;
          
          //If system admin has logged any case today then only mail will be sent
          if(userCaseMap.containsKey(ur.Id))
          {
              //Fetching all cases logged by sys admin
              List<Case> allCasesOfSalesRep = userCaseMap.get(ur.Id) ;
              
              String body = '' ;
              //Creating tabular format for the case details
              body = BodyFormat(allCasesOfSalesRep) ;
              
              //Sending Mail
              Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
              
              //Setting user email in to address
              String[] toAddresses = new String[] {ur.Email} ;
              
              // Assign the addresses for the To and CC lists to the mail object
              mail.setToAddresses(toAddresses) ;
    
              //Email subject to be changed
              mail.setSubject('New Case Logged');
              
              //Body of email
              mail.setHtmlBody('Hi ' + ur.Name + ',<br/><br/> Details of Cases logged today is as follows : <br/><br/>' + body + '<br/> Thanks');
        
              //Sending the email
              Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
          }
      }
   }
   
   public String BodyFormat(List<Case> lst)
   {
       String str = '' ;
       for(Case cs : lst)
       {
           str += '<tr><td>'+ cs.CaseNumber +'</td>'+'<td>'+ cs.Owner.Name +'</td>'+'<td>'+ cs.Account.Name +'</td>'+'<td>'+ cs.Contact.Name +'</td>'+'</tr>' ;
       }
       str = str.replace('null' , '') ;
       String finalStr = '' ;
       finalStr = '<table border="1"> <td> CaseNumber </td> <td> Owner </td> <td> Account </td> <td> Contact </td> '+ str +'</table>' ;
       return finalStr ;
   }

   global void finish(Database.BatchableContext BC)
   {
   }

}


Now to execute this batch class we can schedule it or simply run this code in system logs for testing :

Batch_CaseEmail controller = new Batch_CaseEmail() ;
Integer batchSize = 1;
database.executebatch(controller , batchSize);



So when we execute it all system administrators will get E-mails in this format 






Thanks.

Mar 5, 2011

Error : "Trigger must be associated with a job detail"

Hi All,


While scheduling a class we some times gets error "Trigger must be associated with a job detail".


For example: I was facing this error when I schedule my class "ScheduleClass" using System.schedule, giving name to the schedule job "One Time ".


ScheduledClass SC = new ScheduledClass() ;
String sch = '0 0 10 * * ?';
System.schedule('One Time ', sch, SC);


Now please notice there is a space after "One Time ", when I removed this extra space from the name ("One Time") my class was scheduled as expected.


Thanks