• CBN
  • NEWBIE
  • 80 Points
  • Member since 2019

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

How to excute below code in Anonymous window 
 
public class UserUpdate {

    public static void updateUser(List<ID> ids){
    Id taskId = 'aAY2h0000004CANGA2';
       
       delete [SELECT Id from PermissionSetAssignment where AssigneeId=:userId and PermissionSet.ProfileId=null];
       delete [Select Id  from GroupMember where UserOrGroupID=:userId];
       delete [SELECT Id FROM UserPackageLicense where UserId=:userId];     
        
        Profile prf = [select id from Profile where Name='ServiceDesk Client'];
        User usr = [select IsActive,UserRoleId,ProfileId,Title,ManagerId from User where id=:userId];
        usr.UserRoleId = null;
        usr.ProfileId = prf.id;
        usr.Title = null;
        usr.ManagerId = null;
        usr.IsActive = false;
        update usr;
        
    }
    }

Kindly Support and suggest

Thanks 
  • May 14, 2019
  • Like
  • 0
Hi All,

How to write a scheduler class for daily and weekly for below Batch Apex
  • IF the recent Recharge record for 'Internet Plan' record was made 1 day ago and its 'Data Plan' = 'Daily', then create Recharge record.
  • IF the recent  Recharge for 'Internet Plan' was made 6 days ago and 'Data Plan' = 'Weekly', then create Recharge record.
    global class batchCreateRecharge implements Database.Batchable<sobject> {
      
        global Database.QueryLocator start(Database.BatchableContext bc){
          
            String query = 'SELECT Id, Data_Plan__c,Total_Amount__c FROM Internet_Plan__c';
            return Database.getQueryLocator(query);
        }
          
        global void execute(Database.BatchableContext bc, List<Internet_Plan__c> scope) {
          try {
         List<Recharge__c> rcList = new List<Recharge__c>();
            for(Internet_Plan__c IP : scope) {
               Recharge__c rc = new Recharge__c();
              rc.Amount__c = IP.Total_Amount__c;
               rc.Internet_Plan__c= IP.id;
             rcList.add(rc);
             System.debug('******Recharge list'+rcList);
            }
       insert rcList;
     }
             
                catch( DmlException e ) {
                    // HANDLES EXCEPTIONS RELATED TO DML STATEMENTS.
                    System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
                }
                catch( Exception e ) {
                    // HANDLES EXCEPTIONS OTHER THAN ABOVE SUCH AS NULL POINTER EXCEPTIONS OR ETC.
                    System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
                }
    
        } 
          
        global void finish(Database.BatchableContext bc) {
          
        }
    }

    Kindly Support and Suggest
Thanks
  • April 30, 2019
  • Like
  • 0
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
 
  • April 16, 2019
  • Like
  • 0
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){
       
      }    


}

 
  • April 02, 2019
  • Like
  • 0
Hi all,

How to Write DML Statements in try catch block 

// Handler Class
 
public class CountContactHandler
{
                public static void countContacts(List<Contact> conList){
                                Set<Id> setAccountIds = new Set<Id>();
                                for(Contact con : conList)
                                {
                                                setAccountIds.add(con.AccountId);
                                }
                               
                                List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id,Contact_Roll__c from contacts) from Account where Id in : setAccountIds];
                               
                                for(Account acc :listAccs)
                                {
                                                Integer i=0,j = 0;
                                                for(Contact conItr : acc.Contacts){
                                                                if(conItr.Contact_Roll__c)
                {
                    i++;
                }
                                                                j++;
                                               
                                                }
                                                acc.No_of_active_contacts__c = i;
                                                acc.Total_No_Of_Contacts__c = j;
                                }
                                update listAccs;
                }
}
// 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)
                {
                                CountContactHandler.countContacts(Trigger.new);
                }
 
                else 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
                                CountContactHandler.countContacts(Trigger.old);
                }
}

Kindly support and suggest

Thanks
 
  • March 27, 2019
  • Like
  • 0
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
  • March 26, 2019
  • Like
  • 0
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;
 
}
 
  • March 26, 2019
  • Like
  • 0
trigger CaseUpdate on Case (before update) {
    
    for(Case c:trigger.New){
      if( Trigger.oldMap.get( c.id ).Status != Trigger.newMap.get(c.id ).Status && Trigger.newMap.get(c.id ).Status =='Working')   
            c.OwnerId=Userinfo.getUserID();
  }

}
  • February 05, 2019
  • Like
  • 0
.....................Batch apex.............................

global class BatchUpdateAccountField implements Database.Batchable <SObject>{
global Database.QueryLocator start (Database.BatchableContext bc)
{
    return Database.getQueryLocator('SELECT name from Account');
}
   global void execute(Database.BatchableContext bc, List<Account> acList)
   {
       for(Account ac :acList)
       {
          ac.name = ac.name + ' Testing';
           
       }
       
       update acList;
   }
    
    global void finish(Database.BatchableContext bc)
    {
        System.debug('>>>Finish');
    }
    
}



...................Schedule apex..............

global class ScheduleBatchAccFieldUpdate implements Schedulable {
    global void execute(SchedulableContext sc)
    {
        BatchUpdateAccountField ba =new BatchUpdateAccountField();
        Database.executeBatch(ba);
    }

}
  • February 05, 2019
  • Like
  • 0
global class Batch_UpdateAcc implements Database.Batchable<sObject>{
    
    List <Account> mapAccount = new List <Account> ();
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT ID, BillingCountry, ShippingCountry FROM Account');
    }
    
    global void execute(Database.BatchableContext bc, List<Account> scope){
        for(Account acc : scope){
            if(acc.BillingCountry=='UK'){
                acc.BillingCountry = 'United Kingdom';
            }
            if(acc.ShippingCountry=='UK'){
                acc.ShippingCountry = 'United Kingdom';
            }
            if(acc.BillingCountry=='USA'){
                acc.BillingCountry = 'United States';
            }
             if(acc.ShippingCountry=='USA'){
                acc.ShippingCountry = 'United States';
            }
            mapAccount.add(acc);
        }
        UPDATE mapAccount;        
    }
    
    global void finish(Database.BatchableContext bc){
        
    }     
}
  • February 05, 2019
  • Like
  • 0
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
 
  • April 16, 2019
  • Like
  • 0
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){
       
      }    


}

 
  • April 02, 2019
  • Like
  • 0
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
  • March 26, 2019
  • Like
  • 0