Saturday, June 28, 2014

How to configure your android device for office and personal work

Ok so this is a slity non saleforce related topic but it is very useful. It will also be helpful in facing some saleforce related task. So here it goes, if you need a proper configuration then get these things setup in your device:

1. Setup your gmail account and link it with your phone.

2. Setup your office mail in the mail application.

3. Use google calander to plan meeting and share it with people.

4. Install google drive, watsup, viber , skype, adobe reader, winzip and quick office.

5. For saleforce users install saleforce1 and saleforceA.

6. Use 3g networks for good speed.

7. Use google map to share path ways for office location or meeting halls.

Feel free to add more fetures by posting in the comments section.

10 things you must have if you are a traveling saleforce developer

The world of saleforce is so connected and vast that many time it may bappen that you have to go to other cities for work or meeting. And if you are a developer or admin it might happen you have to develop or config some of the changes that customer asked ASAP. Here are 10 things you must have so you can complete the task

1. A decient internet connection, if you have dongel its good but if not then at least a 3g enabled mobile phone with modem support. You can easly attach android to laptop via usb cable and share data connection by going in setting>tethadering port>usb tethadering.

2. Eclips helios for developing and depoloyment related task.

3. List of all username,password and security token.

4. SaleforceA installed in your device, believe me its very usefull for smaller task like resetting userpassword.

5. Saleforce1 for recent changes for mobile varification.

6. Link for database.io an online data loader.

7. Saleforce Soql guide for reference.

8. Wight list your ip if you are accessing the org from some other or new network. Note this has to be done before flying out or ask your admin to provoid you.

9. Microsoft excel or open office installed for accesing importand document. Or if not use chrome it has pdf and doc support in built.

10. If you have laptop that you carry then admin rights, some time we need to install thoes handy little software to debug webservice callout failure.

Hope this help you out have a nice day.

Friday, June 27, 2014

Hot to read debug logs in salesforce

There are times when we have that one buggy code and to get it sorted out we have to make our hands dirty. Reading the debug code log is a very frustrating task. If you have to do it keep the below points in mind it might help you.

1. Always search for "USER_DEBUG" chances are the may be some with logical values.
2. If the code is going in catch block search for "Exception" it will quickly show the error message.
3. The loop variables and flow will again return to the start of the execution.
4. Always read the line number in "[]" as we can quickly which line the code went in.
5. If the code debug is for trigger then remember that before events value changes are not captured in log implicitly.
6. If you have manage package with triggers then for the triggers the values will appear in separate limit log in side debug log.

Hop these tips help you get most out of debug logs.

Thursday, June 26, 2014

How to use "Savepoint" and "Rollback" in apex Salesforce

Many times when we are coding and handling exception we do not thing about transaction control. Transaction control is a process to handle the flow to data that is being committed to data base based on the condition that satisfy our logic. Consider this in a code flow you are updating Account and at the same time you are creating related Opportunity records, this is in try catch block and the yet to be created Opportunity records do not satisfy the validation condition and fails to insert, but the Accounts are already updated and because it is a try block the error is just thrown to catch which do not do any thing.


We can use "Savepoint" and "Rollback" to revert the changes that "Update" operation has done.

Example:
In the above code we are setting "Savepoint" at line 9 and reverting database at line 17.
Hope this help some one.

Wednesday, June 25, 2014

How to have sorting in list for visualforce Salesforce

Many time when working with visualforce page to display list of records to the user the commonly asked feature is to have sorting. Sorting can be easy achieved by using standerd saleforce having the below structure :

[ORDER BY fieldExpression ASC | DESC ? NULLS FIRST | LAST ?]

or simply it can be written as :

SELECT Name FROM Account ORDER BY Name DESC NULLS LAST

But that is not the only way, we can do this by the below mentioned ways:
  • Sorting (Order By) in SOQL.
  • Using List methods to sort list.
  • Custom sorting.
Sorting (Order By) in SOQL.
Example : SELECT Name FROM Account ORDER BY Name DESC NULLS LAST

Using List methods to sort list.
Example : List.sort();

Using this method, you can sort primitive types and sObjects (standard objects, custom objects, and SelectOption). For more information on the sort order used for sObjects, see List Sorting. You can also sort your own custom types if they


Custom sorting.
Example : 

Friday, June 20, 2014

How to check "undefined" in javascript in visualforce Salesforce

It is very important that if we are using javascript remoting that we check the result for undefined, as it might happen that no value is retrieved for the result and we are referring it in the page.
Take an example of this:
var res = result;
alert(res.Custom__c);
it returns "undefined" if the value of "Custom__c" is null.

To overcome this in javascript we can check the variable for typeof undefines as:

if (typeof(var) == 'undefined')  
or
if (typeof(var) != 'undefined')
Hope this helps some one :)

How to get records from inner SOQL query

Inner SOQL query are great way to get related child records when querying parent records. In back end its just file "Join" query so that results will be displayed encapsulated by parent records. I came up with this query when I need to get all attachments of a account and display it on the visualforce page as a download link.

List<Account> vAccLst = [SELECT Id, Name, Type, (SELECT Id,ContentType from Attachments) from Account where Id IN: vSetAcc];

This will return the result in some what this form :
You can use the attachment as mentioned below:

  • String IdAttach = vAccLst[0].Attachments[0].Id;

or a more robust approach is :

  • Set<String> attachId = new Set<String>();
  • for(Account acc: vAccLst){
  • for(Attachments a : acc.Attachments){
  •      attachId.add(a.Id);
  • }
  • }

Wednesday, June 18, 2014

How to get started with Salesforce1 visualforce development using Jquery mobile

Today application on force.com are very stable and are well integrated with each other, but as the new phase is coming we have to learn to develop mobile application on the same platform. As many application are now moving to mobile platform Salesforce has also joined in the race.

Salesfore1 platform enables you the develop and deploy the mobile version compatible application to cloud. Using the same technology with a little help of Jquery we can make mobile enable visualforce pages  to meet our requirement. 

Follow these steps to get the mobile version example and Jquery mobile zip installed in your organization.
Salesforce1 Mobile package for more information you can go to Getting Started.

After installing the above package create a new visualforce page and paste this code:

Now paste this class code:


The above code is for custom lookup, as we cannot get standard lookup in visualforce pages yet.
Hope this helps you.

How to use System.assert in Salesforce

So you are writing code and trigger a lot but as a mandatory step you need to have at lest 75% or greater of production code coverage to move your code. "Ok no problem I will write a test class for this trigger/class" will be your answer, but what happen if your code contains try catch block or you have some value returning from a method that you want to cross check. To cover the above scenario we can take help from "System.assert" method provided by salesforce.

Reference Link

Say I have a validation rule on opportunity that will only allow me to insert opportunity records if the probability is more then 50%. And I am inserting an "Opportunity" at the time of "Account" creation:


Now in my above if I try to give probability less then 50 then error will occur and my test will fail. To avoid this in my test class I can use try catch and inside catch I can use assert like this:

Hope this helps you.

How to get OAuth / Access token from Salesforce

Ok so you are doing integration and need to get the oauth token. There are many ways we can get it but if you need you can brows from these klinks :

Server Side Auth
Username and Password Auth
Understanding Auth

But for now we can just use this:

Request:
https://login.salesforce.com/services/oauth2/token

grant_type:password
client_id:********************************************
client_secret:***************
username:<Username>
password:<Password><Sec Token>

client_id: Is the client id of the connected app.
client_secret: Is the secret of the connected app.

The responce will be some thing like this with the access token:

Responce:
{ "id": "https://login.salesforce.com/id/XXXXXXXXX/XXXXXXXXXXXX",
"issued_at": "1399267999911",
"token_type": "Bearer",
"instance_url": "https://ap1.salesforce.com",
"signature": "XXXXQWXXXX/xXXXXXMsN/CCCCyrHzI=",
"access_token": "0FFGGGFFFG!AQEAQNpfMEtD1nccdssafaFFGGYCFneh8HGo9MHV9Eowl5zoNcNUwhWMb6RpdQcTPFakqxQSQmD_1jIBB78mjK95" }

How to access Connection User in debug log.

We all work with external points where data is created by connection user in salesforce. One example is the Salesforce to Salesforce connection record share. In this the data is created by a special user called as "Connection User". "That's ok what is the problem in that" well the problem is that if there is a trigger or validation rule that is not letting the data to be created in the receiving org then we cannot view "Connection User" in debug log.
But there is a little trick that you can use :

https://XXX.salesforce.com/p/setup/layout/AddApexDebugLogUser?retURL=%2Fsetup%2Fui%2FlistApexTraces.apexp&UserLookupInput_lkid=YYYYYYYYYYYYYY
&UserLookupInput=Connection%20User

Use this URL and replace "XXX" with your domain name and "YYYYYYYYYYYYYY" with the ID of the connection user (can get it if you have any record in the org created by that user, the Id will be in the OwnerId). Just put those there and hit enter the user will be added in the debug log.

Tuesday, June 17, 2014

Sunday, June 15, 2014

How to get Element Id using Jquery in Visualforce Salesforce

Ok so you have made a very nice page and want to add Jquery to get some visual effect or maybe for some validation, but stuck on how to get Id of an element. As in Javascript we can get Id using hardcoding as:
var eleId = documnet.getElementById("jO1:jO8");

The problem in Jquery is that we can use "#" tag to get the value but it is not effective as some time we may need to get array to values so in that case we can use the below approach, also we can use name to get jquery object.



Thursday, June 12, 2014

How to solve "unable to send email" problem in Salesforce

Some time an error occurs with a nature of "unable to send email" in apex or in workflow. The first place to look is in the "Email Administration".

Go to Setup > Administration Setup > Deliverability > Access to Send Email
and check the drop-down list : it should be "All email". If not change it to "All email" and save.

That's it.

Sunday, June 8, 2014

How to pass parameter from page to component in visualforce page

When we work with visualforce pages then it is a good practice to put the code that will be used more then once into a component. This approch is good but it is not dynamic say "Search". If you need a page to Search different objects and also need to use component then an fast approch is to pass the variable name as parameter and search on that bases.

Example:

Now if the page is passing the value in objName parameter then at the component side we can get this as :

Example:

Now the values passed from the page is assigned to the variable in component.

Thursday, June 5, 2014

How to use Hierarchy Custom Setting

Hierarchy Custom Setting is one of the powerful feature provided by salesforce. Powerful as because we can use it in Apex code, Workflow Process and Validation Rule. To use it in workflow and validation rule just use it as :
{!$Setup.CustomSettingName__c.CustomFieldName__c}
If you want to use it in code then see the below example:
CustomSetting__c cus = CustomSetting__c getInstance(Userinfo.getUserId());
string mPhone = cus.CustomField__c;

  • getInstance() Returns a custom setting data set record for the current user.
  • getInstance(ID) Returns the custom setting data set record for the specified user ID.
  • getInstance(ID) Returns the custom setting data set record for the specified profile ID.
  • getOrgDefaults() Returns the custom setting data set record for the organization.
  • getValues(ID) Returns the custom setting data set record for the specified user ID.
  • getValues(ID) Returns the custom setting data set for the specified profile ID.

Monday, June 2, 2014

How to use Aggregate SOQL

Aggregate functions in SOQL, such as SUM() and MAX(), allow you to roll up and summarize your data in a query. You can use aggregate functions without using a GROUP BY clause. To get result of aggregate Soql query we have to use AggregateResult array or list.

Aggregate Function : 
  • AVG()
  • COUNT() and COUNT(fieldName)
  • COUNT_DISTINCT()
  • MIN()
  • MAX()
  • SUM()
However, these functions become a more powerful tool to generate reports when you use them with a GROUP BY clause.