Ads

Friday 3 May 2013

How to Access Wrapper class variable in Visualforce page?

Apex Code:

public class a
{

     //accessing wrapper class variable
     public w var {get;set}
    //wrapper class
    public class w
    {
        public String str;
    }
}


Visualforce page:

<apex:outputText value="{!var.str}"/>

Retrieving parent record from child record in Salesforce?


Visualforce Page:

<apex:page controller="sample" >
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!intList}" var="i">
            <apex:column value="{!i.Name}"/>
            <apex:column value="{!i.Member__r.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Controller:

public class sample
{   
    public List<Interest__c> intList {get;set;}
    public sample()
    {
        String sql = 'SELECT Name, Member__r.Name FROM Interest__c';
        intList = Database.Query(sql);
    }   
}


Output:

@RemoteAction in Visual force page?


JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.

JavaScript remoting has three parts:
  • The remote method invocation you add to the Visualforce page, written in JavaScript.
  • The remote method definition in your Apex controller class. This method definition is written in Apex, but there are few differences from normal action methods.
  • The response handler callback function you add to or include in your Visualforce page, written in JavaScript.
 To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following form:
[namespace.]controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);
  • namespace is the namespace of the controller class. This is required if your organization has a namespace defined, or if the class comes from an installed package.
  • controller is the name of your Apex controller.
  • method is the name of the Apex method you’re calling.
  • parameters is the comma-separated list of parameters that your method takes.
  • callbackFunction is the name of the JavaScript function that will handle the response from the controller. You can also declare an anonymous function inline. callbackFunction receives the status of the method call and the result as parameters.
  • configuration configures the handling of the remote call and response. Use this to specify whether or not to escape the Apex method’s response. The default value is {escape: true}.
Visualforce Page:

<apex:page controller="sample">
    <script type="text/javascript">
    function getAccountJS()
    {
        var accountNameJS = document.getElementById('accName').value;       
        sample.getAccount( accountNameJS,
        function(result, event)
        {
            if (event.status)
            {
                // demonstrates how to get ID for HTML and Visualforce tags
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}").innerHTML = result.Id;
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}").innerHTML = result.Name;
            }
            else if (event.type === 'exception')
            {
                document.getElementById("errors-js").innerHTML = event.message;
            } else
            {
                document.getElementById("errors-js").innerHTML = event.message;
            }
        }, {escape:true});
    }
    </script>
    Account Name :<input id="accName" type="text" />
    <button onclick="getAccountJS()">Get Account</button>
    <div id="errors-js"> </div>
    <apex:pageBlock id="theBlock">
        <apex:pageBlockSection id="thePageBlockSection" columns="2">
            <apex:pageBlockSectionItem id="theFirstItem">
                <apex:outputText id="accId"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id="theSecondItem" >
                <apex:outputText id="accNam" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>


Apex Controller:

global class sample
{
    public String accountName { get; set; }
    public static Account account { get; set; }
    public sample() { }
   
    @RemoteAction
    global static Account getAccount(String accountName)
    {
        account = [select id, name, phone, type, numberofemployees from Account where name = :accountName ];
        return account;
    }
}


Ouptut:

How to Include One Visualforce Page within Another?

<apex:page sidebar="false" showHeader="false">
    <p>Test Before</p>
    <apex:include pageName="MainPage"/>
    <p>Test After</p>
</apex:page>

How to reset password for particular users in Salesforce using Apex?

List<User> u = new List<User>();
List<Id> ids = new List<Id>();
String test = 'test';
u = Database.Query('SELECT Id, Name FROM User WHERE Name =: test');
System.debug('User details are ' + u);
for(User usr : u)
{
    System.resetPassword(usr.Id,true);
}

Chatter Follow/Un-Follow button is missing from the Action column in the enhancedlist view?

We should add <chatter:feed/> to enable Chatter Follow/Un-Follow button before <apex:enhancedList/>

Sample Code:

<apex:page standardController="opportunity">
     <chatter:feed entityId="{!$User.Id}" />
    <apex:enhancedList type="opportunity" height="70"/>
</apex:page>

How to convert lead using Apex?


 
Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons');
insert myLead;

Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead.id);

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());