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
AndySureDepositAndySureDeposit 

associate a contact with more than one account

How can I associate a contact with more than one account? I am using the developer edition.

 

Instead of

 

contact.AccountId = strAccountId;

 

I need

 

contact.AccountId[0] = strAccountId1;
contact.AccountId[1] = strAccountId2;


Thanks,
Andy

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Just to flesh that out further...

 

You can't have    contact.Account[0], contact.Account[1] because Salesforce doesn't define them that way.  So, as Sati says, you can create your own:

 

contact.myAccount0

contact.myAccount1

etc. where each is a different field.

 

Another option which is pretty ugly but might be better for you if you don't know how many Account/Contact realtionships you want and most of your interactions with this data will be programmatic, is to create a "joining object", something similar to OpportunityContactRoles.

 

You could create an AccountContactRole object which has only a Name field (autonumber), and two lookup fields, one to an Account and another to a Contact.   Not pretty, but just a different option.

 

Best, Steve.

All Answers

shillyershillyer

You could create multiple Lookup Relationships to the Account object.

 

Hope that helps,

Sati

SteveBowerSteveBower

Just to flesh that out further...

 

You can't have    contact.Account[0], contact.Account[1] because Salesforce doesn't define them that way.  So, as Sati says, you can create your own:

 

contact.myAccount0

contact.myAccount1

etc. where each is a different field.

 

Another option which is pretty ugly but might be better for you if you don't know how many Account/Contact realtionships you want and most of your interactions with this data will be programmatic, is to create a "joining object", something similar to OpportunityContactRoles.

 

You could create an AccountContactRole object which has only a Name field (autonumber), and two lookup fields, one to an Account and another to a Contact.   Not pretty, but just a different option.

 

Best, Steve.

This was selected as the best answer
SJTECH2009SJTECH2009

The Best Option is U can take the multi -add page functionality. it will solve this issue.

AndySureDepositAndySureDeposit

Thanks. I followed Steve’s instructions verbatim. Everything worked. Below is the code.

 

contactaccount = new ContactAccount__c();
contactaccount.Account__c = strAccountId;
contactaccount.Contact__c = strContactId;
string strContactName;
strContactName = ds.Tables[0].Rows[j]["firstname"].ToString() + " " +ds.Tables[0].Rows[j]["lastname"].ToString();
contactaccount.Name = strContactName + " - " + strAccountName;
contactaccounts[j] = contactaccount;
SaveResult[] sr2 = binding.create(contactaccounts);
if (sr2[0].success)
{
Console.WriteLine("A contactaccount was created with an id of: " + sr2[0].id);
      }
}

absabs

I generally use the juction object (the one steve mention) for this kind of problems.

 

 

What is this multi add page feature? Can you eplain a bit more ?

 

I would love to know if something is better than junction object to solve this kind of problem. 

 

Regards,

Sunil Matta