1

I am trying to adapt this: https://developer.salesforce.com/forums/?id=906F0000000BRKWIA4

to work on a custom object, rather than the accounts object.

Here is my apex:

trigger primaryContactOwnership on MRG_Ownership__c (after insert,after update) {
List<MRG_Ownership__c> acclist = new List<MRG_Ownership__c>();
set<Id> accIdSet = new set<Id>();
Set<Id> ContactIds = new Set<Id>();
if(Trigger.IsUpdate){
for ( MRG_Ownership__c s : trigger.new ){
system.debug('*******Runish'+s.Property__r.name);
        if(s.Property__r.name != null)
        accIdSet.add(s.Property__r.name);   
        MRG_Ownership__c oldcon = Trigger.oldMap.get(s.Id); 
        system.debug('*******Runish'+oldcon);      
        if(oldcon.id != null)
        ContactIds.add(oldcon.id);

                                }

acclist=[select id, name,Property__r.name,Primary_Contact__c from MRG_Ownership__c where Property__r.name IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<MRG_Ownership__c> conlist=new List<MRG_Ownership__c>();
MRG_Ownership__c c1=new MRG_Ownership__c();

 if(checkRecursive.runOnce()){
        for ( MRG_Ownership__c s : Trigger.new)

             {         
            if(s.Primary_Contact__c == true)
                {
                  for(MRG_Ownership__c a :acclist)
                      {
                         a.Primary_Contact__c = false;
                         c1=a;      
                         update c1;      
                      }

                    //update c1; 
                } 

              }
           update acclist;
                       }
     }


      ////next is trigger for after insert


        if(Trigger.isInsert){
                for ( MRG_Ownership__c s : trigger.new ){
                        if(s.Property__r.name != null)
                        accIdSet.add(s.Company__r.id);   
                        MRG_Ownership__c oldcon = Trigger.newMap.get(s.Id);       
                        if(oldcon.id != null)
                        ContactIds.add(oldcon.id);

                                                }

acclist=[select id, name,Property__r.name,Primary_Contact__c from MRG_Ownership__c where Property__r.name IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<MRG_Ownership__c> conlist=new List<MRG_Ownership__c>();
MRG_Ownership__c c1=new MRG_Ownership__c();

 if(checkRecursive.runOnce()){
for ( MRG_Ownership__c s : Trigger.new)
         {      
            if(s.Primary_Contact__c ==true)
                {
                  for(MRG_Ownership__c a :acclist)
                          {
                             a.Primary_Contact__c =false;
                              c1=a;   
                                                   update c1;          
                          }

        }  

       }
       update acclist;
       }

        }
         }

The object is a custom object called "Ownership" that is assigned to another custom object, "Property". Each Ownership object also is related to a contact. The Ownership object acts as a sort of "bridge" between property and contact. This Ownership object also has a checkbox for "Primary Contact" However, only one contact for each property should be the "Primary Contact", and when this "Primary Contact" checkbox is set to true, he checkbox on all other Ownerships on that property should go back to false. I hope this makes sense. Here's some pics to help explain: enter image description here enter image description here enter image description here enter image description here

I have tried using a trigger on both the Contact and custom object, however so far no luck.

1

Since the field Primary_Contact__c is on Ownership object. The logic should definitely be on Ownership object. Here is my code without looking too much at your code and I haven't tested it (but should be working)

trigger primaryContactOwnership on MRG_Ownership__c (after insert,after update) 
{
    if(Util.inOwnershipTrigger)
        return;

    Util.inOwnershipTrigger = true; //Don't forget to use this mechanism since you are going to update ownership object in this trigger

    List<Id> primaryOwnershipIdList = new List<Id>();
    List<Id> relatedPropertyIdList = new List<Id>();

    for(MRG_Ownership__c ownership : Trigger.New)
    {
        if(ownership.Primary_Contact__c == true && (Trigger.Old == null || Trigger.OldMap.get(ownership.Id) == null 
        || Trigger.OldMap.get(ownership.Id).Primary_Contact__c != true)) // To handle null scenario
        {
            primaryOwnershipList.add(ownership.Id);
            relatedPropertyIdList.add(ownership.Property__c);
        }
    }

    List<MRG_Ownership__c> relatedOwnershipList = [Select Id, Primary_Contact__c 
                                                    From MRG_Ownership__c 
                                                    Where Property__c in :relatedPropertyIdList
                                                    And Primary_Contact__c = true
                                                    And Id Not in :primaryOwnershipList];

    if(relatedOwnershipList.size() > 0)
    {
        for(MRG_Ownership__c ownership : relatedOwnershipList)
            ownership.Primary_Contact__c = false;

        update relatedOwnershipList;
    }
}

And I looked at your code again after finishing this. I am 100% sure about why your code doesn't work but here are some suggestions:

  1. I don't see the point you need to separate your insert and update code in this case.
  2. I kind of feel you should put checkRecursive.runOnce() at the top of your trigger.
  3. Don't put DML sentence in the loop.

Feel free to let me know if the above code doesn't work, which I don't see the reason it would happen for now. But there might be something I don't see.

  • I am getting the following error: Variable does not Exist: Util.inOwnershipTrigger I'm not sure what this is. – MRG Jan 13 '16 at 17:46
  • You need to define that in a class as a static variable and initialize it to be false. This is one of the ways to prevent recursive execution of your trigger. You can also use checkRecursive.RunOnce() if you want – Lance Shi Jan 13 '16 at 22:47
  • I see. I've used checkRecursive.RunOnce() in the past for that purpose. Anyways your code is working for me. Thanks for the help! – MRG Jan 14 '16 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.