Saturday, 14 December 2013

Dynamic CreatedDate/LastModifiedDate in Test Class Salesforce

 Hi All,

Recently I came across this issue , In Testclass we used to create testrecords to test the functionalities of the code we have written ,but the problem here is like

whenever we insert the test records in test class we can't specify the CreatedDate since that is the ReadOnly Field.

In my scenario i will be creating  task for the same business day for the lead when the user tries to create that in the business hours (9 AM to 5 PM -May varies

according to org)   or else the task will be created in the next business day .

So the code tries to access the createdDate (time) and act according , So whenever we create the test record in the test class , the whole code will not be covered.
Code coverage here is based on the time we run the test class.

Inorder to get 100% code coverage we need to insert test records
#1.within Business hours
#2.Out of Business hours.

Actually  inserting test records within /out of business hours is not possible until we are able to make createdDate field is dynamic .Solution is to make the createdDate field dyanmic for test class.

We can prepare the test records  in a csv file - with createdDate(specified) and upload that in the static resource and we can refer that static resource file in the test class.


  1.  @istest
  2.  public class TestLeadTaskAutoCreation{    
  3.    public static testMethod void test()
  4.     {
  5.     // TestLeadCreatedDate is the name of static resource which contains a CSV file with Test Lead Records that will be test inserted during test run.
  6.     // We can make use of Test.loadData method instead of inserting test record in the test class - that can reduce the no of lines of code for the test class.
  7.     //Created Date Field will not be Read only field when we use Test.loadData method .
  8.             final String staticResourceName = 'TestLeadCreatedDate';
  9.             final Lead[] testAccounts = (Lead[]) Test.loadData(Lead.sObjectType, staticResourceName);
  10.      }
  11.        
  12.     }

Static Resource File - TestLeadCreatedDate.csv
LastName,Company,CreatedDate
LastName1,Company1,2013-11-08T23:00:01.235-07:00
LastName2,Company2,2013-11-07T10:00:01.235-07:00
LastName3,Company3,2013-11-06T08:00:01.235-07:00
LastName4,Company4,2013-11-05T18:00:01.235-07:00

Saturday, 5 October 2013

My First Movie Review

Hi Everyone,

Its nice to share something about the movie which have seen today " Raja Rani"

I have never gone to movie alone in my life time till i write this post. I usually go to movie with "My Dad " or with my friends/colleagues . I will never like go to movie alone , But this weekends made to do this , All of colleagues went to home this weekend and Its shut down too .

So thought of moving to movie alone to have some fun in the very frustated development life .

I used to tell to myself , its not a good practice of doing this two things  alone 1) Going to movie 2) Had a drink . From my stands points this things are just to have fun for sometimes .

Let me share the things about the movie  "Raja Rani" .

Its simply Awesome .

The theme of the movie is " There is life(love) after the love failure " .

All the characters in the movie did their job nicely and clearly what it meant for .
Especially  SatyaRaj character showed us how a father should be .
Asusual Sathanam comedy is extremely great.

GV Prakash's BGM  is brilliant asset and made the movie enjoyable at every single clip . I am proud to be your fan GV.
Director Atlee has projected his view in very good way . He has shown his talent and potential in his first movie and got a fame . Director shankar can tell atlee is my assistant for a while.

Young team's victory is very much inspirational  to me . I am very impressed with the young team's work especially GV and Atlee. Both of them are just around 25 and Did a great job and attracted everyone .  Kudos both of you .

Kindly watch this movie only on theatres.

Forgive me if i said anything wrongly in the above post and also if it hurts you .

With Love
Lovable Mariappan

Thursday, 19 September 2013

Rollup on Self Relationship in Salesforce

Hi all,

The requirement here is like "Need to roll up the things in child records to parent record" - Self Relationship
When I started to write the code, I feel quite easy to achieve this task, Later I came to know that the code I wrote would work only on single record.
I didn't find the way of bulkily the code for a while. Since the case is like parent record will have many children and the trigger is supposed to handle many parent records which will be having many child records.
At later point of time, I came across the idea of having Map (collection) to process this sort of complexity.

Let me share the code here:
  1. trigger AccoutApplications on Account ( after update)
  2. {
  3.  Set<Id> accids=new Set<Id>();
  4.  List<Account> acctoupdate=new List<Account>();
  5.  Map<Id , List<Account>> AccountMap=new Map<Id , List<Account>>();
  6.  For(Account aa : Trigger.New)
  7.  {
  8.  Account oldacc= Trigger.oldMap.get(aa.Id);
  9.  Account newacc= Trigger.newMap.get(aa.Id);
  10. // Get the parent records whose one of the child records being updated
  11.  if( aa.parentId!=null )
  12.  {
  13.  accids.add(aa.parentId);
  14.  }
  15.  }
  16.  try
  17.  {
  18.  // Query the child’s records which are being updated along with all the child’s of the parents record to which it is associated
  19.  List<Account> Accountts = [ select id,Standard_Application__c,Custom_Application__c,parentId from Account where ParentId IN :accids];
  20.  // Map collection ( Id , List of records )
  21. // Parent Record Id as key and List of child records as the value
  22. For(Account aa: Accountts)
  23. {
  24.   if(AccountMap.containsKey(aa.parentId))
  25.   {
  26.   List<Account> tempList = AccountMap.get(aa.parentId) ;
  27.   tempList.add(aa);
  28.   AccountMap.put(aa.parentId , tempList) ;
  29.   }
  30.   else
  31.   {
  32.   AccountMap.put(aa.parentId , new List<Account>{aa}) ;
  33.   }
  34.   }
  35. ///Processing
  36. // Pulling out the all the child records of the parent and update the parent record ,and iterate over all the parent records which is having mutiple child records
  37.   For(Id ii:accids)
  38.   {
  39.   if(userCaseMap.containsKey(ii))
  40.   {
  41.   Decimal SA=0;
  42.   Decimal CA=0;
  43.   List<Account> allaccountofid = AccountMap.get(ii) ;
  44.   For(Account allacc:allaccountofid)
  45.   {
  46.   SA+=allacc.Standard_Application__c;
  47.   CA+=allacc.Custom_Application__c;
  48.   }
  49.   Account aid=new Account(id=ii,Standard_Application_Summary__c=SA,Custom_Application_Summary__c=CA);
  50.   acctoupdate.add(aid);
  51.   }
  52.   }
  53. }
  54.  Catch(Exception e){ }
  55.  update acctoupdate;
  56. }

Related Query Size


Hi All,

I had a requirement today which should count the number of records on the related list on the parent record. Initially I thought of having custom field and update that based on updating of child records. Pulling the parent record and increment/decrement the custom field in it.
Actually it need lot of code if i ran with that way of doing the requirement. So, I tried different way of achieving this task.

Here is the code I used for this.

  1. trigger ActiveStudentonOpportunity on Account ( after update) {
  2. Set<Id> oppid= new Set<Id>();
  3. List<Opportunity> opportunitylist=new List<Opportunity>();
  4. For(Account acc:Trigger.New)
  5. {
  6. // Pulling the records which is having opportunity lookup field
  7. if( acc.Opportunity__c!=null )
  8. {
  9. oppid.add(acc.Opportunity__c);
  10. }
  11. }
  12. // Query the opportunity records for which we are counting the no of records in the related list on child record updation
  13. Map<id,Opportunity> oppMap = new Map<id,Opportunity>([select id,Actual_Student_Accounts__c from Opportunity where id IN :oppid]);
  14. List<Opportunity> oo=[Select id ,(Select id from Accounts1__r )from Opportunity where id in:oppid];
  15. For(Opportunity opp:oo)
  16. {
  17. oppmap.get(opp.id).Actual_Student_Accounts__c=opp.Accounts1__r.size();
  18. opportunitylist.add(oppmap.get(opp.id));
  19. }
  20. update opportunitylist;
  21.  
  22. }

Sunday, 11 August 2013

My First Post

Hi All,

Its really curious to write something in blog and I want to make this as habit .

I would also like to share something like newly learned things and so .