+ Start a Discussion
TheMaryCTheMaryC 

Clone Opportunity

Below is some Apex code I found to copy an opportunity.  However, I have a custom field "Membership_Expires" on the opportunity tab.  I would like the new opportunity to add 365

 

 

try{

   {!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}

 

   // ** EDIT THIS QUERY TO LIST THE FIELDS YOU WANT TO COPY **

   var result = sforce.connection.query("Select o.Type, o.StageName,  o.Membership_Expires__C, o.CampaignId,  o.Amount, o.AccountId From Opportunity o WHERE o.Id = '{!Opportunity.Id}'");

   var newOpp = result.getArray("records");

 

   // Reset the Opp Id and reset fields to default values

   newOpp[0].Id = '';
   newOpp[0].Name = " {!Opportunity.Name}";
  

   // ** EDIT THESE FIELDS TO SET DEFAULT ANY VALUES **
   newOpp[0].StageName = "Pledge";
  newOpp[0].CloseDate = new Date(2099, 0, 1);
  

  newOpp[0].Membership_Expires__c  = {!Opportunity.Membership_Expires__c}

//This was my attempt to get the field to copy but that failed... once I got it to copy I was going to fiquer out how to add 365 days to the filed// 

 

   var saveResult = sforce.connection.create(newOpp);

   if (saveResult[0].getBoolean("success")) {
      newOpp[0].id = saveResult[0].id;
      alert("Opportunity cloned without line items");
   }
   else {
      alert("Failed to create clone: " + saveResult[0]);
   }

   // Refresh the page to display the new oppportunity
   window.location = newOpp[0].id;
}
catch (err) {
   alert (err.description );
} days to this custom field.  

Eric_SantiagoEric_Santiago

First, this is Ajax or Scontrol code not Apex. Second use 

 

 

newOpp[0].Membership_Expires__c = {!Opportunity.Membership_Expires__c + 365}

 


 

 

to update that value.