Play Games

Search This Blog

Thursday, April 21, 2016

How to display Rollup Summary Field Value without decimal places in Visualforce Page.

Solution: Use this formatting <apex:outputText value="{0, number, ###,###,###,##0}"><apex:param value="{!a.sum__c}" /></apex:outputText> to display data without decimal values.
Example:
Assume Sum__c is rollup field(sum of its related opportunities Amount) in Account.
Apex Class:
public class RollupWithOutDecimal {
    public List<Account> accountList {get;set;}
    public RollupWithOutDecimal () {
        accountList =[select id,name,sum__C from Account where id=:apexpages.currentpage().getparameters().get('id')];
    }
}
Visualforce Page Code:
<apex:page Controller="RollupWithOutDecimal">
    <apex:form >
        <apex:pageBlock title="My Account" >
                <apex:pageBlockSection columns="4">
                    <apex:pageBlockTable value="{!accountList}" var="a">
                        <apex:column value="{!a.name}"/>
                        <apex:column headerValue="Sum"><apex:outputText value="{0, number, ###,###,###,##0}"><apex:param value="{!a.sum__c}" /></apex:outputText></apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output: When we run the above vf page with AccountId as id parameter value,then the output will be like this

Note :If you want to restrict number of decimal places after number go through the link given below.
Decimal Places

Wednesday, April 20, 2016

How to filter records using multiselect picklist field

Assume Subject__c is multiselect picklist field in lead object with available picklist values
Maths
Science
Social
English

Example 1: If you want to query all the lead records in which subject field has Maths,science as selected values(exact match),use "=" in query with all the selected values separated by ";" as below
Query:
List<Lead> leadList =[select id,name from lead where ksk__Subject__c =: ('social;Maths')];
system.debug('Lead Records'+leadList);
Example 2: If you want to query all the lead records in which subject field has any one of Maths or Science as selected value,use includes in the query with each value separated by "," as shown below.
Query:
List<Lead> leadList =[select id,name from lead where ksk__Subject__c includes  ('social','Maths')];
system.debug('Lead Records'+leadList);
Example 3: If you want to query all the lead records in which subject field doesn't have either Maths or science as selected value,then use "!=".
Query:
List<Lead> leadList =[select id,name from lead where ksk__Subject__c !=: ('social;Maths')];
system.debug('Lead Records'+leadList);

Error: Compile Error: Method does not exist or incorrect signature: Test.setMock(Type, someClassName)

I observed the above error in one of my friends developer org.
Problem: When he is saving some apex class,he is facing that issue.

That piece of code looks like this
@isTest
public class AnimalLocatorTest {
    @isTest static void testPostCallout() {
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        string s = AnimalLocator.getAnimalNameById(1);
    }
}
Solution: At first I was surprised.I didn't find any thing wrong in the code.Finally I observed that he has created one apex class with the name "test" which is creating this problem.So delete the apex class with the name "Test",then your issue is resolved.
Enjoy Coding...

Tuesday, April 19, 2016

How to remove html tags from rich text area field while displaying data in visualforce page.

Problem : When we are displaying data in vf page,if we display data of rich text area using <apex:outputtext> tag,the output data contains some html tags as shown in the image below.

Solution : Use escape ="false" attribute with in <apex:outputtext> .
Example: In this example I have created Rich Text field in lead object as Rich Text Area field as shown in image below.

Also Created Lead Record as shown in image below.

Apex Code:
public class RichTextAreaIssue {
    public Lead lobj {get;set;}
    public string textStr {get;set;}
    public RichTextAreaIssue () {
        lobj  = [select id,name,rich_text__c from lead where id=:'00Q2800000HTety' ];
        textStr =lobj.rich_text__c.stripHtmlTags();
    }
}
Visualforce page code Without using escape attribute :
<apex:page Controller="RichTextAreaIssue">
    <apex:form >
        <apex:pageBlock title="Display Rich Text Area Data in Visualforce Page" >
                <apex:pageBlockSection columns="4">
                     <apex:outputText >{!lobj.Rich_Text__c}</apex:outputText>
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output : 

Visualforce page code  using escape attribute :
<apex:page Controller="RichTextAreaIssue">
    <apex:form >
        <apex:pageBlock title="Display Rich Text Area Data in Visualforce Page" >
                <apex:pageBlockSection columns="4">
                     <apex:outputText escape="false" value="{!lobj.Rich_Text__c}"></apex:outputText>
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output : 

Important Note: use value attribute of <apex:outputtext> to display data.
if we use like this <apex:outputText escape="false" >{!lobj.Rich_Text__c}</apex:outputText>  it won't work.
if we use like this <apex:outputText escape="false" value="{!lobj.Rich_Text__c}"></apex:outputText>,then only it will work.

Enjoy Coding.............


Saturday, April 16, 2016

Pagination using Standard Set Controller

Example : To display Lead Records in Visualforce Page using Pagination.
Apex Class:
public class LeadController {
    Public Integer size {get;set;}
    Public Integer noOfRecords {get; set;}
       
    public LeadController() {
        size=10;
    }
   
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {              
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select id,Name,company,email,industry from Lead]));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }          
            return setCon;
        }
        set;
    }
    public List<Lead> getLeadList() {
         return (List<Lead>) setCon.getRecords();
    }
}
Visualforce Page :
<apex:page controller="LeadController">
    <apex:form >
        <apex:pageBlock id="pbId">
            <apex:pageBlockSection title="Lead" collapsible="false" columns="1">
                <apex:pageBlockTable value="{!leadList}" var="leadObj">
                    <apex:column headerValue="Lead Name">
                        <apex:outputField value="{!leadObj.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Email">
                        <apex:outputField value="{!leadObj.email}"/>
                    </apex:column>
                    <apex:column headerValue="Industry">
                        <apex:outputField value="{!leadObj.Industry}"/>
                    </apex:column>
                    <apex:column headerValue="Company">
                        <apex:outputField value="{!leadObj.company}"/>
                    </apex:column>
                </apex:pageBlockTable>
                <apex:panelGrid columns="8">
                <apex:commandButton status="fetchStatus" reRender="pbId" value="First" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
 
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Previous" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
 
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
 
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Last" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
 
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,
                     (setCon.pageNumber * size))} of {!noOfRecords}
                </apex:outputText>
                <apex:outputPanel >                    
                    <apex:actionStatus id="fetchStatus" >
                        <apex:facet name="start" >
                          <img src="/img/loading.gif" />                  
                        </apex:facet>
                    </apex:actionStatus>
                </apex:outputPanel>
            </apex:panelGrid>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output :

Friday, April 15, 2016

How to highlight the field value (or) display the field value in color in standard detail page

In order to highlight or color a field value, we need to use formula field along with infographics api.
Example : In lead Detail page,I want to highlight company field Value.
Create a formula field using infographics api as shown in the image below.
Here I created company__c as the custom field.

Formula field : IMAGE("//chart.googleapis.com/chart?chst=d_text_outline&chld=FFCC33|32|h|FF0000|b|"  & Company, "")
Output :

Note: Please go through the link to know more about Infographics API .
 "https://developers.google.com/chart/infographics/docs/dynamic_icons#outlined-font-text-blocks-text-only"
Thanks to Deepak Anand for providing this solution. 

Wednesday, April 13, 2016

SOSL Query Example (or) How to use string variable in SOSL query

Example : To write a SOSL query  to return both contacts and leads that have first or last name matching with 'Smith'.
Piece Of Code:
string searchString = 'Smith';
List<List< SObject>> recordList = [FIND :searchString IN ALL FIELDS RETURNING Contact(name,id where firstname=:searchString or lastName =:searchString ),Lead(firstname,lastName,name where firstname=:searchString or lastName =:searchString)];
system.debug('recordListrecordList'+recordList);

Output :

Tuesday, April 12, 2016

How to embed visual force page in page layout?

Step 1: Create a visual force Page as required using standard controller.If you don't use standard controller We cannot embed this vf page in page layout.
For example if we want to include vf page in Lead pagelayout or lead detail page use standardcontroller ="Lead" as shown in example below.

<apex:page standardController="Lead">
    This Visualforce Page is Embeded in Lead Detail Page
</apex:page>

Step 2: Go to Edit Layout of Lead record as shown in the image below.

Step 3: Scroll the scrollbar in the left side of panel and click on Visualforce pages.You will find all visualforce pages which can be included in layout.


Step 4: Drag the required visualforce page to any section where we need to embed vf page.

Step 5: If you want to adjust height/width of this vf page,click on wrench symbol at the top right of this vf page in layout as shown in the image below.

step 6: Save the layout. That's it.Now the vf page is embeded in layout.


How to open formula field Value in new tab

Solution : Create a formula field using hyperlink and use '_blank' to open that hyperlink in new tab as shown in the image below.

Output : 

When you click that "Google" hyperlink, the google page will be opened in new tab.

Wednesday, April 6, 2016

Lightning components require My Domain. Please contact your system administrator for more information.

Problem : When you run the lightning app,sometimes you get the above message.

Solution :
Step 1: Check whether "My domain" in Domain Management section is created or not.If not create my domain first.

Step 2: Go to Setup --> Domain Management --> My Domain --> click on "Deploy to users" button.You will the get the alert message like this

click "Ok".
step 3:Now run the lightning app.
Enjoy...

Tuesday, April 5, 2016

Order Of Execution in Salesforce

if we summarize the order of Execution will be as follows
order of Execution:
1) Loads the record.
2) System validations will fire.
3) Before triggers will fire.
4) system and user defined validation rules .
5) Duplicate rules.
6) Save the record but doesn't commit yet.
7) After triggers
8) Assignment rules
9) Auto response rules
10) workflow rules
11) Escalation rules
12) Entitlement rules
13) Commits all DML operations to the database.
In detail the order of Execution will be as follows .
The browser runs JavaScript validation if the record contains any dependent picklist fields. The validation limits each dependent picklist field to its available values. No other validation occurs on the client side.
On the server, Salesforce:
1) Loads the original record from the database or initializes the record for an upsert statement.
2) Loads the new record field values from the request and overwrites the old values.
If the request came from a standard UI edit page, Salesforce runs system validation to check the record for:
a)Compliance with layout-specific rules
b)Required values at the layout level and field-definition level
c)Valid field formats
d)Maximum field length
When the request comes from other sources, such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys. Prior to executing a trigger, Salesforce verifies that any custom foreign keys do not refer to the object itself.
Salesforce runs user-defined validation rules if multiline items were created, such as quote line items and opportunity line items.
3) Executes all before triggers.
4) Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce doesn't run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.
5) Executes duplicate rules. If the duplicate rule identifies the record as a duplicate and uses the block action, the record is not saved and no further steps, such as after triggers and workflow rules, are taken.
6) Saves the record to the database, but doesn't commit yet.
7) Executes all after triggers.
8) Executes assignment rules.
9) Executes auto-response rules.
10) Executes workflow rules.
11) If there are workflow field updates, updates the record again.
12) If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules, duplicate rules, and escalation rules are not run again.
13) Executes processes.
If there are workflow flow triggers, executes the flows.
The Process Builder has superseded flow trigger workflow actions, formerly available in a pilot program. Organizations that are using flow trigger workflow actions can continue to create and edit them, but flow trigger workflow actions aren’t available for new organizations.
14) Executes escalation rules.
15) Executes entitlement rules.
16) If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
17) If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the grandparent record. Grandparent record goes through save procedure.
18) Executes Criteria Based Sharing evaluation.
19) Commits all DML operations to the database.
20) Executes post-commit logic, such as sending email.