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
Malhar_Ulhas_AgaleMalhar_Ulhas_Agale 

How to delete Accountcontact relationship record from apex ?

Hi i have desinged a vf page to add multiple accounts records for a particular contact using related accounts at same time. 

 But now i also have to delete existing relationships in the related accounts for the contact at the same time. 
Since the account contact relationship object doesnt have an ID field, also the account ID and contactID lookup are not editable fields.

So how can i delete the accountcontactrelationship record without deleting the associated contact or account ?

Please let me know.

Thanks,
Malhar Ulhas Agale.
ManojjenaManojjena
HI Malhar_Ulhas_Agale,

Here bit confusing ,your requirmenet is to delete the relationship or you want to reparent means you need to trasfer the contacts of account a to account B .
The relationship field between Account and Contact is AccountId , 

1. If you want to remove the relationshipship then make the AccountId=null ,
2.If you  want to reparent or trasfer the contacts from  one account to another then simply update th eAccountId field with different Account's Id field .

let me know if it helps .

Thanks 
Manoj

 
Malhar_Ulhas_AgaleMalhar_Ulhas_Agale

Hi Manoj,

Thanks for your answer. But i figured out how to delete the records of accountcontactrelation object. I just queried the records of accountcontact relation for the particular contact and compared whether it contacined the certain account id. Based on it i populated it in an list<accountcontactrelation> , then fired the delete DML operation.

Following is the code which i used:
 

/*
"removal" is an list<id> which contain the ID's of Accounts which i want to compare as shown below.

string URL_contact_ID = "Yours Contact's ID";

*/

list<AccountContactRelation> Delete_from_obj = new list<AccountContactRelation>(); 

for(AccountContactRelation ac_cn_rel : [select id,AccountId from AccountContactRelation where ContactId =: URL_contact_ID]) {
     if(removal.contains(ac_cn_rel.AccountId)){
              Delete_from_obj.add(ac_cn_rel);
      }
}

Delete Delete_from_obj;
In this way i solved the problem.