• Akshay Alandkar
  • NEWBIE
  • 30 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 8
    Replies
public class DelteContactController {
    public account acc {get; set;}
    public List<ContactInfoWrapper> ContactsWrapper {get; set;}
    public DelteContactController(ApexPages.StandardController controller)
    {}
    public List<ContactInfoWrapper> getAccObj(){
        Id id = apexpages.currentpage().getparameters().get('id');
         acc = [SELECT id from account where Id =:id];
        list<contact> conList=[SELECT Id,Account.Name,Name, Email FROM Contact where AccountId=:acc.id];
        ContactsWrapper = new List<ContactInfoWrapper>();
        for(Contact con:conList){
            ContactsWrapper.add(new ContactInfoWrapper(con,false));
        }
        return ContactsWrapper;
    }
    
    
    
    
    //Wrapper class
    public class ContactInfoWrapper
    {
        public Contact sObj{get;set;}
        public Boolean checked {get;set;}
        public ContactInfoWrapper(Contact con,boolean selectedBox)
        {
            sObj = con;
            checked=selectedBox;
        }
        
    }
   
    
    //Delete
    
    public pageReference deletecontact()
    {
        list<Contact> conlist = new list<Contact>();
        for(ContactInfoWrapper c : ContactsWrapper)
        {
            if(c.checked == true)
            {
                
                conlist.add(c.sObj);
            }
        }
        if(conlist.size()>0)
        {
           Delete conlist ;
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Contact Deleted Successfully !!!'));
        }
        pageReference pr = new pageReference('/lightning/o/Account/list?'+acc.id);
        return pr ;
        
        
    }
    
  

}
In account detail page create a button”Delete contacts” which displays and delete the
corresponding contacts of the particular account record.
public class AccountHelperClass 
{
  // Que.1. Write a Trigger on Account which will create the clone record.
    public static Boolean CheckRecurssion=true;
    
    public static void AccountClone(List<Account> account1)
    {
        List<Account> accList=new List<Account>();
        if(CheckRecurssion)
        {
            CheckRecurssion=false;
            for(Account acc:account1)
                {
                Account CloneAcc=acc.clone(false,false,false,false); //clone id
                accList.add(CloneAcc);
                }
        insert accList;
        }
}
//after acc insert 5 contact & 3 oppty record
    public static void AccountInsert(List<Account> account2){
        List<Contact> conList=new List<Contact>();
        List<Opportunity> oppList=new List<Opportunity>();
        for(Account acc:account2){
            for(Integer i=1;i<6;i++)
            {
                Contact con=new Contact(FirstName=acc.name,LastName='Contact '+i,AccountId=acc.Id);
                conList.add(con);
            }
            
            for(Integer i=1;i<4;i++)
            {
                Opportunity opp=new Opportunity(Name=acc.Name+'Oppty '+i,CloseDate=Date.today()+60,
                                                StageName='Prospecting',AccountId=acc.Id);
                oppList.add(opp);
            }
        }
        Insert conList;
        Insert oppList;
    }
    //if verification needed selected set user as account owner
    public static void AccountVerification(List<Account> account3){
        USER u=[SELECT Id,Name,username,Alias FROM User WHERE Name='barak obama'];
        for(Account acc:account3){
            if(acc.Status__c=='Verification Needed'){
                acc.OwnerId=u.Id;
            }
        }
    }
    
    public void REIError(List<Account> account3){
       for(Account acc:account3)
        {
          string testName='REI';
          if(acc.Name.Contains(testName))
            {
              acc.addError('The Account Name Should Not contain REI Text Value.');
            }
            else
            {
                if(acc.Name.Contains('__Test')){
                    //do nothing
                }
                else{
                    acc.name=acc.name+'__Test';
                }
            }
        }
    }
    
     //A7 Q1. If you delete any account record corresponding child contact records to be assigned to a Particular account.
    //(Associate Contact record to another Account)
    
    public static void method(List<account> accList)
    {
        list<contact> conList= [select lastname,accountid from contact where accountid IN: accList];
        account acc=[select id from account LIMIT 1];
        list<contact> updateConList= new List<contact>();
        if(conList.size()>0)    
        {
            for(contact con: conList)
            {
                con.AccountId=acc.Id;
                updateConList.add(con);
            }
        }
        if(updateConList.size()>0)
        {
            update conList;
        }
    }
    
    //A7 Q4. If Account has at least one Opportunity records associated to it then prevent user from deleting that Account.
    //Error message to show: This Account has some related Opportunity record(s), you cannot delete this Account.    
        
    public static void PreventDeletion(List<account> oldAccount)
    {
        list<opportunity> oppList=[select name, accountid from opportunity where accountid in: oldAccount];
        map<id,integer> PreDel= new map<id,integer>();
        for(opportunity opp: oppList)
        {
            if(!PreDel.containsKey(opp.accountid))
            {
                PreDel.put(opp.accountid,1);
            }
            else
            {
                integer a=PreDel.get(opp.accountid);
                a++;
                PreDel.put(opp.accountid,a);
            }
        }
        for(account acc: oldAccount)
        {
            if(PreDel.containsKey(acc.id))
            {
                acc.adderror('This Account has some related Opportunity records, you cannot delete this Account');
            }
        }
    }
    
    //A7 Q6.Whenever an account phone is modified, update all the contacts of the account
    // Contacts other Phone as OldPhone of Account
    // Contacts Mobile Phone as NewPhone of Account 

     public static void phone1(List<account> oldAccount, List<account> newAccount)
     {
        List<contact> conList=[select lastname,otherphone,accountid,mobilePhone from contact where accountid IN: newAccount];
        map<id,string> oldAccidVsPhone= new map<id,string>();
        map<id,string> newAccidVsPhone= new map<id,string>();
        for(account newAcc: newAccount)
        {
            for(account oldAcc: oldAccount)
            {
                if(newAcc.phone!=oldAcc.phone && oldAcc.id==newAcc.id)
                {
                    oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
                    newAccidVsPhone.put(newAcc.id,newAcc.phone);
                }
            }
        }
        list<contact> updateContactList= new List<contact>();
        for(contact con: conList)
        {
            if(oldAccidVsPhone.containskey(con.accountid))
            {
                con.otherphone=oldAccidVsPhone.get(con.accountid);
                con.mobilePhone=newAccidVsPhone.get(con.accountid);
                updateContactList.add(con);
               }
        }
        if(updateContactList.size()>0)
        {
        update updateContactList;
        }
    }
   }
public void phone1(List<account> oldAccount, List<account> newAccount)
     {
        List<contact> conList=[select lastname,otherphone,accountid,mobilePhone from contact where accountid IN: newAccount];
        map<id,string> oldAccidVsPhone= new map<id,string>();
        map<id,string> newAccidVsPhone= new map<id,string>();
        for(account newAcc: newAccount)
        {
            for(account oldAcc: oldAccount)
            {
                if(newAcc.phone!=oldAcc.phone && oldAcc.id==newAcc.id)
                {
                    oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
                    newAccidVsPhone.put(newAcc.id,newAcc.phone);
                }
            }
        }
        list<contact> updateContactList= new List<contact>();
        for(contact con: conList)
        {
            if(oldAccidVsPhone.containskey(con.accountid))
            {
                con.otherphone=oldAccidVsPhone.get(con.accountid);
                con.mobilePhone=newAccidVsPhone.get(con.accountid);
                updateContactList.add(con);
               }
        }
        if(updateContactList.size()>0)
        {
        update updateContactList;
        }
    }
   }
public void AccountVerification(List<Account> account3){
        USER u=[SELECT Id,Name,username,Alias FROM User WHERE Name='barak obama'];
        for(Account acc:account3){
            if(acc.Status__c=='Verification Needed'){
                acc.OwnerId=u.Id;
            }
        }
    }
    
    public void REIError(List<Account> account3){
       for(Account acc:account3)
        {
          string testName='REI';
          if(acc.Name.Contains(testName))
            {
              acc.addError('The Account Name Should Not contain REI Text Value.');
            }
            else
            {
                if(acc.Name.Contains('__Test')){
                    //do nothing
                }
                else{
                    acc.name=acc.name+'__Test';
                }
            }
        }
    }
    
public class ContactHelperClass
{
    //email and phone number fields cant be duplicate
    public void PhoneAndEmailDuplicate(List<Contact> contact1)
        {
        Set<String> NewEmailSet=new Set<String>();
        Set<String> NewPhoneSet=new Set<String>();
        
        for(Contact con:contact1)
        {
        if(con.Email !=null)
        {
        NewEmailSet.add(con.Email);
        }
        if(con.Phone !=null)
        {
        NewPhoneSet.add(con.Phone);
        }
        }
        
        List<Contact> existingcontList=[SELECT Id,Name,Email,Phone FROM Contact WHERE Email != null OR Phone!=null];
        for(Contact con:contact1)
        {
        for(Contact con1:existingcontList)
        {
        if(NewEmailSet.contains(con1.Email))
        {
        con.Email.addError('A Contact with the Same email Address alredy exist in the System.');
        }
        if(NewPhoneSet.contains(con1.Phone))
        {
        con.Phone.addError('A Contact with the Same Phone Number alredy exist in the System.');
        }
        }
        
        
        }
        }
public class DelteContactController {
    public account acc {get; set;}
    public List<ContactInfoWrapper> ContactsWrapper {get; set;}
    public DelteContactController(ApexPages.StandardController controller)
    {}
    public List<ContactInfoWrapper> getAccObj(){
        Id id = apexpages.currentpage().getparameters().get('id');
         acc = [SELECT id from account where Id =:id];
        list<contact> conList=[SELECT Id,Account.Name,Name, Email FROM Contact where AccountId=:acc.id];
        ContactsWrapper = new List<ContactInfoWrapper>();
        for(Contact con:conList){
            ContactsWrapper.add(new ContactInfoWrapper(con,false));
        }
        return ContactsWrapper;
    }
    
    
    
    
    //Wrapper class
    public class ContactInfoWrapper
    {
        public Contact sObj{get;set;}
        public Boolean checked {get;set;}
        public ContactInfoWrapper(Contact con,boolean selectedBox)
        {
            sObj = con;
            checked=selectedBox;
        }
        
    }
   
    
    //Delete
    
    public pageReference deletecontact()
    {
        list<Contact> conlist = new list<Contact>();
        for(ContactInfoWrapper c : ContactsWrapper)
        {
            if(c.checked == true)
            {
                
                conlist.add(c.sObj);
            }
        }
        if(conlist.size()>0)
        {
           Delete conlist ;
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Contact Deleted Successfully !!!'));
        }
        pageReference pr = new pageReference('/lightning/o/Account/list?'+acc.id);
        return pr ;
        
        
    }
    
  

}
In account detail page create a button”Delete contacts” which displays and delete the
corresponding contacts of the particular account record.
public class AccountHelperClass 
{
  // Que.1. Write a Trigger on Account which will create the clone record.
    public static Boolean CheckRecurssion=true;
    
    public static void AccountClone(List<Account> account1)
    {
        List<Account> accList=new List<Account>();
        if(CheckRecurssion)
        {
            CheckRecurssion=false;
            for(Account acc:account1)
                {
                Account CloneAcc=acc.clone(false,false,false,false); //clone id
                accList.add(CloneAcc);
                }
        insert accList;
        }
}
//after acc insert 5 contact & 3 oppty record
    public static void AccountInsert(List<Account> account2){
        List<Contact> conList=new List<Contact>();
        List<Opportunity> oppList=new List<Opportunity>();
        for(Account acc:account2){
            for(Integer i=1;i<6;i++)
            {
                Contact con=new Contact(FirstName=acc.name,LastName='Contact '+i,AccountId=acc.Id);
                conList.add(con);
            }
            
            for(Integer i=1;i<4;i++)
            {
                Opportunity opp=new Opportunity(Name=acc.Name+'Oppty '+i,CloseDate=Date.today()+60,
                                                StageName='Prospecting',AccountId=acc.Id);
                oppList.add(opp);
            }
        }
        Insert conList;
        Insert oppList;
    }
    //if verification needed selected set user as account owner
    public static void AccountVerification(List<Account> account3){
        USER u=[SELECT Id,Name,username,Alias FROM User WHERE Name='barak obama'];
        for(Account acc:account3){
            if(acc.Status__c=='Verification Needed'){
                acc.OwnerId=u.Id;
            }
        }
    }
    
    public void REIError(List<Account> account3){
       for(Account acc:account3)
        {
          string testName='REI';
          if(acc.Name.Contains(testName))
            {
              acc.addError('The Account Name Should Not contain REI Text Value.');
            }
            else
            {
                if(acc.Name.Contains('__Test')){
                    //do nothing
                }
                else{
                    acc.name=acc.name+'__Test';
                }
            }
        }
    }
    
     //A7 Q1. If you delete any account record corresponding child contact records to be assigned to a Particular account.
    //(Associate Contact record to another Account)
    
    public static void method(List<account> accList)
    {
        list<contact> conList= [select lastname,accountid from contact where accountid IN: accList];
        account acc=[select id from account LIMIT 1];
        list<contact> updateConList= new List<contact>();
        if(conList.size()>0)    
        {
            for(contact con: conList)
            {
                con.AccountId=acc.Id;
                updateConList.add(con);
            }
        }
        if(updateConList.size()>0)
        {
            update conList;
        }
    }
    
    //A7 Q4. If Account has at least one Opportunity records associated to it then prevent user from deleting that Account.
    //Error message to show: This Account has some related Opportunity record(s), you cannot delete this Account.    
        
    public static void PreventDeletion(List<account> oldAccount)
    {
        list<opportunity> oppList=[select name, accountid from opportunity where accountid in: oldAccount];
        map<id,integer> PreDel= new map<id,integer>();
        for(opportunity opp: oppList)
        {
            if(!PreDel.containsKey(opp.accountid))
            {
                PreDel.put(opp.accountid,1);
            }
            else
            {
                integer a=PreDel.get(opp.accountid);
                a++;
                PreDel.put(opp.accountid,a);
            }
        }
        for(account acc: oldAccount)
        {
            if(PreDel.containsKey(acc.id))
            {
                acc.adderror('This Account has some related Opportunity records, you cannot delete this Account');
            }
        }
    }
    
    //A7 Q6.Whenever an account phone is modified, update all the contacts of the account
    // Contacts other Phone as OldPhone of Account
    // Contacts Mobile Phone as NewPhone of Account 

     public static void phone1(List<account> oldAccount, List<account> newAccount)
     {
        List<contact> conList=[select lastname,otherphone,accountid,mobilePhone from contact where accountid IN: newAccount];
        map<id,string> oldAccidVsPhone= new map<id,string>();
        map<id,string> newAccidVsPhone= new map<id,string>();
        for(account newAcc: newAccount)
        {
            for(account oldAcc: oldAccount)
            {
                if(newAcc.phone!=oldAcc.phone && oldAcc.id==newAcc.id)
                {
                    oldAccidVsPhone.put(oldAcc.id,oldAcc.phone);
                    newAccidVsPhone.put(newAcc.id,newAcc.phone);
                }
            }
        }
        list<contact> updateContactList= new List<contact>();
        for(contact con: conList)
        {
            if(oldAccidVsPhone.containskey(con.accountid))
            {
                con.otherphone=oldAccidVsPhone.get(con.accountid);
                con.mobilePhone=newAccidVsPhone.get(con.accountid);
                updateContactList.add(con);
               }
        }
        if(updateContactList.size()>0)
        {
        update updateContactList;
        }
    }
   }
public void AccountVerification(List<Account> account3){
        USER u=[SELECT Id,Name,username,Alias FROM User WHERE Name='barak obama'];
        for(Account acc:account3){
            if(acc.Status__c=='Verification Needed'){
                acc.OwnerId=u.Id;
            }
        }
    }
    
    public void REIError(List<Account> account3){
       for(Account acc:account3)
        {
          string testName='REI';
          if(acc.Name.Contains(testName))
            {
              acc.addError('The Account Name Should Not contain REI Text Value.');
            }
            else
            {
                if(acc.Name.Contains('__Test')){
                    //do nothing
                }
                else{
                    acc.name=acc.name+'__Test';
                }
            }
        }
    }
    
public class ContactHelperClass
{
    //email and phone number fields cant be duplicate
    public void PhoneAndEmailDuplicate(List<Contact> contact1)
        {
        Set<String> NewEmailSet=new Set<String>();
        Set<String> NewPhoneSet=new Set<String>();
        
        for(Contact con:contact1)
        {
        if(con.Email !=null)
        {
        NewEmailSet.add(con.Email);
        }
        if(con.Phone !=null)
        {
        NewPhoneSet.add(con.Phone);
        }
        }
        
        List<Contact> existingcontList=[SELECT Id,Name,Email,Phone FROM Contact WHERE Email != null OR Phone!=null];
        for(Contact con:contact1)
        {
        for(Contact con1:existingcontList)
        {
        if(NewEmailSet.contains(con1.Email))
        {
        con.Email.addError('A Contact with the Same email Address alredy exist in the System.');
        }
        if(NewPhoneSet.contains(con1.Phone))
        {
        con.Phone.addError('A Contact with the Same Phone Number alredy exist in the System.');
        }
        }
        
        
        }
        }