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
sumit dsumit d 

Relate Contacts to Account

Hi All,
I have a requirement in which i want to map contacts related to An Account i have a field Zd_Organisation_id__c on Contact
which is Zendesk_Id__c on Account(like AccountId on contact and Id on Account) i have values in this field on contact.
i want to relate contacts related to a particular Account using this field. (Like if Any contact have Zd_Organisation_id__c =256148307
than my requirement is to relate All the contact which have this '256148307' as Organization Id should show on Contact related list on Account).
How to do it using Apex class?
Any suggestion?
Best Answer chosen by sumit d
Mohan ChanderMohan Chander

Hi Sumit,

If you want to show as a related list i am afraid there is no other alternative apart from building some custom component and embedding it the page which would obviously consume lot of efforts.

 

so you need to write logic which is similar to this in your batch or trigger  (ZdOrganization__c assume this to be the look up field)


if(Account.Zendesk_Id__c == contact.Zd_Organisation_id__c) {
contact.ZdOrganization__c = AccountId
}

 

 

All Answers

Mohan ChanderMohan Chander

Hi Sumit,

In order to show Contact related list on Account you need to create a look up field on Contact to Account. So let us see how to achieve this requirement.

 

Create a lookup field on Contact to Account and call it Zd_Organization__c.

create Zd_Organisation_id__c this field as formula field = Account.Zendesk_Id__c 

No how you populate this Zd_Organization__c field on Contact is upto your requirement. You might want to run a batch to update existing records or add some logic in trigger that is up to you.

 

Once you do this you have 2 related lists of contacts on Account Record Page. (Assuming you have setup the required configuration on the Page Layouts). You first related list shows you normal contacts related to Account. Second related list shows you the related contacts specified by your requirement.

Try it out in your dev org. Hope this helps.

Regards,
Mohan

sumit dsumit d
is there any other method also?
sumit dsumit d
i have already Zd_Organization__c field on contact which is text .and have value already which is one of Account's Zendesk_Id__c . but the issue is they are not related because i migrate from external source
Mohan ChanderMohan Chander

Hi Sumit,

If you want to show as a related list i am afraid there is no other alternative apart from building some custom component and embedding it the page which would obviously consume lot of efforts.

 

so you need to write logic which is similar to this in your batch or trigger  (ZdOrganization__c assume this to be the look up field)


if(Account.Zendesk_Id__c == contact.Zd_Organisation_id__c) {
contact.ZdOrganization__c = AccountId
}

 

 

This was selected as the best answer
sumit dsumit d
Hi Mohan,
i want that when i run a query on query editor it should show me related contact of that Account using organization_id?
is it Possible?
Any suggestions?
Mohan ChanderMohan Chander

Hi Sumit,

 

Select Id, Name, (select Id, FirstName, LastName from Contact where Zd_Organisation_id__c  = 'someId') from Account where organization_id = 'someId'

 

replace 'someId' with actual id you should get Account with related contacts

Regards,
Mohan

sumit dsumit d
Hi mohan,
i did this 
for(Contact con : conList ){
  if(contact.Zd_Organisation_id__c == Account.Zendesk_Id__c) {
contact.AccountId = Account.id;

is it correct ? , can i relate the contacts to Account using this condition? or should i use set and Map?
Guide me?
sumit dsumit d
Map<String,id> MapZendestIdToAccId = new MapZendestIdToAccId([select id, Zendesk_Id__c, name, 
                                                              (select id, Zd_Organisation_id__c, name from Contacts)
                                                              from account where Zendesk_Id__c != null
                                                             ]);
                                                              for(Contact con : conList ){
                                                                  MapZendestIdToAccId.get(con.Zd_Organisation_id__c);
                                                                  if(contact.Zd_Organisation_id__c == Account.Zendesk_Id__c) {
                                                                      contact.AccountId = Account.id;
                                                                  } 
                                                              }
is it correct?