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
Juan Antonio CantareroJuan Antonio Cantarero 

Get Opportunity field value from javascript in VisualForce

Dear, 

I want to perform some customs validations in the Opportunity but I can't use the "validation rules" functionality. The approach I would like to use is to create a custom VisualPage at the top of the Opportunity details page. When this VisualPage is loaded I run a "window.onload = function()". On this function I can perform any validation I want and in case of error, an alert is shown. 

But my problem is that I don't know how to get the Opportunity field values from javascript. Below is my code. I hope you can give some light here:
 
<apex:page standardcontroller="Opportunity">
  
    <apex:form id="formId"> 
         <!-- No Opportunity fields are placed here -->        
        <apex:image value="/img/msg_icons/confirm16.png" style="vertical-align:top;" id="Warning_icon"/>       
    </apex:form>
                          
    <script>    

    var Original_Amount__c = '{!Opportunity.Original_Amount__c}'; //THIS DO NOT WORKS
   
    window.onload = function() 
    {
        var Original_Amount__c  = document.getElementById({!Opportunity.Original_Amount__c});  //THIS DO NOT WORKS
                                                                      
        //Validations with Original_Amount__c value....                                    
    };
    </script>
    
</apex:page>

Thanks in advance!.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Juan,

Do you need to display some warning message, when some fields are not have the data while saving the opportunity?
Juan Antonio CantareroJuan Antonio Cantarero
Right. But I can't use "validation rules". The validations I want to check are complex, not so easy to check if a field value is provided or > 0, or in a range. 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Juan,

try with below code. you add multiple conditions in Controller.

VF Page
<apex:page standardController="Opportunity" extensions="Oppcontroller">
 <apex:outputPanel id="hiddenblock">
 <apex:pagemessages />
 </apex:outputPanel>
</apex:page>
Class:
public class Oppcontroller {
public String currentRecordId {get;set;}

public Oppcontroller (ApexPages.StandardController controller) {
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
//query fields you want
  Opportunity opp = [select id, stage from Lead where id =: currentRecordId ];
//check the conditions when you want show messages
    if(ld.stage ==Null){
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please select the stage'));
    }
       
     }

}

If this helps, Please mark it as best answer.

Thanks,
Ankaiah

 
Juan Antonio CantareroJuan Antonio Cantarero
Dear Ankaiah.

My license is SalesForce Professional Edition. Am I able to define code with classes like in your proposal?

Thanks.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Juan,

You cant able to do in professional edition.

You need to develop in developer edition and then deploy it to your professional org. please refer the below link.

https://help.salesforce.com/s/articleView?id=000323569&type=1

Thanks,
Ankaiah



 
Juan Antonio CantareroJuan Antonio Cantarero
Thanks Ankaiah, but it sounds complex and I don't like it. It's very "artificial".
Thanks anyway.
CouponPlay comCouponPlay com
Do you need to display some warning message, when some fields are not have the data while saving the opportunity?
Visit my site (https://couponplay.com/) to see more
Juan Antonio CantareroJuan Antonio Cantarero
Any idea? The solution given is very artificial.