+ Start a Discussion
Michael Webb 5Michael Webb 5 

Trigger to create record is creating duplicate records first time saved.

I have a trigger that will create a Ownership Record when a field is populated on the contact. It takes that field and the contact and creates the association and adds it to the related list.  It is working pretty well except the first time it runs ie. when the field is empty and I put in a value (lookup from the property object) it creates 2 Ownership records. Every other time it only creates one. I do not want the trigger to run if the field is empty obviously because then it would not be able to create the record. I am not sure what I am doing wrong here.  Any help is very much appreciated. 

rigger 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;
        }
    }
}
Best Answer chosen by Michael Webb 5
GlynAGlynA
It is very likely that there is a Workflow that uses a field update to modify the Contact.  This will cause the Contact trigger to run a second time, in turn causing a second Ownership record to be created.  One way around this is to create a static Boolean in a helper class and add something like this to your trigger:

<pre>
    if ( HelperClass.contactTriggerRan ) return;
    contactTriggerRan = true;
</pre>
 

All Answers

GlynAGlynA
It is very likely that there is a Workflow that uses a field update to modify the Contact.  This will cause the Contact trigger to run a second time, in turn causing a second Ownership record to be created.  One way around this is to create a static Boolean in a helper class and add something like this to your trigger:

<pre>
    if ( HelperClass.contactTriggerRan ) return;
    contactTriggerRan = true;
</pre>
 
This was selected as the best answer
RamuRamu (Salesforce Developers) 
Change these lines as below(Bold text) 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){



 if (c.One_Prop_Owned__c != oldContact.One_Prop_Owned__c && c.One_Prop_Owned__c != null)
      
Michael Webb 5Michael Webb 5
Changing that one line helped, it allows me to clear out the field if needed, however, the first time it still adds two ownership records. I checked the workflow rules there are none that run on the Contact at this time it is a very clean org has not gone into production yet. I am completely lost on the helperclass where would I put the helper class? Thanks again for any help it is so confusing. Michael Webb
Michael Webb 5Michael Webb 5
Aaaahhh it is a workflow, I am not sure how to get around it but it is a workflow.
Michael Webb 5Michael Webb 5
Any help on a test class?  It was a workflow that was causing the issue. I can't move it into production though, I thought this test class would cover it.

@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;
        }
        }