• Bernard Pros
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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;
        }
    }
   }
Hi Friends,
Is it possible for APEX to refer formula fields for example refering to a roll-up summary field.

Please let me know your answers.

Thanks,
JG