-3
public class AccountContactHandler2 {
    //trigger AccountContacttrigger2
    //---------------After insert -----------------
    public static void createContact(List<Account> acc ){
        List<Contact> con=new List<Contact>();
        for(Account a : acc){
            if(a.CreateNumberofRelatedContact__c!=null){
                Integer relatedContacts= Integer.valueOf(a.CreateNumberofRelatedContact__c);
                for(Integer i=0;i<relatedContacts;i++){
                    con.add(new contact(Accountid=a.id, FirstName=a.Name+i, LastName=a.Name+i ));
                }

            }
        }
        if(!con.isEmpty()){
            insert con;
        }
    }
    //--------------After update----------------------


    public static void updateContact(List<Account>acc ,map<id,Account>oldac){
        Integer oldvalue;
        Integer newvalue;
        List<Contact> newcon=new List<Contact>();
        list<contact>consoql=new list<contact>();
        for(Account account1:acc){
            Account oldaccount = oldac.get(account1.Id);
            newvalue=Integer.valueOf(account1.CreateNumberofRelatedContact__c);
            oldvalue=Integer.valueOf(oldaccount.CreateNumberofRelatedContact__c);
            if (newvalue > oldvalue){
                system.debug(' new value------>'+newvalue+  'oldvalue----'+oldvalue);
                for(Integer i=oldvalue;i<newvalue;i++){
                    newcon.add(new contact(Accountid=account1.id, FirstName=account1.name+i, LastName=account1.name+i ));  
                    system.debug('new added contacts------>'+newcon);
                }
                if(!newcon.isEmpty()){
                    upsert newcon;
                }

            }
            // ----------------delete opertion----------

            else if(newvalue < oldvalue){
                system.debug('newly entered value--->' +newvalue+  'oldvalue----'+oldvalue);
                consoql=[select id ,Accountid, firstname, lastname from contact where Accountid in:oldac.keyset()];
                for(Contact c:consoql) {
                    system.debug('contacts retrieved ---+++++++-'+consoql);
                    for(Integer i=newvalue;i<oldvalue;i++)  {
                        if(c.firstname==account1.name+i && c.lastname==account1.name+i)
                        {
                            newcon.add(c);  
                            system.debug('contacts to-----del----'+newcon);
                        }
                    } 
                }
                if(!newcon.isEmpty()){
                    delete newcon;

                }

            } 
        }  
    }
}

closed as off-topic by Reshma, Pranay Jaiswal, Lukas Lunow, Derek F, battery.cord Jun 18 at 13:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Reshma, Pranay Jaiswal, Lukas Lunow, Derek F, battery.cord
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Welcome to SFSE. Please take a moment to go through how to ask and then edit your question to provide the exact error you are getting and the stack trace of the error message. – Vijay Ganji Jun 12 at 8:05
  • 4
    Hi Nischay, What do you want to do? Do you want to delete all related contacts to the account? Are you trying to learn how to code in APEX, it seems like to are inserting and updating contacts when ever inserting/updating and account and also deleting them. If want to delete all contacts related to an account when updating the account, you might want to do that as a batch job, if you have a lot of contacts related to that account or if you are updating many accounts. You would then pass the account ids to the batch job and query the contact there and delete them in the batch job. – ChristianJuul Jun 12 at 8:32
2

Your code is a bit hard to follow: always worth stating what you are aiming to do in the question in words and also explaining exactly what has gone wrong.

Here are some general suggestions that may help:

  • A benefit of using a handler class is that you can break pieces of logic out into separate methods so what is going on is made clearer.
  • Don't declare all the variables up front but rather declare them where they are needed and give them a value immediately.
  • Variable names such as Name1 and temp aren't informative: choose better names that explain what is going on.
  • Setting a variable such as relContact in one loop and then using its value in another loop won't work; the second loop will just get the last value set by the first loop. Generally, maps keyed by an Id value (the Account Id in this case) are needed for this sort of logic. This is a likely cause of your trigger not working.
  • If the aim is to limit the number of related contacts, you will need to be careful to query them in some specific order (use order by) so the appropriate ones are deleted not arbitrary ones.

PS

On the subject of hitting a CPU limit, this doubly nested loop:

            for(Contact c:consoql) {
                system.debug('contacts retrieved ---+++++++-'+consoql);
                for(Integer i=newvalue;i<oldvalue;i++)  {
                    if(c.firstname==account1.name+i && c.lastname==account1.name+i)
                    {
                        newcon.add(c);  
                        system.debug('contacts to-----del----'+newcon);
                    }
                } 
            }

is a likely source of the problem as explained here ... cause of System.LimitException: Apex CPU time limit exceeded where performance declines exponentially. The general solution is to build a map up front.

  • @nischay Please edit your question. Comments are not meant for adding details. – David Reed Jun 12 at 20:03
  • @david Reed I updated the title – nischay Jun 13 at 3:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.