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
WillyumWillyum 

How to link spouse records in lookup fields (one to one relationship)

Can anyone tell me exactly how I can create a lookup from one person account record to another in a one to one manner.  I want to create a field called spouse and have it lookup to another person account.  But after I do that, I want the recipricol spouse field to populate on the other record.  I don't want a related list, just a spouse lookup.  I've seen it done in another org but don't access to the developer.

 

HELP.

RArunrajRArunraj

Hi,

 

To do this functionality, You need to write a trigger on the corresponding object. Currently I have done the same for Account object not for person Account. Below are the code,

 

Trigger:

 

trigger ReciprocalSpouseField on Account (after insert, after update) {        

 

List<Account> actList = new List<Account>();        

for(Account a: Trigger.new) {        

if(a.spouse__c != null) {                    

 if (!ReciprocalSpouseController.hasAlreadyUpdated()) {                

if(Trigger.oldMap.get(a.id).spouse__c != null && Trigger.oldMap.get(a.id).spouse__c != a.spouse__c ) {

                Account act = new Account(id=Trigger.oldMap.get(a.id).spouse__c,spouse__c=null);                     actList.add(act);                

}                              

 Account act = new Account(id=a.spouse__c,spouse__c=a.id);                

actList.add(act);            

}        

}     

}      

 

if(actList.size() > 0) {        

ReciprocalSpouseController.setAlreadyUpdatedOn();        

update actList;    

}


}

 

Apex Class

 

public class ReciprocalSpouseController {
    public static boolean alreadyUpdated = false;         

public static boolean hasAlreadyUpdated() {        

return alreadyUpdated;    

}        

public static void setAlreadyUpdatedOn() {        

alreadyUpdated = true;    

}
}

 

Thanks,

Arunraj

RArunrajRArunraj

Did you execute the above code?

 

Thanks,

R. Arunraj

WillyumWillyum

H i RArunraj,

 

Thank you for the code.  I tried it but it didn't work and I think that's because of two reasons.  1)  I don't know how to exactly modify the code for person accounts.  When I copied the trigger information in, it gave me an error.  The second reason is that I don't know how to install a trigger as I don't lknow development, only basic admin functions.

 

Any thoughts.