Monday, February 13, 2012

Where do I add a trigger for “Notes and Attachments” in Salesforce.com?


--> Go to any object's trigger list and click create new. In the URL locate entity query string parameter (e.g. entity=Case) and change it to Attachment (entity=Attachment) and press Enter. New loaded screen will accept Attachment trigger.

Monday, February 6, 2012

Apex Code Best Practices:

Best Practice #1: Bulkify your Code
Best Practice #2: Avoid SOQL Queries inside FOR Loops
Best Practice #3: Bulkify your Helper Methods
Best Practice #4: Using Collections, Streamlining Queries, and Efficient For Loops
Best Practice #5: Streamlining Multiple Triggers on the Same Object
Best Practice #6: Querying Large Data Sets
Best Practice #7: Use of the Limits Apex Methods to Avoid Hitting Governor Limits
Best Practice #8: Use @future appropriately
Best Practice #9: Writing Test Methods to Verify Large Datasets 
Best Practices #10: Avoid Hardcoding IDs

Integrating Salesforce Web service with Asp.Net :

Salesforce web service:

global class LeadInfo
{
    WebService static List<Lead> GetLeadInfo() 
    {
        List<Lead> Leadinfo = new List<Lead>();
        Leadinfo=[SELECT FirstName,LastName,Email from Lead limit 5];
        return Leadinfo; 
    }
}

Design  a ASP.Net page as just like below:






 






C# Code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Enterprize;
using LeadInfo_Service;
public partial class Default2 : System.Web.UI.Page
{
    private string userid = "anyone@someone.com";// Salesforce user id
    private string password = "password"; // password
    private string sessionId;
    private string serverUrl;
    private string leadEmail;
    private DateTime nextLoginTime;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Panel1.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Application["_sessionId"] == null)
            Application["_sessionId"] = "";
        if (Application["_serverUrl"] == null)
            Application["_serverUrl"] = "";
        if (Application["_nextLoginTime"] == null)
            Application["_nextLoginTime"] = " #1/1/2000#";
        if (!isConnected())
            getSessionInfo();
        //       ' Call getLeadInfo Web Service
        Panel1.Visible = true;
        LeadInfoService getLeadInfo = new LeadInfoService();
        getLeadInfo.SessionHeaderValue = new LeadInfo_Service.SessionHeader();
        getLeadInfo.SessionHeaderValue.sessionId = sessionId;
        LeadInfo_Service.Lead[] getres = new LeadInfo_Service.Lead[1];
        getres = getLeadInfo.GetLeadInfo();
        int i;
        for (i = 0; i < getres.Length; i++)
        {
            FirstName.Items.Add((i+1)+")"+getres[i].FirstName);
            LastName.Items.Add((i + 1) + ")" + getres[i].LastName);
            Email.Items.Add((i + 1) + ")" + getres[i].Email);
        }
    }
    public Boolean isConnected()
    {
        if (sessionId != "" & sessionId != null)
        {
            if (DateTime.Now > nextLoginTime)
                return false;
            else
                return true;
        }
        else
            return false;
    }
    public void getSessionInfo()
    {
        Enterprize.LoginResult lr;
        Enterprize.SforceService ss = new Enterprize.SforceService();
        lr = ss.login(userid, password);
        sessionId = lr.sessionId;
        Application["sessionId"] = lr.sessionId;
        serverUrl = lr.serverUrl;
        Application["serverUrl"] = lr.serverUrl;
        Application["nextLoginTime"] = DateTime.Now;


    }
}


URL Rewriting - Salesforce


We need two Visualforce pages for URL Rewriting.
1)      Home
2)      Contact_Page
               On the Site Edit page, choose a Home Visualforce page for Active Site Home Page.
                Incoming URL requests can only be mapped to Visualforce pages associated with your site. We can't map to standard pages, images, or other entities. To rewrite URLs for links on your site's pages, we have used the !URLFOR function with the $Page merge variable.
Home VF Page:
                This is the Visualforce page created for the test site. Create an output link in the “home” page with label “Click me”. When we click on the link it will be redirected to the second Visualforce page that is “Contact_Page”.
 Contact_Page:
                While redirecting we can observe that the URL is rewrited. Usually In the URL we should see the id of the contact, But the Apex class developed for mapping the standard URL with user friendly URLs will map the id to its corresponding contact name.
Apex Class: URL Rewriter Class

global class URLRewriterClass implements Site.UrlRewriter
{
 //Variable to represent the friendly URLs for pages
 String DIRECTORY = '/Contact_Page/';
 //Variable to represent my custom Visualforce pages that display page information
 String VISUALFORCE_PAGE ='/Contact_Page?id=';
 // The first global method for mapping external URL to an internal one
 global PageReference mapRequestUrl(PageReference myFriendlyUrl)
 {
    String url = myFriendlyUrl.getUrl();
    System.debug('******************************'+url);
    if(url.startsWith(DIRECTORY))
    {
       String name = url.substring(DIRECTORY.length(),url.length());
       //Select the ID of the page that matches the name from the URL
       Contact site_page = [select id from Contact where name=:name LIMIT 1];
       System.debug('******************************'+site_page.id);
       //Construct a new page reference in the form of my Visualforce page
       return new PageReference(VISUALFORCE_PAGE+ site_page.id);
    }
    return null;
  }
  // The second global method for mapping internal Ids to URLs
  global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls)
  {
    //A list of pages to return after all the links have been evaluated
    List<PageReference> myFriendlyUrls = new List<PageReference>();
    for(PageReference mySalesforceUrl : mySalesforceUrls)
    {
      //Get the URL of the page
      String url = mySalesforceUrl.getUrl();
      //If this looks like a page that needs to be mapped, transform it
      if(url.startsWith(VISUALFORCE_PAGE))
      {
        //Extract the ID from the query parameter
        String id= url.substring(VISUALFORCE_PAGE.length(), url.length());
        Contact site_page2 = [select name from Contact where id =:id LIMIT 1];
        //Construct the new URL
        myFriendlyUrls.add(new PageReference(DIRECTORY + site_page2.name));
     } 
     else
     {
       myFriendlyUrls.add(mySalesforceUrl);
     }
  }
  //Return the full list of pages
  return myFriendlyUrls;
  }
}
                The “generateUrlFor” method in the apex class that queries name from the id which is there in the standard URL query string and appends the name to the user friendly URL.
                The “mapRequestUrl” method in the apex class that queries the id from the name which is there in the user friendly URL and maps it to the standard URL.
After creating the URL rewriting Apex class, Add the same to the site.
Below are the steps to add the Apex class to the site:
1.       Click Your Name | Setup | Develop | Sites.
2.       Click New or click Edit for an existing site.
3.       On the Site Edit page, choose an Apex class for URL Rewriter Class.
4.       Click Save.

Home Page VF Code: 


<apex:page standardController="Account">
<apex:form >
<apex:pageBlock >
<apex:outputlink value="{!URLFOR($Page.Contact_Page,null,[id='00390000009IBJw'])}">Click Me</apex:outputlink>
</apex:pageBlock>
<apex:detail />
</apex:form>
</apex:page>



Contact_Page VF Code:


<apex:page standardController="contact"> 
    <apex:detail relatedList="false"/>
</apex:page>