0

I have a custom page where I update and insert two objects. The main object is the account, after inserting the record in the account. I get the account Id to reference to its associated object.

When there is an insert or update operation, the trigger for the second object should fire. However It does not work.

Here is the snippet where I execute the insert and update operation.

public class EditAccountExtension 
{

private final Account acct;

public Object2__c {get;set;}
public string SubHeader {get; set;}

public EditAccountExtension(ApexPages.StandardController stdController) 
{
    this.acct = (Account)stdController.getRecord();

    try{

        SubHeader = this.acct.Name;
        Object2 = Database.query('Select Name, field1__c, field2__c' 
                + ' From Object2__c Where Account__c = \'' + this.acct.Id + '\'');

    } catch (Exception ex) {

        SubHeader = 'New Account';
        Object2__c = new User_Group__c(
            Name = 'Default Object2',
            field1__c = true,
            field2__c = true,

        );

    }

}

public void SaveAccount()
{
    if(acct.Id == null)
    {
        insert acct;

        Object2.Account__c = acct.Id;
        insert Object2;
    }
    else
    {
        update acct;
        update Object2;
    }
}

//omitted some code
}

Here is the trigger for Object 2 that should fire.

trigger Object2_SyncChanges on Object2__c(after insert, after update) 
{
    Object2__c object2 = Trigger.new[0];
    Account account = Database.query('SELECT Is_Active__c' 
       + ' FROM Account WHERE Id = \'' + object2 .Account__c + '\'');

    if(account.Is_Active)
    {
       if(Trigger.isUpdate)
       {
           Object2__c oldObject2 = Trigger.old[0];

           if(object2 .Name != oldObject2 .Name)
           {
               Object2API.SaveObject2(object2 .Id);
           }
       }
       else
          Object2API.SaveObject2(object2.Id);
   }
}

The trigger for Account is working fine.

2
  • can you share your complete class & trigger code .. the current code doesn't give enough info on the variable declaration , scope etc.. and which events your trigger handles.. Apr 13, 2015 at 3:22
  • anyone can disregard this question, since the problem is not in the trigger. Trigger is working okay, the problem has something to do with one of my method.
    – rpm07
    Apr 14, 2015 at 7:02

2 Answers 2

1

First of all, your trigger is not bulkified - as designed, it will only work for a single record and hence fail with any Data Loader operation.

as to your specific issue

  1. The controller constructor, when entered for a new Account will have a null value for acct.id
  2. Hence the try{..} will fail.
  3. The catch{..} creates an Object2__c with is_active__c = false
  4. The user clicks save, invoking the action method saveAccount() (which should probably be declared to return a PageReference, not void as a successful save almost always would redirect to a new page (like the detail page)
  5. SaveAccount() inserts the Account and the Object2__c. The latter is not marked with is_active__c = true.
  6. When the Object2__c Object2_SyncChanges trigger is fired, nothing will happen as the triggered record isn't active.

Finally, method SaveAccount() should have its code wrapped with try-catch so you can detect errors and display a meaningful message back to the user

But, please bulkify that trigger before you do anything else.

5
  • regarding about #4 SaveAccount is a generic method to be used for PageReference Save() and PageReference SaveAndNew() I just ommitted some of my codes.
    – rpm07
    Apr 13, 2015 at 5:02
  • By default Is_Active = true.
    – rpm07
    Apr 13, 2015 at 5:03
  • for now I want to insert one record only.
    – rpm07
    Apr 13, 2015 at 5:11
  • I used try and catch for Editing and creating new account.
    – rpm07
    Apr 13, 2015 at 5:13
  • yes - but Data Loader on the Object2__c will produce unexpected results as the trigger is not bulkified
    – cropredy
    Apr 13, 2015 at 5:35
1
if(acct.Id == null)
{
    insert acct;
// do a query to get the acct.Id then pass it to the object 2 if you want to confirm write a debug log and check the acct.Id is not returning now so you need to query and then assign it.


    Object2.Account__c = acct.Id;
    insert Object2;
}
else
{
    update acct;
    update Object2;
}
1
  • 2
    code only answers are tough to relate to the question and understand how it solves the issue. can you please add some details around your answer. Apr 13, 2015 at 3:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .