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
rkrk 

Add click on Javascript Button to Visualforce Page.

Hi.. I am new and learning salesforce. I wanted to create a custom Button in lightning.Upon clicking,they need to validate a field and only if true it has to open a visualforce page. I am able to do this using onclick javascript button,but i need it in lightning. I have added my javascript and visualforce page. Please guide me in this.

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

var type = "{!Opportunity.Type}"; 

if(type === 'Existing Customer - Downgrade'){ 
window.open('/apex/OppRenewal'); //to reload the window and show the updated values 

}else{ 
alert('Your not allowed.'); 

}


Visualforce Page Code

<apex:page standardController="Opportunity"  showHeader="true" standardStylesheets="true" sidebar="true">

   <apex:form id="mainform">
        <apex:pageMessages id="msgs"/>
 <apex:pageblock title="Quote Renewal">
                <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
    <apex:pageBlockSection title="Information" >
                <apex:outputfield value="{! Opportunity.name}"/>
                <apex:outputfield value="{! Opportunity.Type}"/>
                <apex:outputfield value="{! Opportunity.Amount}"/>
   </apex:pageBlockSection>
   <apex:pageBlockSection title="Contacts">
                 <apex:inputField value="{! Opportunity.Bill_to__c}" label="Bill TO"/>
   </apex:pageBlockSection>
</apex:pageblock>
            </apex:form>

</apex:page>


I tried to add the <script> but I am not able to achive it.
rkrk
Hi..Can abyone clarify this .. When I click a custom button it should validate a particular field is true and then it should open a VF page..  I am able to achive in onclick Javascript but I have to have this in lightning... 
mukesh guptamukesh gupta
Hi Rk 

For you solution you neeed to create  HeadLess quick action:-

headlessQuickAction.html
<template>
    
</template>
headlessQuickAction.js
import { LightningElement,api} from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import fetchOpportunity from '@salesforce/apex/OpportunityController.fetchOppDetail';

export default class HeadlessQuickAction extends LightningElement {
    @api recordId;
    isModalOpen = false;
    @api invoke() {
        fetchOpportunity({'recId' : this.recordId}).then(result => {
            if(result == true){
                this.isModalOpen = true;
                console.log('this.isModalOpen==>>>  '+this.isModalOpen); 
            }
        }).catch(error => {
            console.log('error.body.message  '+error.body.message);
        })
}
}

headlessQuickAction.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
   <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordAction</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordAction">
            <actionType>Action</actionType>
        </targetConfig>
     </targetConfigs>
</LightningComponentBundle>

OpportunityController.cls
public with sharing class OpportunityController {
    public OpportunityController() {
    }

    @AuraEnabled
        public static Boolean fetchOppDetail(Id recId){
            Boolean flag = false;
            try {
                Opportunity opp = [Select Id, Type from Opportunity where Id =: recId AND Type = 'Existing Customer - Downgrade'];
                if(opp != null){
                     flag = true;         
                }
                
            } catch (Exception e) {
                throw new AuraHandledException(e.getMessage());
            }
            return flag;
        }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
rkrk
Hi Mukesh... Thanks for the reply.. Just wanted to know can we create using VF page..  My requirement is i need a button in opp object that open a VF Page .. But to open the open it has to check a field in Opp object and only if its true we can access the VF page.. 
mukesh guptamukesh gupta
Hi Rk,

Yes, we create by VF page 

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
rkrk
Thanks Mukesh... I have field Type In the opportunity and can give a code or explain how I can validate a button and acces a  Simple VF page. Please note this is for my learning so I have got tge field  I have in developer sandbox.