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 

Help to write vf page using custom controller

We had a button with the below JS code
JS Code on Button

{!requireScript("/soap/ajax/30.0/connection.js")} 
{!requireScript("/soap/ajax/30.0/apex.js")} 

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') {
      alert(optresult[0].errors.message);
   } else {
      sforce.apex.execute('EmbedAutomation', 'automationForSales',    {optyId:'{!Opportunity.Id}', action:'Default'});    
alert('{!$Label.Def_Proceed}');
   }
} catch(err) {
   alert(err.message);
}
document.location = '/{!Opportunity.Id}';

Now need to convert this JS code in vf page. I tried below but not working as expected.

 

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

Need to use standard controller with extension customcontroller but didn't get success yet. It should be look like below screenshot.

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
Please anyone can help me out in this?
sachinarorasfsachinarorasf
Hi Chirag G,

Please go through the below link for detailed information:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm
https://mindmajix.com/custom-controller-in-salesforce
https://www.opencodez.com/salesforce/building-visualforce-custom-page-salesforce-app.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com