Skip to main content
Based on @sfdcfox comments , I have removed checking of blank (' ') n the if loop
Source Link
SfdcBat
  • 4.8k
  • 28
  • 105
  • 213

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 || c.Focus_ID__c == ''){       
        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;

}

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 || c.Focus_ID__c == ''){       
        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;

}

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;

}
Source Link
SfdcBat
  • 4.8k
  • 28
  • 105
  • 213

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 || c.Focus_ID__c == ''){       
        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;

}