0

I'm trying to update the account every time a contact is updated and the simplest thing seems to be blowing up.

trigger ContactAccountOriginTrigger on Contact (before insert, before Update) {
    List<Account> AccountList = new List<Account>();
    List<ID> IdList = new List<ID>();

    for (Integer i = 0; i <Trigger.new.size(); i++){
       IdList.Add(Trigger.new[i].AccountId);        
    }    

    AccountList = [SELECT Id, Name FROM Account WHERE Id in : IdList];

    for (Integer i = 0; i <Trigger.new.size(); i++){
        if (AccountList.size() > 0){
            for (Account a:AccountList){
                if(Trigger.new[i].Focus_ID__c != null || Trigger.new[i].Focus_ID__c == ''){             
                    logic
                }
                else{
                    logic
                }
                if(a.get('Id') == null) {
                    insert a;                   
                }else{
                    update a;
                }
            }
        }        
    }
}

Test Class

@isTest
public class ContactAccountOriginTest {
    static testMethod void insertNewAccount() {
        Account acctToCreate = new Account();
        Contact contToCreate = new Contact();

        acctToCreate.Name  = 'Practice Test';
        insert acctToCreate;

        //1st pass
        contToCreate.FirstName = 'test';
        contToCreate.LastName = 'test';
        contToCreate.AccountId = acctToCreate.Id;
        contToCreate.Focus_ID__c = '00001';
        insert contToCreate;
        update acctToCreate;

        //2nd pass
        contToCreate.FirstName = 'test';
        contToCreate.LastName = 'test';
        contToCreate.AccountId = acctToCreate.Id;
        contToCreate.Focus_ID__c = null;
        update contToCreate; 

        update acctToCreate;
    }
}
3
  • 4
    It would be nice if you could tell u what the issue is. Would make it much much easier to help you. Include error messages verbatim. Immediately I see DML inside a for loop and some extraneous code
    – Eric
    Commented Jun 8, 2017 at 23:55
  • If you don't know if your record has an Id or not, you can use upsert to say to the database "hey, update this thing if it exists, or insert it otherwise". So you can eliminate that last condition when you check the Id. Commented Jun 9, 2017 at 0:09
  • Fyi, a field coming from the database will never be equal to '' (the empty string); it will either be null or contain at least one non-white-space character. Also, no need to check if a list is empty before you iterate over it.
    – sfdcfox
    Commented Jun 9, 2017 at 4:14

1 Answer 1

0

I am assuming you are trying to update the exsisting account of the contact if it has a Account else create a new Account. Below is an example of how you can update the parent based on the child:

Although, you may consider using a Process builder which can update the parent record or even create new records. Might save you writing some lines of code.

trigger ContactAccountOriginTrigger on Contact (before insert,before update) {


    List<ID> IdList = new List<ID>();
    List<Account> accToUpdateOrInsert = new List<Account>();
    for(Contact c : trigger.new){
        if(c.accountid!=Null){
        idList.add(c.accountid);   
        }else{
           Account a = new Account(Name='WhatName');
           accToUpdateOrInsert.add(a);
        }
    }

    Map<Id,Account> accountMap = new Map<Id,Account>();
    List<Account> accList = [select id,Name,(select id, Focus_ID__c from Contacts) from Account where id in:idList];
    For(Account a : accList){
      if(a.contacts.size()>0){
        For(Contact c: a.Contacts){
            accountMap.put(c.id,a);          
        }
      }

    }

    For(Contact c:trigger.new){

        if(c.Focus_ID__c!=NULL){       
        Account acnt = accountMap.get(c.id);
        // Your Logic to do whatever with the account field
        accToUpdateOrInsert.add(acnt);       
        }else{
        Account acnt = accountMap.get(c.id);
        // Your Logic to do whatever with the account field
        accToUpdateOrInsert.add(acnt); 
        }

    }

    upsert accToUpdateOrInsert;

}
10
  • I like the thought of the process builder, granted I tried it and since our people are deciding not to assign primary contacts that's kinda dead, right? I'm getting another error when I try to deploy: Attempt to de-reference a null object Trigger.ContactAccountOriginTrigger: line 29, column 1 The logic is update some info, just a field for now, to a text string. The field is a picklist with set values. ex. acnt.Account_Origin__c = 'test'; I feel like an idiot. This seems so simple. my .net mind not working with this.
    – user39944
    Commented Jun 9, 2017 at 16:37
  • Does your pick list value has restricted values?
    – SfdcBat
    Commented Jun 9, 2017 at 17:04
  • Did you say you got null pointer when you try to deploy? or update a particular contact with a Account associated with it. Null pointer can't occur because the loop runs for contact with account data.
    – SfdcBat
    Commented Jun 9, 2017 at 17:12
  • Nope, no restricted values. Yes. it fails to validate when deploying. I see what you mean, but that's the error, which is why I'm a bit confused too.
    – user39944
    Commented Jun 9, 2017 at 17:21
  • Bit of a duh moment, I forgot one of fields. Thanks for the help, I need to get in the habit of using maps instead of relying on lists.
    – user39944
    Commented Jun 9, 2017 at 17:47

You must log in to answer this question.