0

We have a field on the Contact form called One Property Owned. This field will be updated in a list view so that one person can go through all of them in a fell swoop. There are two other custom objects one named Property, this is the lookup on the contact. They lookup the property name and it associates. In order to have a one to many association we have another object called Ownership, very simple it has the contact as a lookup and the Property as a lookup then puts them in a related list. So I need to create a trigger when the property lookup field One Property Owned, that will create an ownership object with the property in the looking and the contact associated. I have written quick ones to create cases, off an account but that is about it.

Below is what I have so far:

trigger PropOwned on Contact (after update) {
    for (Contact c : Trigger.new){
        McLabs2__Ownership__c ownNew= new McLabs2__Ownership__c();
        Contact oldContact = Trigger.oldMap.get(c.id);
        if (c.One_Prop_Owned__c != oldContact.One_Prop_Owned__c)
            if (c.One_Prop_Owned__c == null || c.One_Prop_Owned__c == c.One_Prop_Owned__c){
                ownNew.McLabs2__Contact__c = c.id;
                ownNew.McLabs2__Property__c = c.One_Prop_Owned__c;
                insert ownNew;
            }
    }
}

The first time it runs it adds 2 Ownership records every other time when I change that field it only adds one. I am not sure why it is adding 2 the first time.

Thank you!

2
  • What have you got so far?
    – Matt Lacey
    Commented Nov 5, 2014 at 8:43
  • So if One_Prop_Owned__c is set to null you want to do nothing right? And if you set a new value then it should create a new ownership object but it's creating two?
    – Matt Lacey
    Commented Nov 10, 2014 at 21:30

1 Answer 1

0

I believe the problem is probably happening because of recursive firing of your trigger(possibly a workflow or other item). Adding a static boolean variable in a separate class should solve the problem if this is the case.

trigger PropOwned on Contact (after update) {

    if(externalclass.hasrun==false){ 

       for (Contact c : Trigger.new){
            McLabs2__Ownership__c ownNew= new McLabs2__Ownership__c();

            Contact oldContact = Trigger.oldMap.get(c.id);

            if (c.One_Prop_Owned__c != oldContact.One_Prop_Owned__c)

                if (c.One_Prop_Owned__c == null || c.One_Prop_Owned__c == c.One_Prop_Owned__c){

                    ownNew.McLabs2__Contact__c = c.id;

                    ownNew.McLabs2__Property__c = c.One_Prop_Owned__c;

                    insert ownNew;
                }
        }

    }

    externalclass.hasrun=true;
}
3
  • Thank you for the reply. I checked on a workflow that is not causing it, there is another trigger though but since it is an installed package (force.com app) I can't see the trigger. I am not sure exactly what I should do above, do I have to write an external class called hasrun? If so any clue as to how to do that? Thank you! Commented Nov 11, 2014 at 16:58
  • It is a workflow, how do I get around that though? Commented Nov 11, 2014 at 18:24
  • Well this worked now I am having a problem with the test class :-) @isTest public class TestOwnership { static testMethod void ownershipCreator() { McLabs2__Ownership__c ownNew= new McLabs2__Ownership__c(); ownNew.McLabs2__Contact__c = 'Michael Webb'; ownNew.McLabs2__Property__c = '131 West 33rd Street'; insert ownNew; } } Commented Nov 11, 2014 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .