• Rohit Shisode
  • NEWBIE
  • 30 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
I have contact fields that i need to validate on client side and if everything is ok then insert that contact. But when i go into js for validation and call controller method from js using action fucntion all my inputField values are null. Contact does not get values that i have written, Please help!
VF:
<apex:pageBlock rendered="{!IsPkg}" id="contactPageBlock">
            <apex:pageBlockSection title="Guest Registration" columns="1" collapsible="false" id="contactSection">
                <apex:inputField value="{!contactObj.FirstName}" label="First Name" id="fname"/>
                <apex:inputField value="{!contactObj.LastName}" label="Last Name" id="lname"/>
                <apex:inputField value="{!contactObj.Email}" label="Email" id="cmail"/>
            </apex:pageBlockSection>
                <apex:commandButton onclick="validate();return false;" value="Save"/>
                <apex:actionfunction name="callfromJS" action="{!saveContact}" immediate="true"/>
        </apex:pageBlock>

Js
function validate(){
            try{
                var fNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.fname}");
                var lNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.lname}");
                var eNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.cmail}");
                console.log(fNameObj.value);
                console.log(lNameObj.value);
                console.log(eNameObj.value);
                if(trim(fNameObj.value) == "" || trim(lNameObj.value) == "" || trim(eNameObj.value) == ""){
                    console.log("Inside If");
                    window.alert("All fields are mandatory!");
                    return false;
                } else if(trim(eNameObj.value) != ""){
                    var x = eNameObj.value;
                    var atpos = x.indexOf("@");
                    var dotpos = x.lastIndexOf(".");
                    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
                        window.alert("Not a valid e-mail address");
                        return false;
                    }else {
	                    var r = confirm("Register a new guest?");
	                    if(r==true){
	                        callfromJS();
	                        return true;
	                    }else{
	                       return false;
	                    }
	                }
	            }
            } catch(e){
                console.log("Inside Catch");
            }
        }
    </script>

​Controller method
public void saveContact(){
        System.debug('Inside Save');
        System.debug('Contact Constructed is::'+contactObj);
        System.debug('selectedEventId:::'+selectedEventId);
        System.debug('selectedPackageId:::'+selectedPackageId);
        if(String.isNotBlank(selectedPackageId)){
            System.debug('Contact Constructed is::'+contactObj);
            if(contactObj.LastName != null){
                System.debug('Contact Constructed is::'+contactObj);
                contactObj.Package__c = selectedPackageId;
                try{
                    System.debug('Contact Constructed is::'+contactObj);
                    //insert contactObj;
                } catch(Exception e){
                    
                }
            }
        }
    }

 
I have 3 objects Event(Master), Package(Detail) and Group Code(Master). I want only one combination of Event and Group code. So how can i implement this?
If I use only standard list controller then pagination is working fine, but i need to sort list so I'm using extension and when i'm querying records in extension the pagination is not working, I'm new to VF, any help would be appriciated thanx.
code:
VF page
<apex:page standardController="Campaign" recordSetVar="Campaigns" extensions="CampaignCustomContoller">
    <apex:form >

        <apex:pageBlock title="User Driven Campaign" id="campaigns_list">
            
            Filter: 
            <apex:selectList value="{! filterId }" size="1">
                <apex:selectOptions value="{! listViewOptions }"/>
                <apex:actionSupport event="onchange" action="{!updatedCampaignsList}" reRender="campaigns_list"/>
            </apex:selectList>
            <apex:commandLink style="Float:right;" action="{!URLFOR($Action.Campaign.New)}" value="New" />
            <!-- Campaigns List -->  
            
            <apex:pageBlockTable value="{! Campaigns }" var="ct">
                <apex:column >
                    <apex:commandLink action="{! $Page.CampaignDetail}?id={! ct.id}" value="{!ct.Name}"/>
                </apex:column>
                
                <apex:column value="{! ct.Status}"/>
                <apex:column value="{! ct.StartDate}"/>
                <apex:column value="{! ct.LastModifiedDate}"/>
            </apex:pageBlockTable>
            <!-- Pagination -->
            <table style="width: 100%"><tr>
                <td align="center">
                    <!-- Previous page -->
                    <!-- active -->
                    <apex:commandLink action="{! Previous }" value="« Previous"
                         rendered="{! HasPrevious }"/>
                    <!-- inactive (no earlier pages) -->
                    <apex:outputText style="color: #ccc;" value="« Previous"
                         rendered="{! NOT(HasPrevious) }"/>
                    
                    &nbsp;&nbsp;  
                    
                    <!-- Next page -->
                    <!-- active -->
                    <apex:commandLink action="{! Next }" value="Next »"
                         rendered="{! HasNext }"/>
                    <!-- inactive (no more pages) -->
                    <apex:outputText style="color: #ccc;" value="Next »"
                         rendered="{! NOT(HasNext) }"/>
                </td>
            
            </tr></table>

        </apex:pageBlock>

    </apex:form>
</apex:page>

Extension::

public with sharing class CampaignCustomContoller {
    public List<System.SelectOption> listView{get;set;}
    ApexPages.StandardSetController standardCampaignController;
    Map<String,String> mapListView = new Map<String,String>();
    public string filterListId{get;set;}
    public List<Campaign> Campaigns {get;set;}
    public Boolean flag=true;
  
  /*Constructor*/
   public CampaignCustomContoller(ApexPages.StandardSetController controller){
       standardCampaignController = controller;
       Campaigns = controller.getRecords();
       listView = controller.getListViewOptions();
       filterListId = controller.getFilterId();
       flag=true;
       
       for(System.SelectOption s: listView){
           mapListView.put(s.getLabel(),s.getValue());
       }
       controller.setFilterId(mapListView.get('Recently Modified'));
       //updatedCampaignsList();
    }
    
   /*updates campaigns list on page*/
    public void updatedCampaignsList(){
        string selectedListView = 'LastModifiedDate';
        filterListId = standardCampaignController.getFilterId();
        //Campaigns = standardCampaignController.getRecords();
        string whereClauseString = '';
        if(flag||mapListView.get('Recently Modified') == filterListId){
            selectedListView = 'LastModifiedDate';
            flag=false;
        }else if(mapListView.get('All Campaigns') == filterListId){
            selectedListView = 'StartDate';
        }else if(mapListView.get('InActive Campaign') == filterListId){
            selectedListView = 'StartDate';
            whereClauseString = ' WHERE IsActive = false ';
        }else if(mapListView.get('All Active Campaign') == filterListId){
            selectedListView = 'StartDate';
            whereClauseString = ' WHERE IsActive = true ';
        }
        string query = 'SELECT Name,Status,Type,StartDate,LastModifiedDate FROM Campaign'+whereClauseString+' ORDER BY '+selectedListView+' DESC';
        //standardCampaignController = new ApexPages.StandardSetController(Database.getQueryLocator(query));
        Campaigns = Database.query(query);
    }
}


 
I have contact fields that i need to validate on client side and if everything is ok then insert that contact. But when i go into js for validation and call controller method from js using action fucntion all my inputField values are null. Contact does not get values that i have written, Please help!
VF:
<apex:pageBlock rendered="{!IsPkg}" id="contactPageBlock">
            <apex:pageBlockSection title="Guest Registration" columns="1" collapsible="false" id="contactSection">
                <apex:inputField value="{!contactObj.FirstName}" label="First Name" id="fname"/>
                <apex:inputField value="{!contactObj.LastName}" label="Last Name" id="lname"/>
                <apex:inputField value="{!contactObj.Email}" label="Email" id="cmail"/>
            </apex:pageBlockSection>
                <apex:commandButton onclick="validate();return false;" value="Save"/>
                <apex:actionfunction name="callfromJS" action="{!saveContact}" immediate="true"/>
        </apex:pageBlock>

Js
function validate(){
            try{
                var fNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.fname}");
                var lNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.lname}");
                var eNameObj = document.getElementById("{!$Component.form.contactPageBlock.contactSection.cmail}");
                console.log(fNameObj.value);
                console.log(lNameObj.value);
                console.log(eNameObj.value);
                if(trim(fNameObj.value) == "" || trim(lNameObj.value) == "" || trim(eNameObj.value) == ""){
                    console.log("Inside If");
                    window.alert("All fields are mandatory!");
                    return false;
                } else if(trim(eNameObj.value) != ""){
                    var x = eNameObj.value;
                    var atpos = x.indexOf("@");
                    var dotpos = x.lastIndexOf(".");
                    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
                        window.alert("Not a valid e-mail address");
                        return false;
                    }else {
	                    var r = confirm("Register a new guest?");
	                    if(r==true){
	                        callfromJS();
	                        return true;
	                    }else{
	                       return false;
	                    }
	                }
	            }
            } catch(e){
                console.log("Inside Catch");
            }
        }
    </script>

​Controller method
public void saveContact(){
        System.debug('Inside Save');
        System.debug('Contact Constructed is::'+contactObj);
        System.debug('selectedEventId:::'+selectedEventId);
        System.debug('selectedPackageId:::'+selectedPackageId);
        if(String.isNotBlank(selectedPackageId)){
            System.debug('Contact Constructed is::'+contactObj);
            if(contactObj.LastName != null){
                System.debug('Contact Constructed is::'+contactObj);
                contactObj.Package__c = selectedPackageId;
                try{
                    System.debug('Contact Constructed is::'+contactObj);
                    //insert contactObj;
                } catch(Exception e){
                    
                }
            }
        }
    }

 
I have 3 objects Event(Master), Package(Detail) and Group Code(Master). I want only one combination of Event and Group code. So how can i implement this?
If I use only standard list controller then pagination is working fine, but i need to sort list so I'm using extension and when i'm querying records in extension the pagination is not working, I'm new to VF, any help would be appriciated thanx.
code:
VF page
<apex:page standardController="Campaign" recordSetVar="Campaigns" extensions="CampaignCustomContoller">
    <apex:form >

        <apex:pageBlock title="User Driven Campaign" id="campaigns_list">
            
            Filter: 
            <apex:selectList value="{! filterId }" size="1">
                <apex:selectOptions value="{! listViewOptions }"/>
                <apex:actionSupport event="onchange" action="{!updatedCampaignsList}" reRender="campaigns_list"/>
            </apex:selectList>
            <apex:commandLink style="Float:right;" action="{!URLFOR($Action.Campaign.New)}" value="New" />
            <!-- Campaigns List -->  
            
            <apex:pageBlockTable value="{! Campaigns }" var="ct">
                <apex:column >
                    <apex:commandLink action="{! $Page.CampaignDetail}?id={! ct.id}" value="{!ct.Name}"/>
                </apex:column>
                
                <apex:column value="{! ct.Status}"/>
                <apex:column value="{! ct.StartDate}"/>
                <apex:column value="{! ct.LastModifiedDate}"/>
            </apex:pageBlockTable>
            <!-- Pagination -->
            <table style="width: 100%"><tr>
                <td align="center">
                    <!-- Previous page -->
                    <!-- active -->
                    <apex:commandLink action="{! Previous }" value="« Previous"
                         rendered="{! HasPrevious }"/>
                    <!-- inactive (no earlier pages) -->
                    <apex:outputText style="color: #ccc;" value="« Previous"
                         rendered="{! NOT(HasPrevious) }"/>
                    
                    &nbsp;&nbsp;  
                    
                    <!-- Next page -->
                    <!-- active -->
                    <apex:commandLink action="{! Next }" value="Next »"
                         rendered="{! HasNext }"/>
                    <!-- inactive (no more pages) -->
                    <apex:outputText style="color: #ccc;" value="Next »"
                         rendered="{! NOT(HasNext) }"/>
                </td>
            
            </tr></table>

        </apex:pageBlock>

    </apex:form>
</apex:page>

Extension::

public with sharing class CampaignCustomContoller {
    public List<System.SelectOption> listView{get;set;}
    ApexPages.StandardSetController standardCampaignController;
    Map<String,String> mapListView = new Map<String,String>();
    public string filterListId{get;set;}
    public List<Campaign> Campaigns {get;set;}
    public Boolean flag=true;
  
  /*Constructor*/
   public CampaignCustomContoller(ApexPages.StandardSetController controller){
       standardCampaignController = controller;
       Campaigns = controller.getRecords();
       listView = controller.getListViewOptions();
       filterListId = controller.getFilterId();
       flag=true;
       
       for(System.SelectOption s: listView){
           mapListView.put(s.getLabel(),s.getValue());
       }
       controller.setFilterId(mapListView.get('Recently Modified'));
       //updatedCampaignsList();
    }
    
   /*updates campaigns list on page*/
    public void updatedCampaignsList(){
        string selectedListView = 'LastModifiedDate';
        filterListId = standardCampaignController.getFilterId();
        //Campaigns = standardCampaignController.getRecords();
        string whereClauseString = '';
        if(flag||mapListView.get('Recently Modified') == filterListId){
            selectedListView = 'LastModifiedDate';
            flag=false;
        }else if(mapListView.get('All Campaigns') == filterListId){
            selectedListView = 'StartDate';
        }else if(mapListView.get('InActive Campaign') == filterListId){
            selectedListView = 'StartDate';
            whereClauseString = ' WHERE IsActive = false ';
        }else if(mapListView.get('All Active Campaign') == filterListId){
            selectedListView = 'StartDate';
            whereClauseString = ' WHERE IsActive = true ';
        }
        string query = 'SELECT Name,Status,Type,StartDate,LastModifiedDate FROM Campaign'+whereClauseString+' ORDER BY '+selectedListView+' DESC';
        //standardCampaignController = new ApexPages.StandardSetController(Database.getQueryLocator(query));
        Campaigns = Database.query(query);
    }
}