Showing posts with label Custom Controller. Show all posts
Showing posts with label Custom Controller. Show all posts

Sunday, 1 June 2014

Salesforce Multi-Select Picklist Using JQuery

I have implemented this good looking mulkti-select picklist on our Salesforce Visualforce pages.
It is so easy to handle and more user friendly as it looks.


User can select multiple option as it does in Salesforce Mutli-picklist fields, and values will be succesfully get saved in database.

User can choose more then one option by check options write in the front of the values.


You need to refer Jquery and Javascript from the static resource.

Here is the Visualforce Page code Visualforce Page

Here is the Apex class

Here is the link of github repo https://github.com/abhitripathi/MultiSelect-Jquery

Happy Coding..!!!

Monday, 14 April 2014

Login Page With CSS Styling in Salesforce


Once I need to create a login page for salesforce sites, but it should look nice, so I used CSS to make my Login page look good, am sharing this, so that you don't waste time in just styling a Page.

Its as simply and perfect as it looks


Isn't it looks good,

Here is the CSS Style code, save this file in CSS format and make it a Zip file and save it in static resource.
"LoginStyles.css"

Here is the Apex Class (this is not perfect, You need to make changes to save)
"LoginController"

Here is Visualforce page.
"LoginPage"

Hope it will save some ones time.

Happy Coding..!!! CHEERS :)

Tuesday, 8 April 2014

Dynamic Look-up of all Objects, To Make Your Life Easy #Salesforce

Some days ago, I got an requirement of mass updating, everything was just fine but got stuck at Dynamic look-up, where I need to make a single look field but if user want to that he can change it to the another Objects look-up and save the record.

That means if I want to create a single look field having dependent on another picklist of Object Names,
If user selects Account Object, then our look-up field will pop-up the records of Account object.

But if user change the Object in the picklist and now he selects Contact object, then our look-up field will pop-up Contact records.

Here is the Snapshot for more understanding.

When you chose Account

And when you chose Contact from the picklist

Then your pop-up window will show me Contact records

The code is so simple but I just like the functionality so I thought to post it.
Hope some of you will like it.

Here is Controller Class "DynamicLookupController"

Visualforce Page "DynamicLookup"

Happy Coding..!!!! CHEERS


Friday, 7 February 2014

Show IP Address on Visualforce Page Salesforce

This is one of the thing, that previously I never thought that it would be help full, but at some point it is.

When you want some where to show the current user's Ip address on page, OR weather you want to restrict a User of a particular IP then you can use this small code snippet with some booming functionality.

Apex Class ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

public class IPAddress {

    //Variable to hold the IP Address value for current logged in user
    public String ipAddress {get; set;}

    //Constructor
    public IPAddress() {

        //Map to hold current URL header parameters
        Map<String, String> mapHeaders = ApexPages.currentPage().getHeaders();  

    //Proceed further only and only if map of header parameter have not null value with it
        if(mapHeaders != null) {

            ipAddress = mapHeaders.get('True-Client-IP');
            if(ipAddress == null){
                ipAddress = mapHeaders.get('X-Salesforce-SIP');
            }
        }
    }
}

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

VisualForce page ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

<apex:page controller="IPAddress">

<h1> Current Logged In User IP Address ::::: </h1>   {!IpAddress}

</apex:page>

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

and here it is..!!!!


HAPPY CODING...!!!

Tuesday, 24 December 2013

Parsing XML file in Apex Class

Somedays ago I was in need to parse the XML response, Which I was getting in reponse. it require a bit labour to get a particular value from that XML file.

In salesforce most of the time when we are working with webservice, we need to parse JSON or XML. So here I am doing a small POC of parsing XML file in salesforce.
Here am trying to fetch the value of "R10TnxID" from the file.

So below is the code, for more detail you can visit on This Link
Here you need learn a bit about DOM

/** Description : Parse XML file. String Response is the XML file which is going to be parsed.
*
* Created By : Abhi Tripathi 
*
* Created Date : 07/09/2013
*
* Revisison Log : 07/09/2013 
*
* Version : V1.0
**/
public with sharing class domXmlParsing {
    
    //method to parse xml file 
    public static string walkThrough(){
        
        //XML string to parse
        String response = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">'
            + '<S:Body>'
            + '<wss:createPendingTicketResponse xmlns:wss="http://www.boomi.com/connector/wss">'
            + '<wss:createPaymentTransctionLogResponse/>'
            + '<PendingTicket xmlns:pay="http://www.boomi.com/v1b/Payment">'
            + '<pay:Response>'
            + '<pay:R10TillID>test001</pay:R10TillID>'
            + '<pay:R10SequenceNumber>test002</pay:R10SequenceNumber>'
            + '<pay:SFTnxId>test003</pay:SFTnxId>'
            + '<pay:R10TnxID>'+ 'TESTME' +'</pay:R10TnxID>'
            + '<pay:Org>test004</pay:Org>'
            + '</pay:Response>'
            + '</PendingTicket>'
            + '</wss:createPendingTicketResponse>'
            + '</S:Body>'
            + '</S:Envelope>';
        
        //dom
        Dom.Document doc = new Dom.Document();
        
        //string
        string result;
        
        //load document
        doc.load(response);
        
        Dom.XMLNode envelope = doc.getRootElement();
        system.debug('!!!!!!!!! value of envelope' + envelope);
        
        //body
        Dom.XMLNode body = envelope.getChildElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/'); 
        system.debug('@@@@@ value of body' + body);
        
        //child element of createPendingTicketResponse
        Dom.XMLNode createPendingTicketResponse = body.getChildElement('createPendingTicketResponse', 'http://www.boomi.com/connector/wss'); 
        system.debug('@@@@@ value of createPendingTicketResponse' + createPendingTicketResponse);
        
        //child element of createPaymentTransctionLogResponse
        Dom.XMLNode createPaymentTransctionLogResponse = createPendingTicketResponse.getChildElement('createPaymentTransctionLogResponse', 'http://www.boomi.com/connector/wss'); 
        system.debug('@@@####@@ value of createPaymentTransctionLogResponse' + createPaymentTransctionLogResponse);
        
        //child element of pending Ticket
        Dom.XMLNode PendingTicket = createPendingTicketResponse.getChildElement('PendingTicket', null); 
        system.debug('@@@####@@ value of PendingTicket' + PendingTicket);
        
        //child element of Response 
        Dom.XMLNode Respon = PendingTicket.getChildElement('Response', 'http://www.boomi.com/v1b/Payment'); 
        system.debug('@@@####@@ value of Response' + Respon); 
        
        //child element of R10TnxID
        Dom.XMLNode R10TnxID = Respon.getChildElement('R10TnxID', 'http://www.boomi.com/v1b/Payment'); 
        system.debug('@@@####@@ value of R10TnxID' + R10TnxID); 
        
        //here is the value in R10TnxID
        result = R10TnxID.getText();
        system.debug('@@@####@@ value of result' + result); 
        
        return result;
    }
}


After saving this above class, go to your Developer Console and write as below shown and then check your debug logs, you'll get the value of R10TnxID


Happy Coding....!!!!!!

Monday, 21 October 2013

360° With Test Class for Beginners #Salesforce

Here is have provided some of the basics to write perfect test class in salesforce



Here I'll explain with short snippets for the test classes
As the first one below

1:  @isTest(seeAllData=false)  
2:    private class Test_CountContactOfAccount {   
3:      //Method  
4:      static testMethod void CountContactOfAccountUnitTest() {  
5:      }  
6:    }  

(seeAllData=false)
You can set it true or false on your own, there are two condition for setting it true or false

1. If you are querying any object and fetching records from the database in the test class then set it to true as well there are few object we can't create in our test like "OpportunityLineItem" so to test with this object records, we need to query it with "seeAllData = true".

2. But if you are inserting test records of an object an then querying it in the test class, then you need to set it false otherwise test class won't be executed.


TESTING CUSTOM CONTROLLER AND STANDARD CONTROLLERr
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

How to cover custom controller in the test class?
If you need to cover constructor in the test class then simply define it in the test class, like CountContactOfAccount is the name of the class, just define it in the test class as in below, it will cover you whole constructor.

1:  //Calling contructor  
2:  CountContactOfAccount controller = new CountContactOfAccount();  

How to call StandardController (ApexPages.StandardController stdController)?
If you are using standard controller in the class then you need to define it in the test class too

1:  //Insert you object that you are using in your class  
2:  Account acc = new Account(Name = 'test');  
3:  insert acc;  
4:  //Define standard controller and pass inserted object   
5:  ApexPages.StandardController stdController = new ApexPages.StandardController stdController(acc);  
6:  //Now define your controller then pass standard controller you've just defined  
7:  CountContactOfAccount controller = new CountContactOfAccount(stdController );  

TESTING WITH STATIC AND VOID METHODS
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

How to call methods in the test class?
As you have seen above we have called controller , by using controller we can call static and void methods easily

So if the method is void like "public void something() {"

1:    //Calling contructor  
2:    CountContactOfAccount controller = new CountContactOfAccount();  
3:    //Calling void method  
4:    controller.something();  

But if the method is static like "public static list something() {"

then you can directly call the method no need to use deifned controller

1:    //Calling static method  
2:    CountContactOfAccount.something(listOfAccount);   

TESTING ApexPages.currentPage().getParameters().get('ID')
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

In this scenario you just need to put the value in the test of the variable that you fetching from the the URL

1:  //this method will put Id in the URL in the test class  
2:  currentPageReference().getParameters().put('id', '001c384348247811');  

ASSERTS (Most Important thing for test classes)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

There as three most commonly used asserts are there shown below, use asserts to check that your method are returning the correct values or not.

1:  System.assert(pBoolean)  
2:  System.assertEquals(pANY, pANY)  
3:  System.assertNotEquals(pANY, pANY)  

TESTING ERROR MESSAGES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

If you have error message in your test class and you want to cover it in test class then use this snippet

1:  //List of Message  
2:  List msgs = ApexPages.getMessages();  
3:  //Define a boolean variable  
4:  boolean msg = false;  
5:  //Loop through the messages of the page  
6:  for(Apexpages.Message msg:msgs){  
7:  //if there is error message then set the boolean to true  
8:  if (msg.getDetail().contains(‘Search requires more characters’)) msg = true;  
9:  }  
10:  //Assert  
11:  system.assert(msg);  
12:  //Single Assert on Error Messages  
13:  System.assert(ApexPages.getMessages()[0].getDetail().contains( 'User not found with given username' ));  

I think these are the most common scenarios for the Test classes that makes some mess.

Some key points:

1. Whole orgs average code coverage should be 75%, NOT each class.
2. Each trigger must more then 0% code coverage.

Hope I've helped you
CHEERS....!!!!