Tuesday, 10 December 2013

How to get a map from database.query()

In salesforce doc you may found that it is possible to get a map from a query instead of a list like this:

Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);

But you can also achieve this if you are using database.query() to run the select.This can be achieved like follows :
 String query = 'SELECT Id, LastName FROM Contact';
Map<id,Contact> mapCon = new Map<id,Contact>((List<Contact>)Database.query(query));

Monday, 14 October 2013

How to validate empty field on visualforce page with standard errors

I have come across this situation more than often, and using the standard required=true on inputfield or inputText sometimes prevent any submission of the page, which is good for some reason, but will prevent also any refresh.Sometime also, using apex:inputText with required=true, doesn't work. Here is the solution with jquery :

1. First add a styleClass to your all your inputText you want to validate for blank.For this example, i will create only one picklist.

 <table class="detailList" cellspacing="0" cellpadding="0" border="0">
                                        <td class="labelCol">{!$ObjectType.HeaderVoucher__c.fields.numm__AccountingDate__c.label}</td>
                                        <td class="dataCol">
                                            <div class="requiredInput">
                                                <div class="requiredBlock"></div>
                                                <apex:inputField styleClass="jq_req" value="{!dummyPiece.AccountingDate__c}" required="false"/>

                                <td class="pbTitle"></td>
                                <td class="pbButtonb">
                                    <apex:commandButton value="{!$Label.Button_Settlement}" action="{!Regler}" onclick="return validateReglementFields();"/>                                               

note: the styleClass i always use is jq_req.

2. Now look at the jquery functions you need :

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
 if(typeof jQuery != 'undefined'){
           $j =jQuery.noConflict();

function jq_addError(me,msg){
        $j(me).parent().append("<div class='errorMsg'>" + msg + "</div>");
        $j(me).parent().append("<div class='errorMsg'>" + msg + "</div>");

function jq_removeError(me){

function validateReglementFields(){
    var b = true;
        if($j(this).val().length == 0){
:</b>Ce champ est obligatoire");
            b = false;
    return b;

For sure you can store the error message in a label and then reference the label in your javascript code. 

 Here is the resulted error :


Friday, 26 April 2013

autocomplete with actionfunction

I saw several codes for autocomplete on salesforce, but none of them send the selected value to the server, thus i rewrite the code for it. In the example below, i am choosing an account and sending the selected account id to the server, thus you can use the below code to replace lookup field for auto complete functionality. Here it is , enjoy :

Page code:

<apex:page controller="DemoAutoComplete" showHeader="false">
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
        if(typeof jQuery != 'undefined'){
           $j =jQuery.noConflict();
         $j(function() {           
                source: function(request, response){
                    getMyData(response ,request.term);
                select: function(event, ui){
                    return false;

        function getMyData(response,param){
                function(result, event){
                    if (event.status) {                       
                        var objList = [];
                        for(var i = 0; i < result.length; i++){
                            var obj = new Object();
                            obj.label = result[i].Name;
                            obj.value = result[i].Name;
                            obj.id = result[i].Id;
                    }else {
                {escape: true}
    <apex:form id="mainForm">   
        <div class="filterOverview">
            <div class="bFilterView">
                <table border="0">                   
                        <td>Choose an account</td>                   
                            <div class="ui-widget">
                                <apex:inputText id="x_val" styleClass="auto001" value="{!accName}" />
                                <apex:inputText id="auto002" value="{!accId}" styleClass="auto002" style="visibility:hidden"/>                               
                        <td>selected Id</td>                   
                            <apex:actionfunction name="refresh" action="{!Refresh}" rerender="pn1"/>
                            <apex:outputPanel id="pn1">
                                <apex:outputText id="x_id" value="{!accId}" />

Controller code :

public class DemoAutoComplete {
    public String accName{get;set;}
    public Id accId{get;set;}
    public DemoAutoComplete(){
    public PageReference refresh(){
        return null;
    public static list<Account> getAccounts(string s){
        list<Account>accList = new list<Account>();
        if(s != ''){
            String query = 'select name from account where Name like \'' + s + '%\'';
            accList = database.query(query);
        return accList;

Tuesday, 16 April 2013

Passing variable in custom label

At a certain time, I thought it was impossible to do, then one day I came across, the way to do it, here and there. Here is the solution :
 suppose you want to add the connected user name dynamically

 1. Create the label  :

2. Now  add the label on your page like this :

  <!-- Begin Default Content REMOVE THIS -->
      <apex:outputText value="{!$label.Hello}">
          <apex:param value="{!$user.userName}" />

  <!-- End Default Content REMOVE THIS -->

There is some problem with this therefore, especially when the text in the custom label contains the ' (single quote) character . There is eventually, a way to bypass it, look at the example below :

Conformément à l'article 121 – II de la loi no. 2012-387 du 22 mars 2012, l'indemnité forfaitaire pour frais de recouvrement en cas de<br/>
40 euros par le décret no.2012-115 du 02/10/2012. Tout matériel ou logiciel reste la propriété de {0} jusqu'au paiement<br/>
Aucun escompte ne sera accordée pour paiement anticipé<br/>
'{1} au capital de {2}, No. Siren : {3}, TVA CEE : {4}

To bypass the ' (single quote)char , we use another '(single quote) in the right place, just that.

Sunday, 14 April 2013

URLFOR examples and explanations

Here are some uses of the apex URLFOR function :
    //Use $Action global varialble to access the New action reference -->
    <apex:outputLink value="{!URLFOR($Action.Account.New)}">New</apex:outputLink>
    //View action requires the id parameter, a standard controller can be used to obtain the id -->
    <apex:outputLink value="{!URLFOR($Action.Account.view, account.id)}">View</apex:outputLink>
    //Edit action requires the id parameter, id is taken from standard controller in this example -->
    <apex:outputLink value="{!URLFOR($Action.Account.Edit, account.id)}">Edit</apex:outputLink> 
    //Delete action requires the id parameter, also a confirm message is added to prevent deleting the record when clicked by mistake -->
    <apex:outputLink value="{!URLFOR($Action.Account.delete, account.id)}" onclick="return window.confirm('Are you sure?');">Delete</apex:outputLink>
    //From all custom buttons, links, s-controls and visualforce pages you can use the following to get the link of the object's homepage -->
    <apex:outputLink value="{!URLFOR($Action.Account.Tab, $ObjectType.Account)}">Home</apex:outputLink>
    //If your file is in another ZIP file, then pass the path of the file as id to URLFOR -->
    <apex:image url="{!URLFOR($Resource.CorpZip, 'images/logo.gif')}" width="50" height="50" />
    //reference to a custom VF page
    <apex:commandButton action="{!URLFOR($page.UpdateCouponReponse)}" value="Voir les coupons-réponse"/>
    //overriding standard page to add defaulted values
    <apex:page standardController="Filleul__c" action="{!URLFOR($Action.Filleul__c.New,null, [Name=$label.GenererNomAuto,retURL=$ObjectType.Filleul__c.KeyPrefix + '/o'],true)}">

Formatting in apex:outputText

Here are some example of how to format apex:outputText values using  apex:param :
  • Date formatting :
<apex:outputText value="{0,date,dd/MM/yyyy}">
        <apex:param value="{!E.Date_du_collecte__c}"/>

the 0, tell to take the first parameter, the date keyword specify the type of data, and the third parameter specify how to format the data.

  • Number formatting :
<apex:outputText value="{0,number,integer}">
        <apex:param value="{!opp.Amount}"/>

the 0, tell to take the first parameter, the number keyword specify the type of data, and the third parameter specify how to format the data.

Sunday, 7 April 2013

How to display a salesforce page without headers

For VF, its simple, you just specify the showheader="false".But suppose you want to display a report on your VF, and you don't want the header and sidebar to appear.

 Just use the parameter isdtp=mn or isdtp=vw in the url parameter

Example :
<apex:iframe src="http://www.salesforce.com?isdtp=vw" 
scrolling="true" id="theIframe"/>