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
John BraunJohn Braun 

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!!!!

Best Answer chosen by Admin (Salesforce Developers) 
CaptainObviousCaptainObvious

Try this:

 

<script type="text/javscript">

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

var quoteRequested = '{!Opportunity.Quote_Requested__c}'; 

if (quoteRequested==true) { 

    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();

} else {

    alert('Quote Requested was not checked');

}
 
</script>

 

All Answers

AdrianCCAdrianCC

Hello,

 

Try to see if the update and insert operations are succesful:

if (result[0].success=='false') 
{ 
alert(result[0].errors.message);
} 
else //do stuff

 Also for a conditional branch just use the field value directly:

if ({!Opportunity.Field} != null) ...

 For debugging use Console.log(var) 

 

Thank you,

Adrian

 

 

 

 

CaptainObviousCaptainObvious

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).

 

 

John BraunJohn Braun

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!!!!

CaptainObviousCaptainObvious

Try this:

 

<script type="text/javscript">

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

var quoteRequested = '{!Opportunity.Quote_Requested__c}'; 

if (quoteRequested==true) { 

    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();

} else {

    alert('Quote Requested was not checked');

}
 
</script>

 

This was selected as the best answer
John BraunJohn Braun
Thanks so much!!