Play Games

Search This Blog

Monday, March 30, 2020

How to display account and its related contacts on selection in visualforce page

How to display account and its related contacts on selection in visualforce page
Apex Class:
public class DisplayContactOnAccountSelController {

    public list<AccountContactWrapper> lstAccountContactWrapper { get; set; }
    public list<AccountContactWrapper> lstSelectedAccountContactWrapper { get; set; }
    public list<account> selectedAccounts{get;set;}   

    public DisplayContactOnAccountSelController () {
        lstSelectedAccountContactWrapper = new list<AccountContactWrapper>();
        if(lstAccountContactWrapper == null) {
            lstAccountContactWrapper = new list<AccountContactWrapper>();
            for(account a:[select id,name,(select id,name from contacts) from account limit 10]) {
                lstAccountContactWrapper.add(new AccountContactWrapper(a));
            }
        }
    }
 
    public void ProcessSelected() {
        lstSelectedAccountContactWrapper =new list<AccountContactWrapper>();
        selectedAccounts =new list<Account>();
        for(AccountContactWrapper wrapobj:lstAccountContactWrapper){
            if(wrapobj.isSelected==true) {
                selectedAccounts.add(wrapobj.acc);
            } 
        }
       
        for(Account acc:[select id,name,(select id,name from contacts) from account where id in:selectedAccounts]) {
            lstSelectedAccountContactWrapper.add(new AccountContactWrapper(acc)); 
        }
             
    }
    public class AccountContactWrapper {
 
        public Account acc {get;set;}
        public boolean isSelected {get;set;}
   
        public AccountContactWrapper(account a) {
            acc = a;
            isselected=false;
        }
    }

}
Visualforce Page
<apex:page sidebar="false" controller="DisplayContactOnAccountSelController">
<apex:form >
    <apex:pageBlock id="AccountContactBlock">
        <apex:pageBlockSection columns="2">
        <apex:pageBlockTable value="{!lstAccountContactWrapper}" var="acc" id="AccountSection">
            <apex:column >
                <apex:inputCheckbox value="{!acc.isSelected}" id="InputId"/>
            </apex:column>
            <apex:column value="{!acc.acc.name}"/>
        </apex:pageBlockTable>
        <apex:pageBlockTable value="{!lstSelectedAccountContactWrapper}" var="acc" id="contactsSection" rendered="{!IF(lstSelectedAccountContactWrapper.size > 0,true,false)}">>
            <apex:column headerValue="Account and Its Related Contacts">
                <apex:pageBlockTable value="{!acc.acc.contacts}" var="con">
                    <apex:facet name="header">{!acc.acc.Name}</apex:facet>
                    <apex:column value="{!con.name}" headerValue="Contact Name"/>
                </apex:pageBlockTable>
            </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
        <apex:pageBlockButtons location="bottom">
          <apex:commandButton action="{!ProcessSelected}" value="Show Account and its Contacts" reRender="AccountContactBlock,contactsSection"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>

</apex:page>
Output:





Tuesday, March 17, 2020

How to display random number in visualforce page

Apex Class: OneDigitRandomNumberController
public class OneDigitRandomNumberController {
    public Integer iRandomNumber {get;set;}
    public void generateOneDigitRandomNumber() {
        iRandomNumber = Integer.valueof((Math.random() * 10));
        System.debug('One Digit Random Number  is'+iRandomNumber);
    }
}

Visualforce Page:OneDigitRandomNumber
<apex:page controller="OneDigitRandomNumberController" action="{!generateOneDigitRandomNumber}">
<apex:form >
<b> One Digit Random Number: </b> {!iRandomNumber}<br/>
<apex:commandButton action="{!generateOneDigitRandomNumber}" value="Refresh"/>
</apex:form>
</apex:page>
Output:

Monday, March 9, 2020

Display Visualforce page Names as dropdown or picklist in visualforce Page

Display Visualforce page Names as dropdown or picklist in visualforce Page
ApexClass: DisplayVFNamesDropDownController
Public class DisplayVFNamesDropDownController {
    public String selectedVF {get;set;}
    public DisplayVFNamesDropDownController() {
    }
    public List<SelectOption> getVisualforcePages() {
        List<SelectOption> lstVFPage = new List<SelectOption>();
        lstVFPage.add(new SelectOption('' , 'Select'));
        for(ApexPage objPage:[Select id,name from ApexPage]) {
            lstVFPage.add(new SelectOption(objPage.Name , objPage.Name));
        }
        return lstVFPage;

    }
    public void getSelectedValue() {
       
    }
}
Visualforce Page: DisplayVFNamesDropDown
<apex:page controller="DisplayVFNamesDropDownController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:selectList value="{!selectedVF}" size="1" multiselect="false" onchange="{!SelectedValue}" >
                <apex:selectOptions value="{!VisualforcePages}" />
                <apex:actionSupport event="onchange" reRender="selectedPageId" action="{!getSelectedValue}"/>
            </apex:selectList>
        </apex:pageBlockSection>
        <apex:pageBlockSection id="selectedPageId">
            Selected Page: {!selectedVF}
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>
Output:


Display data using Apex Maps in Visualforce Page - Salesforce Globe For You

Display data using Apex Maps in Visualforce Page  - Salesforce Globe For You 

Apex Code:  DisplayMapController
public class DisplayMapController {
    public Map<String,Account> mapAccount {get;set;}
    public DisplayMapController() {
        mapAccount = new Map<String,Account>();
        for(Account objAccount:[Select id,name,type,description from Account]) {
            mapAccount.put(objAccount.Id,objAccount);
        }
    }
}
Visualforce Page:DisplayMap

<apex:page controller="DisplayMapController">
<apex:form>
    <apex:pageBlock>
        <apex:pageBlockSection>
            <apex:pageBlockTable value="{!mapAccount}" var="acc">
                <apex:column value="{!mapAccount[acc].Name}"/>
                <apex:column value="{!mapAccount[acc].Type}"/>
                <apex:column value="{!mapAccount[acc].Description}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>
Output:


Thursday, March 5, 2020

Retrieving Data from an Apex Controller - Salesforce Globe For You

Retrieving Data from an Apex Controller

To retrieve the string array from an Apex controller, bind the component to the controller. This component retrieves the string array when a button is clicked.

<aura:component controller="namespace.AttributeTypes">
    <aura:attribute name="countries" type="String[]" default="India, USA,Singapore, Germany"/>
    <aura:iteration items="{!v.countries}" var="s">
        {!s}
    </aura:iteration>
    <lightning:button onclick="{!c.getString}" label="Update"/>
</aura:component>






Set the Apex controller to return a List<String> object.



public class AttributeTypes {
    private final String[] arrayItems;
    
 @AuraEnabled
    public static List<String> getStringArray() {
        String[] arrayItems = new String[]{ 'Canada', 'Mexico', 'Austarlia' };
        return arrayItems;
    }

}


This client-side controller retrieves the string array from the Apex controller and displays it using the {!v.countriesexpression.

({
    getString : function(component, event) {
    var action = component.get("c.getStringArray");
     action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var stringItems = response.getReturnValue();
                component.set("v.countries", stringItems);
            }
        });
        $A.enqueueAction(action);
    }
})

To retrieve data from an object that’s returned by an Apex controller, create an attribute with a type corresponding to a standard or custom object.

<aura:attribute name="accounts" type="Account[]"/>

Salesforce Record Creation Example in Visualforce Page - Salesforce Globe For You

Salesforce Record Creation Example in Visualforce Page  - Salesforce Globe For You 

Apex Class: RecordCreationController
public class RecordCreationController {
    public Account acc {get;set;}
    public RecordCreationController() {
        acc = new Account();
    }
    public PageReference saveRecord() {
        insert acc;
        system.debug('Account:'+acc);
        PageReference acctPage = new ApexPages.StandardController(acc).view();
        acctPage.setRedirect(true);
        return acctPage;
    }
}

Visualforce Page: Record Creation
<apex:page controller="RecordCreationController">
  <apex:sectionHeader title="New Account Creation" />
    <apex:form >
        <apex:pageBlock title="Create a New Account">
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!saveRecord}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Account Information">
                <apex:inputField  value="{!acc.name}"/>
                <apex:inputField  value="{!acc.site}"/>
            </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>
Ouput:


Tuesday, March 3, 2020

Build a Chevron Process View Using Salesforce Lightning Framework

Build a Chevron Process View Using Salesforce Lightning Framework




Output :



1) Survey.cmp

<aura:component >
    <aura:attribute name="selectedStep" type="string" default="stage1"/>
    <aura:attribute name="options" type="List" default="[
    {'label': 'Yes', 'value': 'option1'},
    {'label': 'No', 'value': 'option2'}
    ]"/>
    <aura:attribute name="value" type="String" default="option1"/>

    <div class="slds-m-around_xx-large">
         
        <lightning:progressIndicator currentStep="{!v.selectedStep}" type="path">
            <lightning:progressStep label="2019" value="stage1" onclick="{!c.selectstage1}" class="slds-progress-bar__value slds-progress-bar__value_success"/>
            <lightning:progressStep label="2020" value="stage2" onclick="{!c.selectstage2}"/>
            <lightning:progressStep label="2021" value="stage3" onclick="{!c.selectstage3}"/>
            <lightning:progressStep label="2022" value="stage4" onclick="{!c.selectstage4}"/>
        </lightning:progressIndicator>
         
        <div class="slds-p-around--medium">
            <div class="{!v.selectedStep == 'stage1' ? 'slds-show' : 'slds-hide'}">
             
    
    <lightning:radioGroup name="radioGroup"
                          label="1) Do You Live in USA ?"
                          options="{! v.options }"
                          value="{! v.value }"
                          type="radio"/>
            </div>
            <div class="{!v.selectedStep == 'stage2' ? 'slds-show' : 'slds-hide'}">
                <p><b>Step 2</b></p>
            </div>
            <div class="{!v.selectedStep == 'stage3' ? 'slds-show' : 'slds-hide'}">
                <p><b>Step 3</b></p>
            </div>
            <div class="{!v.selectedStep == 'stage4' ? 'slds-show' : 'slds-hide'}">
                <p><b>Step 4</b></p>
            </div>
        </div>
         
        <div>
            <button disabled="{!v.selectedStep != 'stage1' ? '' : 'disabled'}" class="slds-button slds-button--neutral" onclick="{!c.handlePrev}">Back</button>  
            <aura:if isTrue="{!v.selectedStep != 'stage4'}">
                <button class="slds-button slds-button--brand" onclick="{!c.handleNext}">Next</button>
            </aura:if>
            <aura:if isTrue="{!v.selectedStep == 'stage4'}">   
                <button class="slds-button slds-button--brand" onclick="{!c.handleFinish}">Finish</button>  
            </aura:if>
        </div>
    </div>
</aura:component>


---------------------

2) SurveyController.js

({
    handleNext : function(component,event,helper){
        var getselectedStep = component.get("v.selectedStep");
        if(getselectedStep == "stage1"){
            component.set("v.selectedStep", "stage2");
        }
        else if(getselectedStep == "stage2"){
            component.set("v.selectedStep", "stage3");
        }
         else if(getselectedStep == "stage3"){
            component.set("v.selectedStep", "stage4");
        }
    },
     
    handlePrev : function(component,event,helper){
        var getselectedStep = component.get("v.selectedStep");
        if(getselectedStep == "stage2"){
            component.set("v.selectedStep", "stage1");
        }
        else if(getselectedStep == "stage3"){
            component.set("v.selectedStep", "stage2");
        }
        else if(getselectedStep == "stage4"){
            component.set("v.selectedStep", "stage3");
        }
    },
     
    handleFinish : function(component,event,helper){
        alert('Finished...');
        component.set("v.selectedStep", "stage1");
    },
     
    selectstage1 : function(component,event,helper){
        component.set("v.selectedStep", "stage1");
    },
    selectstage2 : function(component,event,helper){
        component.set("v.selectedStep", "stage2");
    },
    selectstage3 : function(component,event,helper){
        component.set("v.selectedStep", "stage3");
    },
    selectstage4 : function(component,event,helper){
        component.set("v.selectedStep", "stage4");
    },
})


3) Launch the App


<aura:application extends="force:slds">
    <c:Survey />
</aura:application>