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
satakshisatakshi 

validation on contact while sending a mail

Hello,

I am writing a code where i want functionality where if contact are not selected then it will throw error otherwise it will proceed further. i tried a lot. but its not working..
My code is:
public with sharing class ContactSelectClassController

{

    public String beforeblob1{get;set;}
    //public String conid{get;set;}
    public String emailEncoded { get; set; }
    public String email { get; set; }
    public String phoneEncoded { get; set; }
    public String con_number { get; set; }
    public String fullname{get;set;}
    public String emailName{get;set;}
    public String fullnameEncoded{get;set;}
    @TestVisible public List<wrapAccount> wrapAccountList {get; set;}
    //-----added----
   
    public List<Contact> selectedAccounts{get;set;}
    
    public String feedid;
    private  List<Id> contactids=new list<Id>();
    public List<Contact> cont{get;set;}
 
    public List<Feedback_Contacts__c> feedcon{get;set;}  
 
// ---------for search-------
    
    public List<wrapAccount> searchResults {get;set;}
    public List<String> selectP = new List<String>();
    
//--------------searchText--------------
   
    public string searchText {
        get {
            if (searchText == null) searchText = ''; // prefill the serach box for ease of use
            return searchText;
        }
        set;
    }  

   
//--------------ContactSelectClassController()--------------  
   
    public ContactSelectClassController()
    {
     feedid= (ApexPages.currentPage().getParameters().get('fid'));
     emailName= (ApexPages.currentPage().getParameters().get('emailName'));
     fullnameEncoded = ApexPages.currentPage().getParameters().get('fullname');
     emailEncoded = ApexPages.currentPage().getParameters().get('email');
     phoneEncoded = ApexPages.currentPage().getParameters().get('con_number');
              
      //-------added--------
         
      if(searchResults == null)
      {
        searchResults = new List<wrapAccount>(); // init the list if it is null
      }
      
      else
      {
        wrapAccountList.clear(); // clear out the current results if they exist
      }
           
      String qry =  'select Id,Name,Email,Phone from Contact  where firstname LIKE \''+String.escapeSingleQuotes(searchText)+'%\' OR lastname LIKE \''+String.escapeSingleQuotes(searchText)+'%\'';
      for(Contact c : Database.query(qry))
      {
             
     
        
         wrapAccount wa = new wrapAccount(c);
        searchResults.add(wa);
      
      }
           
    //-------added--------
       
      }

//--------------search()--------------


 public PageReference search() {
 
      searchResults = new List<wrapAccount>();
      
      String qry = 'select Id,Name,Email,Phone from Contact where firstname LIKE \''+String.escapeSingleQuotes(searchText)+'%\' OR lastname LIKE \''+String.escapeSingleQuotes(searchText)+'%\'';
      List<Contact> contactList = Database.query(qry);
       
       if(contactList.size()<1)
       {
       String htmlText = 'Oops...Contact not found, Sorry !';
       ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, htmlText));
       }  
         
      for(Contact c : Database.query(qry)) {
          
            wrapAccount cw = new wrapAccount(c);
            searchResults.add(cw);
        }
       
     return null;
    }

//--------------CancelAction()--------------
   
   public pageReference CancelAction()
  {
    PageReference pr1 = new PageReference('/apex/SBSendSurvey');
    pr1.setRedirect(true);
    return pr1;
  }
    
    
//--------------processSelected()--------------
    
     @TestVisible public void processSelected()
     {
     try
       {
     
     feedid= (ApexPages.currentPage().getParameters().get('id'));
    
     fullnameEncoded = ApexPages.currentPage().getParameters().get('fullname');
   
     if(fullnameEncoded !=null || fullnameEncoded =='')
     {
        Blob bodyBlob1 =EncodingUtil.base64Decode(fullnameEncoded );
        fullname = bodyBlob1.toString();
        
     }
  else
  {
   fullname = ApexPages.currentPage().getParameters().get('fullname');
  }
    emailEncoded = ApexPages.currentPage().getParameters().get('email');
 
    if(emailEncoded !=null || emailEncoded =='')
    {
      Blob bodyBlob1 =EncodingUtil.base64Decode(emailEncoded );
      email = bodyBlob1.toString();
    }
  else
  {
   email = ApexPages.currentPage().getParameters().get('email');
  }
  phoneEncoded = ApexPages.currentPage().getParameters().get('con_number');
  if(phoneEncoded !=null )  
  {
  Blob bodyBlob2 =EncodingUtil.base64Decode(phoneEncoded );
  con_number =bodyBlob2.toString();
  }
  else
  {
  con_number=ApexPages.currentPage().getParameters().get('con_number');
  }
        
    selectedAccounts = new List<Contact>();
                            
        for(wrapAccount wrapAccountObj : searchResults)
         {
            if(wrapAccountObj.selected == true)
             {
                selectedAccounts.add(wrapAccountObj.acc);
                
             }
        }
        
           for(Contact cont : selectedAccounts)
              {
                contactids.add(cont.Id);
              }
           
       feedcon = new List<Feedback_Contacts__c>();

        cont  = new List<Contact>();
        cont  =  [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :selectedAccounts];
        for(Contact c :cont)
        {
       
        string fullname = c.firstname + ' ' + c.lastname;
      
        Blob beforeblob = Blob.valueOf(fullname);
        EncodingUtil.urlEncode(fullname,'UTF-8');
     
        string email = c.email;
        Blob beforeblob1 = Blob.valueOf(email);
        EncodingUtil.urlEncode(email,'UTF-8');
   
           string con_number = c.MobilePhone;

           Blob beforeblob2 = Blob.valueOf(con_number);
           EncodingUtil.urlEncode(con_number,'UTF-8');
    
       
        Feedback_Contacts__c fc = new Feedback_Contacts__c();
        fc.FeedbackContactName__c= c.Id;
        //system.debug('++++++++++ConNumber__c++++++++++'+ fc.FeedbackContactName__c);
        fc.FeedBackNo__c = feedid;
        // system.debug('++++++++++ConNumber__c++++++++++'+fc.FeedBackNo__c);

        fc.test__c = EncodingUtil.base64Encode(beforeblob );
        fc.Email__c = EncodingUtil.base64Encode(beforeblob1 );
       fc.ConNumber__c= EncodingUtil.base64Encode(beforeblob2 );
         //system.debug('++++++++++ConNumber__c++++++++++'+ fc.ConNumber__c+'**'+fc.FeedBackNo__c);
        feedcon.add(fc);
        //system.debug('++++++++++feedcon++++++++++'+feedcon);
       }
       insert feedcon;
        
        // system.debug('++++++++++INSERT++++++++++'+feedcon);
      
        Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
        mail.setTargetObjectIds(contactids);
   
     if(!Test.isRunningTest())
     {
   
          EmailTemplate templateId = [Select id from EmailTemplate where name = 'SurveyFeedback'];
         // system.debug('+++++++++++++++++++++++TEMPLATE ID++++++++++++++++++++++++++++++'+templateId.id);
          
        mail.setTemplateId(templateId.id); //00X55000000E1LO
        Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});
        
      }   
        String msg1 = 'Mail Sent!';
      ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO,msg1));
    }
        catch(Exception e) {
       // System.debug('Oops ! Email limit exceeded...Sorry');
               ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Error,'Message not sent'));
System.debug('*********************Oops ! Email limit exceeded...Sorry******************************');
      }    
     }
     
     
//--------------class wrapAccount--------------

    @TestVisible public class wrapAccount {
        public Contact acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Contact a) {
            acc = a;
            selected = false;
        }
    }
    
    
//--------------Redirect()--------------

    
    public PageReference Redirect(){
    PageReference ref = new PageReference('/apex/SBEmailTemplate');
    ref.setRedirect(true);
    return ref;
    }
    
    
    public PageReference RedirectCon(){
    PageReference ref = new PageReference('/apex/ContactPage');
    ref.setRedirect(true);
    return ref;
    }
    
}

Thanks & regards,
Satakshi
Rowallim TechnologyRowallim Technology
Hi Satkshi 
I suggest you to change the (contactList.size()=o)  in this code" if(contactList.size()<1)
       {
       String htmlText = 'Oops...Contact not found, Sorry !';
       ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, htmlText));
       }  " .
Please let me know if it helps you.
Mark this question as solved if it gives you answer.
Regards
satakshisatakshi
No. Its not working as it is for search box from where we can directly send to contact by entering few letters of name
Rowallim TechnologyRowallim Technology
Hi satakshi
According to your requirement do you have the field of data type checkbox or picklist????

 
satakshisatakshi
checkbox