Tuesday 30 July 2013

How to Use NOT LIKE keyword in SOQL Query


Example:

To get the count of users from sales force where the email address of the user doesn't belongs some specific domain, let's say 'abcd'

Here is the SOQL

Select count() from User where NOT email like '%@abcd.com'

Tuesday 23 July 2013

Formula field to capture Record/Case Age in Days ,hour and Minutes in Sales force

Here is the formula for Capturing age of record in Days Hours and Minutes.

Text(FLOOR(NOW() - CreatedDate)) &" "&"day(s)" &" "&Text(FLOOR(MOD( (NOW() - CreatedDate) * 24 ,24))) &" "&"hrs"&" "&Text( FLOOR(MOD(((NOW() - CreatedDate) * 24)*60 ,60)))&" "&"min"

Saturday 13 July 2013

How to utilize deserialize method to parse JSON object in sales force.

Parsing individual elements from  JSON object can be done from sales force Json Parser, but if the data in the Json Object is quite big then the parser will easily hit the script exception.

To parse a JSON Object with following structure can easily done a single line of code using deserialize method in Apex JSON class. But for that there should be an Object model to map the Json Object attributes into an apex Object.

Let's assume you have received a JSON Object in the below format.
{
 "kind": "calendar#events",
 "etag": "Some Text",
 "summary": "mailaddress@gmail.com",
 "updated": "2013-07-11T08:09:37.786Z",
 "timeZone": "Asia/Calcutta",
 "accessRole": "owner",
 "defaultReminders": [
  {
   "method": "email",
   "minutes": 10
  },
  {
   "method": "popup",
   "minutes": 30
  }
 ]
 }

To use deserialize method we need to have a class model to map the JSON Object data into class.
The class model for the above JSON Object is given below.


class Calender
{
 String kind;
 String etag ;
 String summary;
 String updated
 String timeZone;
 String accessRole,
 List<remainder> defaultReminders;
class remainder
{
     String method;
    String minutes;
}
}
The deserialize method map the JSON object properties into apex class properties by variable name, So the variable name in the class should match the Json Object attributes.


To map the JSON object into an apex object the deserialize method can be used as follows.
Calender c = (Calender)System.JSON.deserialize(JsonObjectBody,Calender.class);

The calender object c will receive all the attributes from Json Object.





Thursday 11 July 2013

How to get field value from visual force page to Java script function

How to get field value from visual force page to Java script function


<apex:page sidebar="false">
<script>
function method1(data)
{
//var dataVal= data.value;
var d=document.getElementById(data).value;
alert('********The field value is ********'+d);
}
</script>
                <apex:pageBlock id="pgblock1" >
                            <apex:form id="form1">
                            <apex:outputText >First Name</apex:outputText><br/>
                            <apex:inputText onchange="method1('{!$Component.pgblock1.form1.fname}');" id="fname" />
                            <br/><hr/>
                            <apex:outputText >Last Name</apex:outputText><br/>
                            <apex:inputText /><br/>
                         
                            </apex:form>
             
                </apex:pageBlock>
   
</apex:page>

Using jQuery tooltip in Visual Force pages


Example of using Jquery Tooltip in Visual force pages.

1.Include the jQuery package in the VF Page
2.call the method tooltip() with reference to the component in the VF page, here in the code the i have called tooltip() method with reference to the entire document. So wherever there is a tag with title attribute the tooltip will be shown with text in the title attribute.

Here is the code :

<apex:page >
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$ = jQuery.noConflict();
$(function() {
$( document ).tooltip()
}
);
</script>

<apex:form >
<apex:inputText title="Input line 1"/><br/>
<apex:inputText title="Input text2"/><br/>
</apex:form>
</apex:page>

Sunday 7 July 2013

Function to generate Datetime from Datetime string in apex.

Function to generate Datetime from string(Datetime) in apex.

It's assumed that the parameter give to the function is string that's in datetime. The output from this function is an apex supported datetime variable. So datetime in string from external application can utilize the below function to get apex variable in datetime format.


The input parameter should look like : 2013-06-28T13:00:00+05:30 , otherwise string exception is thrown since the below code depends on the input format.

This is just for reference, you can refine the code further.

public datetime  datetimefromString (String stringDate)
{
        String dates = stringDate.subString(0,stringDate.indexOf('T'));
        String[] str = dates.split('-');
        Integer yr = Integer.valueOf(str[0]);
        Integer mnth = Integer.valueOf(str[1]);
        Integer dy = Integer.valueOf(str[2]);
      
        String times  = stringDate.subString(stringDate.indexOf('T')+1, stringDate.length());
        String[] tme = times.split(':');
        Integer hour = Integer.valueOf(tme[0]);
        Integer min = Integer.valueOf(tme[1]);
        Integer  sec = Integer.valueOf(tme[2].substring(0,tme[2].indexof('+')));
      
      
        Datetime d = Datetime.newInstance(yr,mnth,dy,hour,min,sec);
        return d;
      
}

Saturday 6 July 2013

Dataloader not updating field value to blank.

Dataloader not updating field value to blank.

Solution:

Just put the field value as #N/A in the .csv file and enable BULK API in the dataloader. Then do update, it will be updating the field value to blank.