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
SFDC Apex DevSFDC Apex Dev 

Need help to convert the existing button code to vf and execute it through button at one click

var oppObj = new sforce.SObject("Opportunity");
oppObj.Id = '{!Opportunity.Id}'; 
var result = sforce.connection.update([oppObj]);
if (result[0].success=='false') {
else {
sforce.apex.execute('ApexClass', 'methodName', {optyId:'{!Opportunity.Id}', action:'Default'});
document.location = '/{!Opportunity.Id}';

I've simply addeed the entire code to vf page, standardcontroller as Opportunity.
I need to use CSS as well so that the error msg can be displayed in left top instead of the alert box which displayed at the center on top.

Please help ASAP.
Ankit Solanki 6Ankit Solanki 6
Can you please share your vf code , then i can solve your error.
SFDC Apex DevSFDC Apex Dev
Hi Ankit,

Here the code I've written. Can you help me out to correct it using custom controller.
<apex:page standardController="Opportunity" showHeader="false">   
        <style type='text/css'>
            left: 20%;
            position: fixed;
            width: 450px;
            height: 100px;
            margin-left: -200px;
            top: 50px;
            .popupBackground .close{
            position: absolute;
            color: white;
            bottom: 0;
            right: 10px;
            font-size: 10px;
            text-decoration: none;
    <apex:form >
        <body onload="init();">
            <apex:outputPanel styleClass="popupBackground" rendered="{!successMsg}">
                    <apex:outputText value="{!$Label.Def_Proceed}" escape="false"/>
                    <apex:commandButton value="OK" styleClass="close" onclick="parent.window.close();"/>
            <apex:outputPanel styleClass="popupBackground" rendered="{!errMsg}">
                    <apex:outputText value="{!$Label.Error}" escape="false"/>
                    <apex:commandButton value="OK" styleClass="close" onclick="parent.window.close();"/>
    <script src="/soap/ajax/30.0/connection.js"></script >
    <script src="/soap/ajax/30.0/apex.js"></script>
    sforce.connection.sessionId = "{!$Api.Session_ID}";
    function init()
        var successMsg;
        var errMsg;
        var opp = new sforce.SObject("Opportunity"); = "{!Opportunity.Id}";
        try {
            opp.Def_Sc = true;
            var optresult = sforce.connection.update([opp]);
            if(optresult[0].success=='false') {
                errMsg = true;
            } else {
                sforce.apex.execute('EmbedAutomation', 'automationForSales', {optyId:'{!Opportunity.Id}', action:'Default'});
                successMsg = true;
        } catch(err) {
            errMsg = true;

User-added image

I want to show the error message or success message once we are clicking the button on opportunity and shoud show the message as per the screenshot.

User-added image
Ankit Solanki 6Ankit Solanki 6
In vf page for error or success message use    

<apex:pageMessages id="showmsg"></apex:pageMessages> this attribute.

Ex : Visual force page : 
<apex:page standardController="Account" extensions="ErrorMessageInVfController">
 <apex:form >
   <apex:pageblock >
      <apex:pageMessages id="showmsg"></apex:pageMessages>
           Account Name: <apex:inputText value="{!}"/>
        <apex:commandButton value="Update" action="{!save}" style="width:90px" rerender="showmsg"/>
Apex Page:
public with sharing class ErrorMessageInVfController {
    public Account acc{get;set;}
    public ErrorMessageInVfController(ApexPages.StandardController controller) {
        acc = new Account();
    public void save(){
      if( == '' || == null){
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
//  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account name'));
//  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account name'));
//  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account name'));
//  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account name'));
Logo looks like this : error, confirm etc.
Getting id you can pass your css for <apex:pageMessages>
Ankit Solanki 6Ankit Solanki 6
If it's solved wil you marked as solved ?