+ Start a Discussion
rushirushi 

VF page for list button not working

I am trying to convert the java script button to lightning and creating a VF page and controller.
The list button on the Model_C object will be on the Contact page layout related list. When user clicks the button it checks the parent object Terms_of_use__C check box, if it's true then we will open the URL in new window otherwise give some alert message.

In classic, java script button is working fine but this VF page is not displaying anything neither the new URL nor alert message.
VF page:

<apex:page standardController="Model__c" recordSetVar="Models" lightningStylesheets="true" extensions="ModelController"> <apex:includeScript value="/soap/ajax/43.0/connection.js"/> <apex:includeScript value="/soap/ajax/43.0/apex.js"/>
<script type="text/javascript">
sforce.connection.sessionId = '{!$Api.Session_ID}';
</script>
<script type = "text/javascript">
function test123(terms){ var MyUrl = '{!JSENCODE($Label.Survey_URL)}';
console.log(terms); if(terms == 1){ window.open(MyUrl,"_blank"); } else { alert("The Contact has not agreed to the Terms .") } } </script> <apex:form >
<apex:actionFunction name="test12" action="{!termsE}" oncomplete="test123('{!terms}');"/> </apex:form>
</apex:page>
Apex class:
public class ModelController{

    public ModelController(ApexPages.StandardSetController controller) {

    }

    public List<Model__c> Models {get; set;}
    //pubmic Contact contact;
    public Boolean terms {get; set;}
    
     public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator(
        [SELECT Id, Name, contact__r.Terms_of_Use__c FROM Model__c where contact__r.Terms_of_Use__c = true]));
        }
            return con;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Model__c> getModels() {
        return (List<Model__c>) con.getRecords();
    }
    
    public Boolean termsE(){
      //Boolean terms= false;
      terms = con.getRecords().size() > 0 ? true : false;
      return terms;
    }
}
Javascript button:
{!REQUIRESCRIPT("/soap/ajax/40.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/40.0/apex.js")}


var MyUrl = '{!JSENCODE($Label.Survey_URL)}'
if({!Contact.Terms_of_Use_c} == 1){
window.open(MyUrl,"_blank");
} else {
alert("The Contact has not agreed to the Terms.")
}