Thursday, April 29, 2010

Visualforce File Upload - for Any SObject


How to Upload Attachment to any SObject using Visualforce

This article explains how you can upload file attachments to any SOobject using Visualforce.


Page:



<apex:page standardController="YourSObjectName" extensions="VFFileUpload">
  <apex:form>
      <apex:pageBlock title="Upload Attachment">
            <apex:inputFile style="width:100%" id="fileToUpload" value="{!fileBody}" filename="{!fileName}" />
            <apex:commandButton value="Upload Attachment" action="{!UploadFile}"/>
       </apex:pageBlock>
  </apex:form>
</apex:page>

Class:



public class VFFileUpload
{
    public Id recId
    {    get;set;    }
    
    public VFFileUpload(ApexPages.StandardController ctlr)
    {
       recId = ctlr.getRecord().Id;     
    }
    
    public string fileName 
    {    get;set;    }
    
    public Blob fileBody 
    {    get;set;    }
  
    public PageReference UploadFile()
    {
        PageReference pr;
        if(fileBody != null && fileName != null)
        {
          Attachment myAttachment  = new Attachment();
          myAttachment.Body = fileBody;
          myAttachment.Name = fileName;
          myAttachment.ParentId = recId;
          insert myAttachment;
           pr = new PageReference('/' + myAttachment.Id);
           pr.setRedirect(true);
           return pr;
        }
        return null;
    }    
}


You can use the above code for any object whether it is standard or custom. Happy coding!

Thanks,
Srinivas,
Technology Evangelist,
Trekbin Technologies,
www.trekbin.com

Salesforce.com FAQ - Technical Questions

Here are more technical questions, that any new one to salesforce has to learn.

1. What are recursive triggers. How can we avoid the recursion problem
2. What are Apex Governer Limits.
3. What are the Spring'10 features
4. How do you use an actionFunction tag
5. What is the difference between apex:actionFunction and apex:actionSupport tag
6. What is actionPoller
7. How do you do FileUpload using Visualforce
8. What is the difference between a Profile and Role
9. What is appexchange? How can I host my application on appexchange
10. What are the different editions available on salesforce
11. What is batch apex.
12. When will we use batch apex and what is the best practice
13. What are webservice callouts
14. What are wrapper classes
15. When do we use wrapper classes

....More questions to be followed. Stay tuned...

You will get answers for these questions mostly form apex and visualforce documentation provided by salesforce. If you find any real trouble in getting an answer I wil defintely help you out.

'Most of them I will be covering in my blog as articles in the very near future

More General/Customization questions on Salesforce.com

Thanks,
Srinivas,
Technology Evangelist,
Trekbin Technologies,
www.trekbin.com

Tuesday, April 27, 2010

Chatter or Twitter - Salesforce?


Chatter or Twitter ? :(

Salesforce has come up with another powerful (is it so?) tool called Chatter. It is a social collaboration platform where people can share anything from profile updates to documents. And of course the idea is not new. It has been taken from Twitter mainly and may be from other social networking sites like facebook.

What is the real value that chatter is going to add to your organization or sales people? your sales people can easily collaborate with each other and can track any record literally, What is happening inside the organization, what other sales reps are doing, what records are getting updated etc. This will really add a value when all the users or most of them use it extensively to track their performance against others or organizations performance. Do Sales reps really have that much time to track all of this and analyse themselves. I doubt.

Twitter is not made for professional networking and its not a performance tracking system. What i heard of from lot of tweeple (yes what they call people use twitter is tweeple :)) about why they use twitter is "No one gives a damn here on what are you doing". I really don't think this is the case with chatter.

One more biggest pain point I am seing is chatter is of no use to small scale organizations who hardly have a few sales representatives and really want to concentrate on increasing the business and not chat or tweet with other people in the orgnization.

These are all my personal opinions and What I am concerned about is, there are hell of ideas on idea exchange which have to be addressed first and these people are wasting their time on internal social networking...A surprise from salesforce.com ....... I am a big admirer of Salesforce.com and I can see the value it can bring to CRM breaking through barriers, at the same time I stick to the basics that you concentrate on increasing the business for customers and addressing their pain points. This is the first time I am getting contradictory to a salesforce.com feature.

There's plenty of comment on Twitter from folks attending Dreamforce. A sampling:
  •      "Wonder what Facebook will think of this? Could Facebook go into competition on this, providing 'private' social networks?" From @SaaSEurope, David Bradshaw.
  •      "Salesforce Chatter calling out Sharepoint 2010 - emphasizing bringing in other native apps. Also showing live tweets on SP hahaha." From @JuliaMak.
  •      "Chatter icon looks like dentures. Anyone want some Super Poligrip with that?" From @rwang0, Altimeter Group's Ray Wang.
  •      "Salesforce Dreamforce Keynote outlasts audience bladders, everyone is leaving during Benioff Chatter keynote." From @marksmithvr, Ventana Research's Mark Smith.
  •      Also from Mark Smith: "Salesforce Chatter support all Force.com native apps so that you can link collaboration across Salesforce ecosystem."
Krigsman says Chatter will need three things to succeed: ability to filter noise,accessibility to non-Salesforce customers and great performance.
reference from itbusinessedge
.

Saturday, April 24, 2010

Bulk Triggers in Salesforce


Bulk Enabled Triggers in Salesforce

When you have a need of writing a trigger, it is strongly recommended that you make it bulk enabled, so that the trigger can handle number of records whcih can be inserted/updated/deleted through data loader or any other data migration tool.
I have explained writing a basic trigger here.


I will explain how to bulk enable a trigger now in this article. Consider a scenario where you might want to update the opportunity status to "Closed-Won" depending on a boolean field CloseOpportunities__c on each of the account that has been updated (That is, if you want to close the opportunities related to accounts that are being updated). And you will be uploading hundreds of accounts using a data uploader tool.

Here is the trigger.

 


Trigger myTrigger on Account(after update)
{
   
    Set <Id> setAccId = new Set<Id>();

    for(Account a: Trigger.new)
    {
        if(a.CloseOpportunities__c)
            setAccId.add(a.Id);//set always contains distinct Ids
    }
   
    list <Opportunity> lstOpp = [select Id, StageName from Opportunity where AccountId in : setAccId];
    for(Integer i = 0; i < lstOpp.size(); i++)
    {
        lstOpp[i].StageName = "Closed-Won";
    }
    if(lstOpp.size() > 0)
        update lstOpp;

}




And a more genaralised way is something like this.


tirgger MyTrigger on SObject([your events goes here comma seperated])
{
       Set<Id> SetOfIds = new Set<Id>();

       for(SObject s : trigger.new)
       {
             SetOfIds.add(s.idField);
       }
  

       Map<Id,SObject> objMap = new Map<Id,SObject>([select Id, [Other Fields] from SObject where Id in : SetOfIds]);

       for(SObject s : trigger.new)
       {
              //do your processing with map
       }
}



 
Hope this is useful.


Thanks
Srinivas,
www.trekbin.com

Friday, April 23, 2010

Call External Web Service from Salesforce Apex

Call External Web Service from Salesforce Apex

Some times, you may need to call an extenal web service which might have written on a serverside language like .net, php or java.
Once you made your web service on the serverside or you can use a third party web service api.

I will explain you this using Authorize.net payment gateway.
There are several types of payment criterias available for Authorize.net. For an example I will go with CIM (Customer Information Manager)
You will find all the details in Authoirze.net developer documentation.

To do payments on authorize using CIM we need to create customer profile first.

Here is the apex code that calls an external web service.

//Before any Apex callout can call an external site, that site must be registered in the Remote Site Settings page, or the call will fail. The platform, by default, prevents calls to unauthorized network addresses.
//You can do this like this: got Setup->Administration setup->Security controls->Remote site settings->New and add your endpoint URL there.
//In our case the ende point is https://api.authorize.net/soap/v1/Service.asmx.

Public class callExternalWS
{
    public void invokeExternalWs()
    {
        HttpRequest req = new HttpRequest();
        //Set HTTPRequest Method
        req.setMethod('POST');
        req.setEndpoint('https://api.authorize.net/soap/v1/Service.asmx');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'text/xml; charset=utf-8');
        req.setHeader('SOAPAction', 'https://api.authorize.net/soap/v1/CreateCustomerProfile');//
        string b =   '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+
                      '<soap:Body><CreateCustomerProfile xmlns="https://api.authorize.net/soap/v1/">'+
                      '<merchantAuthentication><name>Merchant name here</name>'+
                      '<transactionKey>Transaction Key here</transactionKey></merchantAuthentication>'+
'<profile><description>description</description>'+
                      '<email>sforce2009@gmail.com</email>'+
                      '<paymentProfiles>'+
                      '<CustomerPaymentProfileType><customerType>individual</customerType>'+
'<payment><creditCard><cardNumber>6011000000000012</cardNumber>'+
                      '<expirationDate>2009-12</expirationDate></creditCard>'+
                      '</payment></CustomerPaymentProfileType></paymentProfiles></profile>'+
                      '</CreateCustomerProfile></soap:Body></soap:Envelope>';
        req.setBody(b);
        Http http = new Http();
        try {
          //Execute web service call here       
          HTTPResponse res = http.send(req);   
          //Helpful debug messages
          System.debug(res.toString());
          System.debug('STATUS:'+res.getStatus());
          System.debug('STATUS_CODE:'+res.getStatusCode());
        //YOU CAN ALWAYS PARSE THE RESPONSE XML USING XmlStreamReader  CLASS
       } catch(System.CalloutException e) {
            //Exception handling goes here....
     }       
}
}

Hope this is useful

Saturday, April 17, 2010

How to use Batch Apex in Salesforce


Batch Apex in salesforce 
As you all might know about the salesforce governer limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governer limits.
But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/editing/deleting them when needed.
Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.
We have to create an global apex class which extends Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs. Below is a sample class which is designed to delete all the records of Account object (Lets say your organization contains more than 50 thousand records and you want to mass delete all of them).

global class deleteAccounts implements Database.Batchable
{
global final String Query;
global deleteAccounts(String q)
{
Query=q;
}

global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC,List scope)
{
List <Account> lstAccount = new list<Account>();
for(Sobject s : scope)
{
 Account a = (Account)s;
lstAccount.add(a);
}
Delete lstAccount;
}

global void finish(Database.BatchableContext BC)
{
                //Send an email to the User after your batch completes
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {‘sforce2009@gmail.com’};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done‘);
mail.setPlainTextBody('The batch Apex job processed ');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}


///////////////////This is how the batch class is called
id batchinstanceid = database.executeBatch(new deleteAccounts(‘select Id from Account’));

When you instantiate the global class and called it using database.executeBatch, the process gets started. As you have observed the deleteAccounts class accepts your query as a parameter in the constructor and sets it to the string variable called Query.
The start method of deleteAccount class which extends database.batchable interface sets the current query to execute using the Database.getQueryLocator method.
Then the result of the query can be captured in execute method. The scope list of SObjects returned as the result of your query.
When you are executing your batch with a query which returns thousands of records, the batch will be executed with 200 records each time that is salesforce divides the total number of records in to batches. Each batch contains 200 records by default (though this is configurable less than 200 by just introducing the number when you are calling the batch class like database.executebatch(new deleteAccounts(‘your query’), number here);).
After processing each batch, the governor limits are reset.
Once the whole batch of thousands records are done the Finish method gets called and an email will be sent to the specified person.


Wednesday, April 14, 2010

Set Id for Apex insert Tag

How to set Id parameter for Apex insert Tag


I came to an interesting question on community. the above. you might want to check (reply from sforce2009, its me :) ).


Thanks,
Srinivas

Saturday, April 3, 2010

GROUP BY Clause in salesforce SOQL

How to use GROUP BY clause in Salesforce

As you know, there are some good things in Spring’10 release of salesforce.com for developers, one of them for SOQL and yet a powerful feature is Introducing GROUP BY Clause and Aggregate functions like MAX(), SUM() etc.

People who are from SQL background or who know SQL are definitely, great appreciators of GROUP BY usage in SQL.

Till now, in salesforce if we want to achieve this feature, we were dealing with the apex code only.
The new syntax is listed below.
SELECT fieldList FROM objectType[WHERE conditionExpression]
[WITH [DATA CATEGORY] filteringExpression]
[GROUP BY fieldGroupByList] | [GROUP BY ROLLUP|CUBE (fieldSubtotalGroupByList)]
[HAVING havingConditionExpression]
[ORDER BY fieldOrderByList ASC | DESC ? NULLS FIRST | LAST ?]
[LIMIT ?]
As you observe on the above syntax, GROUP BY is of 3 types here.

GROUP BY Explained with Visualforce report:

Page:

<apex:page controller="TestGroupBy">
  <apex:pageBlock title="Test Group By">
      <apex:pageBlockTable value="{!Results}" var="ar">
          <apex:column headerValue="Number of Opportunities" value="{!ar.Total}"/>
           <apex:column headerValue="Close Date" value="{!ar.CloseDate}"/>
      </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

Class:
public class TestGroupBy
{
public list<AggregateResult> lstAR = new list<AggregateResult>();
/*
Note that any query that includes an aggregate function returns its results in an array of AggregateResult objects. AggregateResult is a read-only sObject and is only used for query results.
Aggregate functions become a more powerful tool to generate reports when you use them with a GROUP BY clause. For example, you could find the count of all opportunities for a CloseDate.

*/
public TestGroupBy()
{
lstAR = [SELECT CloseDate, COUNT(id) Total FROM Opportunity GROUP BY CloseDate];
}

public list<OppClass> getResults()
{
list<OppClass> lstResult = new list<OppClass>();
for (AggregateResult ar: lstAR)
{
oppClass objOppClass = new oppClass(ar);
lstResult.add(objOppClass);
}
return lstResult;
}

class oppClass
{
public Integer Total
{ get;set; }

public Date CloseDate
{ get;set; }

public oppClass(AggregateResult ar)
{
//Note that ar returns objects as results, so you need type conversion here
Total = (Integer)ar.get('Total');
CloseDate = (Date)ar.get('CloseDate');
}
}
}


Result:


GROUPING, GROUP BY CUBE, GROUP BY ROLLUP,

These topics are better explained in the apex pdf.
To use them in visualforce, after writing the specific query, you can use the above Page and Class

Friday, April 2, 2010

Visualforce

Visualforce is a powerful tool for designing your forms with similar look and feel of Salesforce default layouts. Let’s say you want to build a search page for Account Object. That is you wish to list all the existing accounts in a list and search functionality by using which you want to view the filtered accounts.
To do the same functionality before Visualforce era, it was very cumbersome and demand more knowledge on javascript (which may not be reliable on all browsers). Visual force is the markup language introduced by Salesforce, It provides a similar look and feel of a Salesforce standard object by just introducing some tags whose namespace is prefixed by apex.
We can use the standard object properties and styles by just setting the attribute StandardController to the desired object like below
<apex:page standardController=”Account”></apex:page>

An example which uses Account as StandardController and renders Account style is below.
<apex:page standardController="Account">
<apex:form>
<apex:pageBlock title="Edit Account for{!$User.FirstName}">
<apex:pageBlockSection>
<apex:inputField value="{!account.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

The above code inherits a pageblock which is similar to Account standard layout page section with the Name field editable. If you observe the Pageblock’s title, we can still use the Formula Merge Fields just like in S-Controls.
In my next Article on Visualforce, I will be posting the code for the functionality described at top (Search functionality on Account listing). This will involve more abilities of Visualforce.

Triggers

Writing a basic Trigger:

The possible events of trigger are
  • before insert, before update, before delete, after insert, after update, after delete, after undelete
Example:

trigger MyTrigger on Account(after insert)
{
//Update Account Name by appending some dummy text
Account a = new Account(Id= trigger.new[0].Id);
a.Name = 'Test ' + Trigger.new[0].Name;
update a;
}

Please note, the above is a very basic trigger. I will discuss in other article how to write a Trigger which is bulk enabled

Salesforce.com FAQ or Interview Questions

People who are using salesforce.com or newly migrating to this platform or who are seeking a career on this platform definitely face some challenges. I am trying to consolidate a few technical questions here.
Questions

1. What is Apex
Ans: It is the in-house technology of salesforce.com which is similar to Java programming with object oriented concepts and to write our own custom logic.

2. What is an S-Control
Ans: S-Controls are the predominant salesforce.com widgets which are completely based on Javascript. These are hosted by salesforce but executed at client side. S-Controls are superseded by Visualforce now.

3. What is a Visualforce Page
Ans: As I said in the above answer, S-controls are superseded by Visulaforce, Visualforce is the new markup language from salesforce, by using which, We can render the standard styles of salesforce. We can still use HTML here in Visualforce. Each visualforce tag always begins with "apex" namespace. All the design part can be acomplished by using Visualforce Markup Language and the business logic can be written in custom controllers associated with the Page.

4. Will Visual force still supports the mege fields usage like S-control.
Ans: Yes. Just like S-Controls, Visualforce Pages support embedded merge fields, like the {!$User.FirstName} used in the example.

5. Where can I write my Visualforce code
Ans: You can write the code basically in 3 ways. One go to, setup->App Setup->Develop->Pages and create new Visulaforce page. While creating the Page, You will find a salesforce editor. You can write your Visualforce content there. Or go to Setup -> My Personal Information -> Personal Information -> Edit check the checkbox development mode. When you run the page like this, https://ap1.salesforce.com/apex/MyTestPage. you will find the Page editor at the bottom of the page. You can write you page as well as the controller class associated with it, there it self. OR Using EclipseIDE you can create the Visulaforce page and write the code.

Stay tuned.... I will be posting more questions and articles on salesforce.com technical side very soon.

More Qustions are Here. All the best for the newbies...

Thanks Srinivas,
www.trekbin.com