function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NickodemusNickodemus 

Trigger new custom object record upon Contact update

Hi all,

I'm really new to Apex, so totally clueless right now...

I want to create a related list on Contact that stores all previous Accounts that a Contact has been associated with... To solve this I've created a 'junction' object (called 'Associated Account')) as a M-D of Contact with the following fields:
  • Associated Account Name (Auto Number)
  • Contact (M-D)
  • Account (lookup)
  • Start Date (required)
  • End Date
  • Currently Active (Checkbox - checked by default)
When the Account lookup field on the Contact record is updated, Is it possible to write a trigger that will update a current 'Associated Account' record to uncheck the 'Currently Active' checkbox, and populate the 'End Date'. Then also create a new 'Associated Account' record with the new Account, and Start Date?

In theory it would give an Account history in the related list of each Contact.... is this the best way to attack this challenge?

Many thanks,

Nick
SantoshChitalkarSantoshChitalkar
Hi Nick,

I guess you can achieve this using trigger. following psuedo code can work - 
1) create after update & after insert trigger on contact
2) upon insert, check the contact.AccountID. If it is not null then create a new record of associated account.
3)upon update, check for old contact.AccountId &new contact.AccountId if they are different then insert associated account.

regards,
Santosh
Mudasir WaniMudasir Wani
Hey Nick,

You can use the following code.

Trigger
trigger contactTrigger on Contact (after update) {
    if (Trigger.isAfter && Trigger.isUpdate){
        system.debug('Mudasir it is working');
        Contacthandler.handleAfterUpdate(trigger.oldmap,trigger.newmap);
    }
}
Handler Class
public without sharing class Contacthandler{
    public static void handleAfterUpdate(map<Id,Contact> oldValues, map<Id,Contact> newValues){
        system.debug('We are in handleAfterUpdate');
        Map<Id,id> accIdMapValues = new Map<Id,Id>();
        Set<Id> contactIdSet = new Set<Id>();
        Set<Id> accountIdSet = new Set<Id>();
        for(Contact conRec : newValues.Values()){
            if(conRec.AccountId != oldValues.get(conRec.Id).AccountId){
                accIdMapValues.put(conRec.id, oldValues.get(conRec.id).AccountId);
                contactIdSet.add(conRec.idd);
                accountIdSet.add(conRec.AccountId);
            }
        }
        updateAssociatedAccounts(accountIdSet);
        createAssociatedAccounts(contactIdSet);
    }
    
    public static void updateAssociatedAccounts(Set<Id> accountIdSet){
       //Fetch all accounts and update the necessary fields
        List<Account> accList = [Select id,name,cretedDate from Account where Id IN: accountIdSet];
    }

    public static void createAssociatedAccounts(Set<Id> contactIdSet){ 
        //Create your associated accounts here and insert 
      }

}


Donot forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

 
NickodemusNickodemus

Thanks Mudasir Wani.

I've put that code into my org, but I'm afraid, i don't really understand what it's doing, and I don't know what to put into the createAssociatedAccounts method. As i say, unfortunately, I know very limited APEX, and although i know what i want it to do, i don't know how to get it done :( Any further help would be AMAZING... if you've got the time.

Many thanks.
Mudasir WaniMudasir Wani
Hey Nick,

What i understood is that you want to have contacts associated to multiple accounts.
But now you have choosen this approach 

There is another way to do it have a look may be this will help
https://success.salesforce.com/answers?id=90630000000gvRIAAY


Approach --

Contact Roles is the only standard feature, through which you can associate a contact with multiple accounts, Cases and Opportunites. 

1. You need to add Contact roles related list on Account page layouts,
2. You can edit  Contact Role list from Contact role settings
Setup>Customize>Accounts>Contact Roles
3. When you click on 'New' button from Contact role related list, you will be able to lookup a contact and set role and save it under account. 

However  I have another solution for you.
You an create a "Contact Role"  custom  object . Here could be the field details
1. Name  field could be auto number, (You do not need Name field)
2. Contact Name : Lookup to contact 
2. Account Name : Lookup to account
3 Picklist (In which you mention Roles)

You will be able to see/edit Contact Role(Custom) related list  below contact and Account both pages. 


If this doesnot help then we can work on trigger.


Donot forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

 
NickodemusNickodemus
Thanks Mudasir, but I did try Contact Roles and it didn't provide me with enough flexibility. My 'Associated Account' custom object is what i'm using to get around that problem, but now I'm trying to add the functionality to trigger new records in a type of 'log' of previously associated accounts.

From what i can tell, the trigger is going to be the best way to accomoplish this, but I'm not knowledgeable enough to write the Apex code...