+ Start a Discussion
@anilbathula@@anilbathula@ 

Lookup field to be update on contact

Hi guys,

 

please help me in this case.

 

I am working with contact object .There is a lookup field called spouse on to contact.(self lookup). 

Here if a contact record name='Daniel'.

And spouse in the record of daniel is 'Mary'.

if i click mary lookup feild the daniel should be the spouse in the mary record.

it means the lookup value should change vice versa.

 

How can i achieve this with trigger plz help me with the trigger.

 

 

Thanks

Anil.B

Best Answer chosen by Admin (Salesforce Developers) 
rohitsfdcrohitsfdc

Hi,

You need to create a static class for preventing recursive trigger.

code will be like this :

 

static class:
public class staticFlag {

public static boolean a = true;

}

trigger:
trigger MatchSpouse on Contact (after insert, before update) {
    List<Contact> spouses = new List<Contact>();
    Contact spouse;
        for (Contact c : Trigger.new){
        if (c.Spouse__c != null){
        
            spouse = [select id,Spouse__c from Contact where Id=: c.Spouse__c ];
          
           if(spouse.Spouse__c != c.id)
           {
            spouse.Spouse__c = c.id;
            spouses.add(spouse);
           }
        }
    }
    if(spouses.size()>0 && staticFlag.a ){
        staticFlag.a = false;
    update spouses;

    }
}

 Please make it bulk trigger, if you are planning to work on bulk records.

 

 

 

All Answers

Scott_VSScott_VS

A simple trigger should do it.

 

trigger MatchSpouse on Contact (before insert, before update) {
    List<Contact> spouses = new List<Contact>();
    for (Contact c : Trigger.new){
        if (c.Spouse__c != null){
            Contact spouse = new Contact();
            spouse.id = c.Spouse__c;
            spouse.Spouse__c = c.id;
            spouses.add(spouse);
        }
    }
    update spouses;
}

 

@anilbathula@@anilbathula@

Hi scott,

 

Thanks for ur response.

 

         This trigger is not saving its throughing an error at line 6.

          Error: Compile Error: Field is not writeable: Contact.Id at line 6 column 13.

         

          if i comment line 6 its saving and on record its throughing error as:-

          first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.MatchSpouse: line 11, column 1.

 

          

Thanks 

Anil.B

 

Scott_VSScott_VS

My bad. Try this one.

 

trigger MatchSpouse on Contact (after insert, after update) {
    // Gather spouse ids
    List<String> spouseIds = new List<String>();
    for (Contact c : Trigger.new){
        if (c.Spouse__c != null){
            spouseIds.add (c.Spouse__c);
        }
    }
    
    // Query spouses
    Map<id, Contact> spouses = new Map<id, Contact>([SELECT id FROM Contact WHERE id =: spouseIds AND Spouse__c = null]);
    
    // Set contacts to spouses
    for (Contact c : Trigger.new){
        if (c.Spouse__c != null && spouses.get(c.Spouse__c) != null){
            spouses.get(c.Spouse__c).Spouse__c = c.id;
        }
    }
    
    update spouses.values();
}

 

rohitsfdcrohitsfdc

Above code is Not working properly.

rohitsfdcrohitsfdc

Hi,

You need to create a static class for preventing recursive trigger.

code will be like this :

 

static class:
public class staticFlag {

public static boolean a = true;

}

trigger:
trigger MatchSpouse on Contact (after insert, before update) {
    List<Contact> spouses = new List<Contact>();
    Contact spouse;
        for (Contact c : Trigger.new){
        if (c.Spouse__c != null){
        
            spouse = [select id,Spouse__c from Contact where Id=: c.Spouse__c ];
          
           if(spouse.Spouse__c != c.id)
           {
            spouse.Spouse__c = c.id;
            spouses.add(spouse);
           }
        }
    }
    if(spouses.size()>0 && staticFlag.a ){
        staticFlag.a = false;
    update spouses;

    }
}

 Please make it bulk trigger, if you are planning to work on bulk records.

 

 

 

This was selected as the best answer
Scott_VSScott_VS

rohit_girdhar wrote:

Not working properly.



How so? I looks good on my sandbox.

rohitsfdcrohitsfdc

Scott_VS wrote:

rohit_girdhar wrote:

Not working properly.



How so? I looks good on my sandbox.


 

when you select spouse__c which already has a spouse__c, this code will not overwrite it.

Scott_VSScott_VS

when you select spouse__c which already has a spouse__c, this code will not overwrite it.



Hmm... good point. With the divorce rate as high at it is, you probably will have a lot of people changing their spouses. ;-)

rohitsfdcrohitsfdc

Scott_VS wrote:

when you select spouse__c which already has a spouse__c, this code will not overwrite it.



Hmm... good point. With the divorce rate as high at it is, you probably will have a lot of people changing their spouses. ;-)


:-) .
 

@anilbathula@@anilbathula@

Thanks Scott and Rohit u helped me a lot thank u very much guys.

 

Thanks 

Anil.B