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
Victor EcheverríaVictor Echeverría 

Custom button validation with visual force page pulling information from the object

I have a custom button on the opportunity object that generates a visualforce page. This Visualforce page is populated with the information in the opportunity record fields. I created a validation for the button to make sure that some fields are populated in order to execute the visualforce page. The problem is that with this validation the visualforce page is no longer pulling information from the opportunity record so I end up only with the skeleton of the page but no content. 

The code is as follows 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}


if({!ISBLANK(Opportunity.Fecha_inicio_contrato__c)}){
alert("No todos los campos necesarios para la ODC están llenos");
}
else{
window.open('/apex/Quote');
}

Is there any way to have a validation on a custom button but still make it possible for it to pull information from the record?
SonamSonam (Salesforce Developers) 
Hi Victor,

You should be able to get the content from the current opportunity records by passing the parameter from JS. Try below code 


{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}

if({!ISBLANK(Opportunity.Fecha_inicio_contrato__c)}){
alert("No todos los campos necesarios para la ODC están llenos");
}
else{
window.open('/apex/Quote?oppId='+{!Opportunity.Id});
}

Now in your Quote VF you need to query the opportunity record 
like 

If(ApexPages.currentPage().getParameters().get('oppId') <> null) {

 string oppId  = ApexPages.currentPage().getParameters().get('oppId');
  for(opportunity opp : [select id from opportunity where id =: oppId] ) {

    // You code logic with opportunity record
  
  }

}
Vidhya K 14Vidhya K 14
Hi Sonam,

Is it possible to do from the Custom button on the visualforce page? Basically I want to get rid of JS custom button as we will be migrating to Lightning soon. Please throw some lights.