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
Allen2Allen2 

Need to create new vf page with custom controller

I was having one custom button with JS code as below:
{!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.isStatus = true;

var optresult = sforce.connection.update([opp]);
if(optresult[0].success=='false') {
alert(optresult[0].errors.message);
} else {

sforce.apex.execute('populateOpportunity', 'automatePopulatingOpp', {optyId:'{!Opportunity.Id}', action:'Default'});

alert('{!$Label.SuccessMsg}');
}
} catch(err) {
alert(err.message);
}
document.location = '/{!Opportunity.Id}';

I'm new to vf page implementation. I need to replace this JS with Vf page. I've written below vf page and custom controller.

VF Page
<apex:page standardController="Opportunity"  showHeader="false" extensions="oppController" >   
    <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 rendered="{!showSuccessMsg}" id="opp">
        <apex:pageMessages id="errmsg"></apex:pageMessages> 
        <body onload="init();">
            <apex:outputPanel styleClass="popupBackground"  >
                <center>
                    <apex:outputText value="{!$Label.SuccessMsg}" escape="false"/>
                    <apex:commandButton value="OK" styleClass="close" onclick="parent.window.close();"/>
                </center>
            </apex:outputPanel>
        </body>
    </apex:form> 
</apex:page>

Custom Controller
public class oppController {
    public Id oppId;
    public Boolean showSuccessMsg{get;set;}
    public Boolean showErrorMsg{get;set;}
    
    public opptController(){
        showSuccessMsg = true;
        showErrorMsg = false;
        
        if(oppId != NULL){
            Opportunity opp = new Opportunity(Id = oppId);
            opp.isStatus = true;
            try{
                update opp;
                populateOpportunity.automatePopulatingOpp(oppId,'Default');
                showSuccessMsg = true;
                showErrorMsg = false;
                Error.LogSuccess(Label.SuccessMsg);
            }catch(Exception e){
                showSuccessMsg = false;
                showErrorMsg = true;
                Error.LogException(e);
            }
            showSuccessMsg = true;
            showErrorMsg = false;
            Error.LogSuccess(Label.SuccessMsg);
        }        
    }
}

Can anyone please help me out to correct this?
Sridhar Venkateswaralu 8Sridhar Venkateswaralu 8
Controller

public class oppController {
    private Opportunity opportunity;
    public String message { get; set; }
    
    public oppController(ApexPages.StandardController controller) {
        opportunity = (opportunity)controller.getRecord(); 
        message = '';
    }

    public void onLoad() {
        try {
            if(opportunity != null && opportunity.Id != null) {
                opportunity.Status = true;
                Database.SaveResult saveResult = Database.update(opportunity, false);
                if(saveResult.getErrors() != null && !saveResult.getErrors().isEmpty()) {
                    message = saveResult.getErrors()[0].getMessage();
                }
                else {
                    populateOpportunity.automatePopulatingOpp(oppId, 'Default');
                    message = 'Success';
                }
            }
        }       
        catch(Exception ex) {
            message = ex.getMessage();
        }
    }
}   



Page

<apex:page standardController="Opportunity"  showHeader="false" extensions="oppController" action="{!onLoad}">        
    <apex:outputPanel rendered="{!NOT(ISBlank(message))}">
        <script>
            alert('{!message}');
            document.location = '/{!Opportunity.Id}';
        </script>
    </apex:outputPanel>
</apex:page>
Allen2Allen2

Hi Sridhar,

Thanks for your reply.
It's working only for success. But there is no scenarios for failure. How will it handle failure here?

Thanks!

Allen