You need to sign in to do that
Don't have an account?

How to add null value to contact lookup in account
Hi All,
I have a requirement to populate the active contact record id in Account object. For this, i have taken a contact lookup in account object. whenever user updates the active field, i have written a trigger to update the contact lookup field in account.
If any user deactivates the contact, then the contact lookup in account object should become empty. I am not able to fulfill this scenario.
Can any one please help me for this requirement.
Code:
I have a requirement to populate the active contact record id in Account object. For this, i have taken a contact lookup in account object. whenever user updates the active field, i have written a trigger to update the contact lookup field in account.
If any user deactivates the contact, then the contact lookup in account object should become empty. I am not able to fulfill this scenario.
Can any one please help me for this requirement.
Code:
trigger sample on contact(after insert,after update){ List<Account> accList = new List<Account>(); List<Account> accUpdateContactPerson = new List<Account>(); List<Id> contactIdList = new List<Id>(); Map<Id,Id> ContactAccountIdMap = new Map<Id,Id>(); accUpdateContactPerson.clear(); for(Contact con:cont){ if(con.Active__C == true){ contactIdList.add(con.Id); ContactAccountIdMap.put(con.AccountId,con.Id); } } accList = [select Id,Contact__c from Account where Id IN: ContactAccountIdMap.keySet()]; for(Account acc:accList){ if(contactAccountIdMap.get(acc.Id)!=null){ acc.Contact__c = contactAccountIdMap.get(acc.Id); } else{ acc.Contact__c = null; } accUpdateContactPerson.add(acc); } update accUpdateContactPerson; }
NOTE: This code has not been tested and may contain typographical or logical errors
Thanks for the reply.
Yes, it will work for one record. But, i want for bulk records. Can you please let me know is there any way to implement this?
Thanks for the information.
Suppose, if user wants to deactivate all the contacts in bulk due to some data issue.In this type of case, do we have any workaround in the above code.