You need to sign in to do that
Don't have an account?
Trying to do something simple with an Onclick Javascript button! Can't get it to work!!
Hi there,
I'm not a developer, but am trying to accomplish something with an Onclick Javascript Button in Salesforce.com for educational purposes. I feel like I'm getting close, but I'm still failing.
The objective here is the following:
Upon clicking this Onclick Javascript Button on the opportunity record, the following actions will occur:
1. Update a checkbox on the opportunity
2. Create a related quote with the quote name field populated to "PQ"
3. Return to the opportunity record when complete
I have the following code, which is only accomplishing 1 and 3:
{!REQUIRESCRIPT("/soap/ajax/16.0/connection.js")}
var update_Opp = []; /*Declaring an array to pass the parameters */
var oOpp= new sforce.SObject("Opportunity"); /* Declaring an object for the Case */
oOpp.Id='{!Opportunity.Id}'; /*setting the ID of the object to the Id of the current opp*/
oOpp. Quote_Requested__c = true; /* Setting the checkbox value as true */
update_Opp.push(oOpp); /*pushing the updated object in queue*/
result_Update=sforce.connection.update(update_Opp); /*updating the object*/
window.location.reload(); /* asking the page to refresh */
var newquote= new sforce.SObject("Quote");
newquote.Name = "PQ";
newquote.OpportunityID="{!Opportunity.Id}";
result = sforce.connection.create([newquote]);
alert(result );
window.location.reload();
Can anyone help with where I am screwing this up?
Also, how would I go about making these actions dependent on a condition? Say, if field x on the opportunity was null, prevent the actions from taking place.
Thank you for any help!!!!
Try this:
All Answers
Hello,
Try to see if the update and insert operations are succesful:
Also for a conditional branch just use the field value directly:
For debugging use Console.log(var)
Thank you,
Adrian
Try removing the first window.location.reload();
You are telling the script to reload the page before creating the new quote (essentially stopping the script).
Guys,
Thank you so much for the responses -
I have it working with the following:
{!REQUIRESCRIPT("/soap/ajax/27.0/connection.js")}
try{
/*Creating a New Quote*/
var quote = new sforce.SObject('Quote');
quote.Name = 'PQ';
quote.OpportunityId = '{!Opportunity.Id}';
var result = sforce.connection.create([quote]);
if(result[0].success=='true'){
alert('The Quote was Created Successfully.');
}
}
catch(e){
alert('An Un-expected Error had Occured.\r\nError: ' + e);
}
try{
/*Updating the Opportunity*/
var opp = new sforce.SObject('Opportunity');
opp.Id = '{!Opportunity.Id}';
opp.Quote_Requested__c = 'true';
opp.StageName = 'Proposal/Price Quote';
opp.Probability = '75';
result = sforce.connection.update([opp]);
if(result[0].success=='true'){
alert('The Opportunity has been Updated Successfully.');
}
}
catch(e){
alert('An Un-expected Error had Occured.\r\nError: ' + e);
}
/*Reloading the Opportunity Page*/
window.location.reload();
However, I'd like to have it be conditional on if a checkbox equals "TRUE" on the opportunity record.
Would anyone mind showing me how I could edit the code above to reject the actions and display a prompt if a checkbox on the opportunity equals "TRUE"? (specifically, the opp.Quote_Requested__c checkbox).
Thank you so much for your help!!!!
Try this: