• gott max
  • NEWBIE
  • -1 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi all,
I have working Code the sends an SMS  via Apex and Twilio and nexmo, Great stuff. The trouble I am having is writing the Tests and I just need a little help. I don't do near enough Apex for my liking and I am feeling it here. 

IAny help/pointers would be most appreciated. 

Apex Class:
 
public with sharing class Bulkconsms {
    public list<contact> selectedcons {set;get;}
    public list<contact> selectedcontacts = new list<Contact>();
    Public string TextMSG {get;set;}
    list<Task> ldlst=new list<Task>();
    list <SMS_Response__c>conlis=new list<SMS_Response__c>();
    public List<ConWrapper> lstWrapper {get;set;}
    public List<ConWrapper> lstSetController{get;set;}
    public Boolean displayPopup {get;set;}
    CustomIterable obj;   
    
    public Bulkconsms()      {      
        
        lstWrapper =  new List<ConWrapper>();        
        lstSetController = new List<ConWrapper>();
        
        List<Contact> lstContact = [select id,FirstName,LastName,Email,account.Name,MailingCity,MailingState,Phone,Title,MobilePhone,Account.AnnualRevenue,
                                    account.Industry,LeadSource,Fax,account.Website,Description from contact order by LastName DESC LIMIT 990]; 
        
        
        system.debug('Contact count :'+lstContact.size());
        for(Contact cont : lstContact )  
        {            
            lstWrapper.add(new ConWrapper(cont ,false));         
        }         
        obj = new CustomIterable (lstWrapper);          
        obj.setPageSize = 10;         
        next();              
    }                   
    public Boolean hasNext {             
        get {
            return obj.hasNext(); 
        }    set; 
    }                  
    public Boolean hasPrevious {
        get { 
            return obj.hasPrevious();
        }   set;
    }                 
    public void next()         
    {             
        lstSetController = obj.next();        
    }                  
    public void previous()          
    {             
        lstSetController = obj.previous();        
    }
    public pagereference homepage()
    {
        PageReference pr = new PageReference('/apex/BulkSmsHome');
        return pr;
    }    
    
    public void processSelected() {                       
        displayPopup = true;               
        selectedcons = new List<contact>();
        
        for(ConWrapper wrapconObj : lstSetController) {
            if(wrapconObj.isselected == true) {
                selectedcons.add(wrapconObj.cont);
                selectedcontacts.add(wrapconobj.cont);
                
            }
            
        }
        try{
            if(selectedcontacts.size()>0){                                                
            }
        } catch(Exception Ex){
        }
    }
    public void closePopup()
    {
        displayPopup = false;
    }
    
    
    
    public void listofcons(){
        for(contact c : selectedcontacts){
            
        }
    }
    public void ldinsert(){      
        
        for(contact con : selectedcontacts){
            configuration_setting__c conf = [select Name,AccountSid__c, Active__c,AuthToken__c,Bulk_SMS__c,Contact_Phone_Number__c,Lead_Phone_Number__c,TestPhone__c FROM configuration_setting__c where Active__c =True  limit 1 ];
            {
                if(conf.Name =='Twilio'){
                    string Text =TextMSG;
                    string tonumber =con.Phone;
                    string Fromnumber=conf.Bulk_SMS__c;
                    Twilio.sendfromtwilio(Fromnumber,Text,tonumber);
                }else if(conf.Name =='Nexmo'){
                    string tonum=con.phone;
                    integer [] tonmb= tonum.getChars();
                    tonmb.remove(0);
                    string toId=string.fromCharArray(tonmb);
                    string fromId = conf.Bulk_SMS__c;
                    string text =TextMSG;
                    string channel ='sms';
                    Nexmo.sendMessage( toId,  fromId,  text,  channel);
                }
            } 
            
            Task newTask = new Task();
            newTask.WhoId = con.Id ;
            newTask.Subject = 'SMS Send';
            newTask.priority= 'Normal' ;
            newTask.status = 'Completed';
            newTask.description = TextMSG ;
            newTask.ActivityDate = System.today() ;            
            ldlst.add(newTask);
            
            SMS_Response__c NewRec = new SMS_Response__c();
            NewRec.Mobile_Number__c =con.Phone ;
            NewRec.Message__c = TextMSG;
            NewRec.SMS_Type__c = 'OutGoing Message' ;
            NewRec.Service_Providers__c = 'Bulk sms from Twilio';
            conlis.add(NewRec);
            
            displayPopup = false;
        }
        if(ldlst.size()>0){
            
            try{
                insert ldlst ;   
                insert conlis;
                
            }
            catch(DMLException E){
                
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,E.getMessage()));
            }
            
        }
    }  
    
    public class ConWrapper {
        public Boolean isSelected {get;set;}
        public Contact cont{get;set;}            
        public ConWrapper(Contact c,Boolean isSelected)     {         
            this.cont= c;        
            this.isSelected= false;    
        }
    }
    public class  CustomIterable implements Iterator<list<ConWrapper>> {    
        list<ConWrapper> InnerList{get; set;}    
        list<ConWrapper> ListRequested{get; set;}    
        Integer i {get;set;}    
        public Integer setPageSize {get;set;}    
        public CustomIterable(List<ConWrapper> lstAccWr)    {        
            InnerList = new list<ConWrapper>();         
            ListRequested = new list<ConWrapper>();           
            InnerList = lstAccWr;       
            setPageSize = 5;       
            i = 0;    
        }       
        public boolean hasNext(){        
            if(i >= InnerList.size()) 
            {           
                return false;        
            } else {            
                return true;         
            }    
        }         
        public boolean hasPrevious(){         
            
            if(i <= setPageSize) {            
                return false;         
            } else {            
                return true;        
            }    
        }       
        public list<ConWrapper> next(){               
            
            ListRequested = new list<ConWrapper>();         
            integer startNumber;       
            integer size = InnerList.size();       
            if(hasNext())        {             
                if(size <= (i + setPageSize))  {                
                    startNumber = i;               
                    i = size;            
                }  else  {               
                    i = (i + setPageSize);                
                    startNumber = (i - setPageSize);           
                }                       
                
                for(integer start = startNumber; start < i; start++)    
                {               
                    ListRequested.add(InnerList[start]);           
                }        
            }  
            return ListRequested;    
        }         
        public list<ConWrapper> previous(){            
            ListRequested = new list<ConWrapper>();        
            
            integer size = InnerList.size();         
            if(i == size)   {            
                if(math.mod(size, setPageSize) > 0)            
                {                   
                    i = size - math.mod(size, setPageSize);          
                }  else  {                
                    i = (size - setPageSize);          
                }         
            } else  {          
                i = (i - setPageSize);       
            }               
            
            for(integer start = (i - setPageSize); start < i; ++start)        
            {            
                ListRequested.add(InnerList[start]);       
            }         
            return ListRequested;   
        }   
    }
    
}

 
  • January 20, 2020
  • Like
  • 0