Saturday 13 September 2014

JSON Integration and handling control characters in JSON Response

Below is the code which sends a Post Http Request and handles the Special or Control characters in Json response while parsing.

Note:- While using the regex '\\p{Cntrl}' , if the length of the Json response is more than the limit then you may get Regex too complicated Error message in apex class. In this case you can apply regex on substring of a Json string and then join the substring back as shown below:-

Integer jsonlength = jsonStr.length();
    String newJsonstr = '';
    
    
    if(jsonlength > 100000)
    {
      Integer Quot = jsonlength/100000;
      Integer modcheck = math.mod(jsonlength,100000);
      
        
        for(Integer i = 0; i < Quot; i++)
           newJsonStr += jsonStr.substring(i * 100000,100000 * (i+1)).replaceAll('\\p{Cntrl}', '');
    
    if(modcheck > 0)
       newJsonStr += jsonStr.substring(jsonlength - modcheck,jsonlength).replaceAll('\\p{Cntrl}', '');
      
    }

Thursday 7 August 2014

Trigger to count the Number of Open Task in Opportunity

Below is one of the example code which helps to get the count of open Task in related Opportunity



Explanation of the Trigger

Code Block 1:-

trigger OpenTaskCount on Task (after insert, after update, after delete, after undelete) 

This Trigger fires on after insert, update, delete and undelete events on Task object
similarly, this trigger can be used on any child object:-

trigger <TriggerName> on ChildObject (after insert, after update, after delete, after undelete)

Code Block 2:-

List<Task> taskList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;
 

If the event is "after Insert" or "after undelete", then the list takes the value Trigger.new or else it takes Trigger.Old
similarly,

List<ChildObject> lstChild = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;

Code Block 3:-

List<Id> taskIds = new List<Id>();
    for (Task tsk : taskList) {
        taskIds.add(tsk.WhatID);
    }

This above code helps to get the RelatedTo Id i.e The Opportunity Id to which this task belongs.
similarly, get the parent id by using the following

List<Id> ChildIds = new List();
    for (ChildObject chd : ChildList) {
        ChildIds.add(chd.ParentFieldname);
    }

Code Block 4:-

 List<Opportunity> oppList = [
            select
                id,
                (select id, WhatID, IsClosed from Tasks),
                 Open_Tasks__c,Total_Tasks__c
            from
                Opportunity
            where
                id in :taskIds];

The above code is the SubQuery which gives us the Number of task records present in that opportunity.
(select id, WhatID, IsClosed from Tasks) is the query which gives the task record and important to note here that "Tasks" is not the Object name, it is the Child Relationship Name.
To get that name, click on Relatedto field in object Task and check the ChildRelationship name.
similarly,

 
List<ParentObject> ParentList = [
            select
                id,
                (select FieldNames from ChildRelationshipName),
                
            from
                ParentObject
            where
                id in :ChildIds];
 
For Custom Object, append the ChildRelationshipName with "__r" i.e ChildRelationshipName__r

Code Block 5:-

 for (Opportunity opp : oppList) {
        Integer count = 0;
        for(Task tsk : opp.Tasks)
         {
           if(tsk.WhatId != null  && !tsk.IsClosed) 
           count += 1;
         }  
              
        opp.Total_Tasks__c = opp.Tasks.size();
        opp.Open_Tasks__c = count;
    }
    update oppList;  

The above code gives us the logic to rollup and count the Open Task.
similarly,

 for(Parentobject parent : ParentList) {
        Integer count = 0;
        for(Integer i =0;i < opp.ChildRelationshipName.size(); i++)
        {
         if(opp.ChildRelationshipName[i].FieldName != null  && !opp.ChildRelationshipName[i].IsClosed) 
          count = count + 1;
          
        }
        parent.FieldName = count;
    }
    update ParentList; 
 

Note:- So in any Case if you want to write a Trigger which updates the Parent object field, the above code can do the trick.

Sunday 3 August 2014

Send Template Email to Custom object email field

Well Recently I was figuring it out to send email to a custom object email field through the list view page custom button. On click of the button it should open a small panel to select the Email Template and send it to the selected email.



Hmm pretty cool panel. But how to do it?
This is how I started,
1) Create a global class as follows. (Note :- You need to replace the "Person_Detail__c" object with your custom object and the "Email__c" field with your custom email field)



2) Created a List View button named "Send Email" on your custom object with content source as "onclick Javascript" option



3) Below is the Javascript code for the button. (Note:- In my case I used "Person_Detail__c" as custom object. You need to use your custom object instead of "Person_Detail__c")


That's it, now click on the "Send Email" button and send cool template emails to your recepients..