+ Start a Discussion
Osiris77706Osiris77706 

trigger Making Two Objects, instead of one

Okay so my code looks like this :

 

/* Apex Trigger: createNewProjectForOpportunnity
 * @ Author: Greg Coley
 * @ Contact: gjc5@students.pti.edu (or) gcoley@conairgroup.com
 *
 * @ Info: This trigger is designed to automatically generate an object of type
 *         SFDC_Project__ in salesforce.com.
 *
 */

// this trigger fires before the inserting or updating of a new object of type opportunity
trigger createNewProjectForOpportunity on Opportunity (before insert, before update)
{

 //Create an temporary variable of type SFDC_Project__c
 SFDC_Project__c tempProject;


  //Grab the Opportunity object that fired the trigger. assign to variable "opp"
  for (Opportunity opp: trigger.new)
  {
     
    //check to see if "opp" is null, it should'nt be, but just in case.
    if (opp != null)
    {
      //use SFDC_Project__c 's parameterized constructer to auto fill fields
      //using the data from "opp" 's fields.
      tempProject = new SFDC_Project__c(SFDC_Project_Name__c = opp.Name);
                                       
                                       
    //Check to see if tempProject 's SFDC_Project_Start_Date__C
    //is set to null. if this is the first creation of the project, the field will
    //be null. otherwise, the object will just be updating, and there is no
    //need to update the start date
    if(tempProject.SFDC_Project_Start_Date__c == null)
    {
        //asign project start date to today.
        tempProject.SFDC_Project_Start_Date__c = Date.Today();
    }
                                       
     
      // only upsert (which means insert if it doesnt exist already, and update
      // if it does exist) if the project object is not null, again it shouldnt be
      // but, just in case.
      if (tempProject != null)
      {
      upsert(tempProject);
      }

    
    }

  }

 
 

 

 

 
 }and instead of making just one object of type Project, it makes two that are identical, except ofcourse an auto number field....perhaps im missing a syntax mistake ?

Osiris77706Osiris77706

Nvm i gigured out the problem with that, but now im trying to make it so if i go and edit the oppurtunity, the corresponding project object is edited, and a new project object is not created.