• CBN
  • NEWBIE
  • 80 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 6
    Replies
Hi all, 

Help me to write a test class
 
public with sharing class AccountwithActivecontactsController {

     public List<AccountInfoWrapper> AccountsWrapper {get; set;}    
    
    public List<List<AccountInfoWrapper>> listofAccountsWrapper {get; set;}
    
 /*   public AccountwithActivecontactsController()
    {
  AccountsWrapper = new List<AccountInfoWrapper>();
  listofAccountsWrapper = new List<List<AccountInfoWrapper>>();
        getAccounts();
    }  */
     
    public List<AccountInfoWrapper> getAccounts()   
    {
        AccountsWrapper = new List<AccountInfoWrapper>();
        try{
        string str = 'SELECT id,Name,Ownerid, Account.Owner.Profile.Name,Type,Total_No_Of_Contacts__c,No_Of_Opportunties__c,(SELECT id, Name, Email, Phone,  createdDate from contacts where Active__c = true order by createdDate desc LIMIT 1) FROM Account';
        for(Account a:database.query(str))
        {
            if(a.contacts.size() > 0)  {   
            AccountsWrapper.add(new AccountInfoWrapper(a, a.contacts)); 
           // AccountsWrapper.add(new AccountInfoWrapper(s.contacts));
         }   
        }   
        system.debug('***********accounts**'+AccountsWrapper);
    } 
    
        catch( QueryException e ) {
                // HANDLES EXCEPTIONS RELATED TO SOQL or SOSL STATEMENTS.
                System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
            }
                    return AccountsWrapper; 

            }
    
    public class AccountInfoWrapper
    {
        public Account sObj{get;set;}
        public contact con {get;set;}
        public AccountInfoWrapper(Account acc, contact con)
        {
            sObj = acc; 
            this.con = con; 
        }  
        
    }

Kindly Support and Suggest

Thanks
 
Hi all

if campaign member status is responded then sent a mail to campaign member contact email adress

Here is my code
 
global class CampaignMassMailbatch implements Database.batchable<sobject>, Database.Stateful {
    
    public string query;
     global Database.QueryLocator start(Database.BatchableContext bc) {
     
     query = 'Select Id, contact.email from CampaignMember where CampaignMember.Status=Responded' ;
     
     system.debug(Database.getQueryLocator(query));
      
     return Database.getQueryLocator(query);
      
      }
      
     global void execute(Database.BatchableContext bc,  List<Campaignmember> Scope){
      system.debug('campaign batch');
      List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
     for(CampaignMember cm : Scope){
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
        EmailTemplate  et=[Select id from EmailTemplate where developername = 'Email to Campaign Members' limit 1];
     system.debug(et.id);
   
          String[] toAddresses = new String[] {cm.Email} ;
          mail.setToAddresses(toAddresses) ;
          //  mail.setHtmlBody('body');
          mail.setTemplateId(et.id);
          mails.add(mail);          
        }
          
        Messaging.SendEmailResult[] results = Messaging.sendEmail(mails);
        if (results[0].success) {
        System.debug('The email was sent successfully.');
        } else {
        System.debug('The email failed to send: '
              + results[0].errors[0].message);
       }  
       }
      
      
      global void finish(Database.BatchableContext bc){
       
      }    


}

 
Hi All,
Use single SOQL quert to fetch the list of contacts. use if condition to check if contact is active then set active contact accordingly.Put logic in helper class and call tis class from trigger

trigger countContact on Contact (after insert, after update, after delete, after undelete) 
{
  Set<Id> setAccountIds = new Set<Id>();
 
  //Whenever your working with After Undelete operation you can access data through 
  //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
  if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
  {
   for(Contact con : Trigger.new)
   {
    setAccountIds.add(con.AccountId);
   }
  }
 
  if(Trigger.isDelete)
  {
   //if you use Trigger.new below in place of Trigger.old you will end up with 
   //System.NullPointerException:Attempt to de-reference a null object
   for(Contact con : Trigger.old) 
   {
    setAccountIds.add(con.AccountId);
   }
  }
 
 List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id from contacts) from Account where Id in : setAccountIds];
  for(Account acc :listAccs)
  {
   acc.Total_No_Of_Contacts__c = acc.contacts.size();
  }
          List<Account> listAccs1 = [Select id,name,No_of_active_contacts__c ,(select id from contacts where Contact_Roll__c = True) from Account where Id in : setAccountIds];
  for(Account acc1 :listAccs1)
  {
   acc1.No_of_active_contacts__c = acc1.contacts.size();
  }
  update listAccs;
  update listAccs1;
 
}

Kindly Support and Suggest
Thanks