0

I am trying to mark a checkbox via a javascript button, i.e. update it as true. The checkbox is a field (Precisa_Cota_o_Frete__c) in the opportunity, but I want the button to work from the quote detail page. Here is the code I've got so far, but I am getting an error message from Salesforce:

Code:

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

 var opportunity = sforce.connection.query("Select Opportunityid From Quote where id = '{!Quote.Id}'");
 var myJob1 = new sforce.SObject("Opportunity"); 
 myJob1.Id = opportunity; 
 myJob1.Precisa_Cota_o_Frete__c = "true"; 
 var result = sforce.connection.update([myJob1]); 
 window.location.reload();

Error Message: A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:Client', faultstring:'Unexpected element {urn:partner.soap.sforce.com}done during simple type deserialization', line:'1015', column:'29', sourceURL:'https://na23.salesforce.com/soap/ajax/20.0/connection.js', }

It already does work without the var opportunity part on the opportunity page, there just defining the Id by putting myJob1.Id = "{!Opportunity.Id }";

Any ideas? Is that cross-referencing at all possible?

5
  • Why you try to use JS in this example? Commented Jun 18, 2015 at 12:12
  • I couldn't find a way to mark a checkbox via URL, if that is the question. Commented Jun 18, 2015 at 12:17
  • As I understand you mean mark/update in database Commented Jun 18, 2015 at 12:19
  • Yes, that is correct. - I want to update the checkbox field Precisa_Cota_o_Frete__c to the value true. The field is in the Opportunity. The button should work from the quote page. Commented Jun 18, 2015 at 12:21
  • If you don't want to write too much javascript, you could write an apex method with the webservice annotation, and then just use javascript to call the function.
    – martin
    Commented Jun 18, 2015 at 12:52

3 Answers 3

2

This should be possible. Try removing the quotation marks around true in your code. The field will be expecting a boolean value, and you're attempting to assign a string.

So the line in question becomes:

myJob1.Precisa_Cota_o_Frete__c = true; 
1
  • The way I programmed the button actually works already on the opportunity, it seems to have accepted it with quotation marks there, although your comment makes sense! My difficulty is how to cross-reference quote/opportunity correctly. Commented Jun 18, 2015 at 12:55
2

Java script button is also quite nice solution, but I think you should try page + controller. Create vf page with controller that will contain method to update parent opportunity - define this action in page. Call page from button and add button to quote layout.

I think it is safer to use standard solutions than JS.

1
  • I've always been torn between which is the best way of doing this. Using a visualforce page feels like the more proper "salesforce" way to do it, but on my org, I feel that it is noticeably slower than javascript.
    – martin
    Commented Jun 18, 2015 at 12:50
1

Change your 6 line to:

myJob1.Id = opportunity.records[0].Opportunityid;

The sforce.connection.query method doesn't return the specified field, but rather a List<SObject>.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .