+ 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

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
{!requireScript("/soap/ajax/30.0/apex.js")}
var oppObj = new sforce.SObject("Opportunity");
oppObj.Id = '{!Opportunity.Id}'; 
var result = sforce.connection.update([oppObj]);
if (result[0].success=='false') {
alert(result[0].errors.message);
}
else {
sforce.apex.execute('ApexClass', 'methodName', {optyId:'{!Opportunity.Id}', action:'Default'});
alert('{!$Label.Success}');
}
}
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">   
    <head>
        <style type='text/css'>
            .popupBackground{
            left: 20%;
            color:red;
            position: fixed;
            width: 450px;
            height: 100px;
            margin-left: -200px;
            top: 50px;
            }
            .popupBackground .close{
            position: absolute;
            background:#008CBA;
            color: white;
            bottom: 0;
            right: 10px;
            font-size: 10px;
            text-decoration: none;
            width:50px;
            }
        </style>
    </head>
    
    <apex:form >
        <body onload="init();">
            <apex:outputPanel styleClass="popupBackground" rendered="{!successMsg}">
                <center>
                    <apex:outputText value="{!$Label.Def_Proceed}" escape="false"/>
                    <apex:commandButton value="OK" styleClass="close" onclick="parent.window.close();"/>
                </center>
            </apex:outputPanel>
            <apex:outputPanel styleClass="popupBackground" rendered="{!errMsg}">
                <center>
                    <apex:outputText value="{!$Label.Error}" escape="false"/>
                    <apex:commandButton value="OK" styleClass="close" onclick="parent.window.close();"/>
                </center>
            </apex:outputPanel>
        </body>
    </apex:form>
    
    <script src="/soap/ajax/30.0/connection.js"></script >
    <script src="/soap/ajax/30.0/apex.js"></script>
    <script>
    sforce.connection.sessionId = "{!$Api.Session_ID}";
    function init()
    {
        var successMsg;
        var errMsg;
        var opp = new sforce.SObject("Opportunity");
        opp.id = "{!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;
        }
    }
    </script>
</apex:page>

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="{!acc.name}"/>
        <apex:commandButton value="Update" action="{!save}" style="width:90px" rerender="showmsg"/>
    </apex:pageblock>
 </apex:form>
</apex:page>
Apex Page:
public with sharing class ErrorMessageInVfController {
    public Account acc{get;set;}
    public ErrorMessageInVfController(ApexPages.StandardController controller) {
        acc = new Account();
    }
 
    public void save(){
      if(acc.name == '' || acc.name == 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'));
  }
}

https://prnt.sc/rn74g2
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 ?