function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jmaskell123jmaskell123 

Creating Renewal Opportunities

Hi Does anyone know how to edit the following code to add 365 days to the close date of the new opp.

 

The Code i have so far:

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.Description, 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 = "Renewal - {!Opportunity.Name}";

// ** EDIT THESE FIELDS TO SET DEFAULT ANY VALUES **
newOpp[0].StageName = "Prospecting";
newOpp[0].CloseDate = new DATE((TODAY()-CloseDate)+365)
newOpp[0].Type = "Renewal"

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

if (saveResult[0].getBoolean("success")) {
newOpp[0].id = saveResult[0].id;
alert("Renewal Opportunity Has Been Created.");
}
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 );
}

 

But i am getting Undefined message.

 

Thanks if someone can help im sure its somthing simple i must be missing ?

 

 

Kind Regards,

 

Jake


zen_njzen_nj

In your setting of value, you have this:

 

   newOpp[0].CloseDate = new DATE((TODAY()-CloseDate)+365)

 

So first of, did you just want the CloseDate of new Opportunity to be Today plus 365 or you need to get the CloseDate value of the old Opportunity?

 

If you just want the CloseDate of the new Opportunity to be 365 from today, then it should just be

 

   newOpp[0].CloseDate = new DATE((TODAY()+365)

 

 But if you need to use the CloseDate of the original Opportunity entry, then I don't see an o.CloseDate in your query the CloseDate field:

 

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

jmaskell123jmaskell123

Thanks for your help i have added that code.

 

 

newOpp[0].CloseDate = new DATE((TODAY()+365)

 but now i am reciving an error when clicking the button, i have attached a screenshot.

 

 


 

 
Hope you can help
 
Thanks
 
Jake 
Message Edited by jmaskell123 on 09-14-2009 01:49 AM
zen_njzen_nj

Sorry I think I had an extra parenthesis.

It should just be

 

newOpp[0].CloseDate = new DATE(TODAY()+365)

 and not

newOpp[0].CloseDate = new DATE((TODAY()+365)

 

jmaskell123jmaskell123

Thanks i have tried what you said and now i am reciving a differnt error message ( see screenshot below ) when clicking the button.

 

http://img36.imageshack.us/img36/2040/error2u.jpg

 

here is my full code :

 

 

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.Description, 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 = "Renewal - {!Opportunity.Name}";
newOpp[0].CloseDate = new DATE(TODAY()+365);

// ** EDIT THESE FIELDS TO SET DEFAULT ANY VALUES **
newOpp[0].StageName = "Prospecting";
newOpp[0].Type = "Renewal"


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

if (saveResult[0].getBoolean("success")) {
newOpp[0].id = saveResult[0].id;
alert("Renewal Opportunity Has Been Created.");
}
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 );
}

 

 

 

 

any ideas ?

 

Thanks

 

Jake

Message Edited by jmaskell123 on 09-17-2009 03:54 AM
zen_njzen_nj

just did a quick lookup on apex code reference, try using this

 

newOpp[0].CloseDate = date.today()+365; 

 

instead of this:

 

newOpp[0].CloseDate = new DATE(TODAY()+365); 

 

jmaskell123jmaskell123

Thanks for your help with this i have changed the code to

 

newOpp[0].CloseDate = date.today()+365;

 

as you said but i still get "undifined" message when i click the button!

 

Thanks

 

Jake 

zen_njzen_nj

you are deploying this in sandbox, right?

So when you are saving this apex code, you aren't getting any syntax error, but when you are actually running it, somehow you are getting this error?

Usually if there is a syntax error, when you try to save the apex code, it will tell you which line and column number is giving you the error.

 

Did you click on the Run Test button of  your apex code to see if it ran successfully, or are you saying when you did that, it just returned with the Undefined error? that shouldn't be the output from Run Test, it should also be giving you a line number and description of error.

 

jmaskell123jmaskell123

I am currently testing the Javascript button in Developer edditon of Salesforce before i create it in our live system.

 

here is a screenshot of me edditing the button and i do not see a "Run Test" button sorry? 

 

http://img7.imageshack.us/img7/7279/syntax.jpg

 

Thats correct there are no sytax error but once i have saved it and go to run the button on an opportunity i am getting the error message.

 

Thanks

 

Jake 

 

 

zen_njzen_nj

I see. Sorry I misunderstood this from the beginning  - I thought you were using apex codes and triggers to do this.

Unfortunately I am not familiar enough with deploying javascripts (I use either url or visualforce pages) - maybe someone with javascripts expertise can help out here?