0

I am not all that familiar with JavaScript. I have a piece of code below that was developed by a predecessor of mine for a Detail page button. The code works fine, but I would now like to use it as part of an embedded Visualforce page's Commandbutton. Can anyone help on how I would convert this to use in a script call for a VF page?

Javascript (Detail Button):

{!RequireScript("/soap/ajax/35.0/connection.js")}
var a = new sforce.SObject("Account");
a.id = "{!Account.Id}";
a.Mark_Account__c = !{!Account.Mark_Account__c};
sforce.connection.update([a]);
window.location.reload();

Javascript (VF Page):

    <apex:includeScript value="/soap/ajax/35.0/connection.js" />
    <script type="text/javascript">
        function Mark();
        {
            var a = new sforce.SObject("Account");
            a.id = "{!Account.Id}";
            a.Mark_Account__c = !{!Account.Mark_Account__c};
            sforce.connection.update([a]);
            window.location.reload();
        }
    </script>

    <apex:form >
        <apex:commandButton value="Mark" onClick="Mark();return false;"/>
    </apex:form>
2
  • what do you mean by scrip call for a VF page?
    – Himanshu
    Jan 19, 2016 at 19:32
  • @Himanshu - I revised my code above to show what works for my Detail page button and what is not working for my VF page commandbutton. Any idea why?
    – J. Neilan
    Jan 19, 2016 at 19:43

1 Answer 1

1

When you are on detail page of your record it will work in following way

<apex:includeScript value="/soap/ajax/35.0/connection.js" />
    <script type="text/javascript">
        function Mark();
        {
            var a = new sforce.SObject("Account");
            a.id = '{!Account.Id}';
            //var marked ='{!Account.Mark_Account__c}';
            a.Mark_Account__c=false;
            alert(a.Mark_Account__c);
            sforce.connection.update([a]);
            window.location.reload();
        }
    </script>

    <apex:form >
        <apex:commandButton value="Mark" onClick="Mark();return false;"/>
    </apex:form>
10
  • Thanks, however, when I click the button it does not update the checkbox field (Mark_Account__c).
    – J. Neilan
    Jan 19, 2016 at 19:58
  • alert(marked); to check the value
    – Himanshu
    Jan 19, 2016 at 20:01
  • Where would I see the value to check it from the alert?
    – J. Neilan
    Jan 19, 2016 at 20:05
  • you can add that like after var marked ='{!Account.Mark_Account__c}';
    – Himanshu
    Jan 19, 2016 at 20:06
  • 1
    Let us continue this discussion in chat.
    – Himanshu
    Jan 19, 2016 at 20:43

You must log in to answer this question.

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