Sunday 30 November 2014

Accessing parent object field in trigger Salesforce!!

Hi Guys!

It is Saturday out here and am sharing a few rodablocks you may face while executing your code. Beware , if you are new to salesforce apex like me. It can take days and hours, to solve the issue which I will discuss with you.

Some good readers may be aware of it already. Ok

Issue:- I was working in my office and doing debugging of a trigger which someone wrote a year back I guess. But I got an scenario that this trigger is not working properly means it is doing 95 5 of its work. But rest 5 % was the bug.

So lemme explain you the scenario. Say, I have two two custom object A  and B . Also I have a USER object.

Lets go with the rlation ship User is the parent of 'A' object. In turn 'A' is the parent of 'B' object.
As we know that there is a field called Manager on User object which is lookup field to user. So, whenever a record of object'A' is created then the manager of parent A should be replicated to a field Manager__B on Custom Object'B'. Obviously we have trigger on Custom Object'A' . But catch here is you cannnot use the value of a parents fields directly. you need to querry that field. Lemme share you with the codesinppet.


for(custom__A res: [Select Id,  custom__A__User__r.ManagerId from custom__A where Id in:resIds ]){
                custom__B mem = resourceToMemberMap.get(res.Id);
                mem.OwnerId = res.OwnerId;
                mem.Line_Manager__c = res.custom__A__User__r.ManagerId;                

You cannot  use the value res.custom__A__User__r.ManagerId untill this field is being used in query.
So, you need to query this field. Otherwise you ll get mem.Linemanager = null always.

Credit for this learning goes to my colleques and to the Senior developer who passed on this piece of knowledge to me .

Cheers
happy coding!

Thanks
Ankush












Thursday 27 November 2014

Upoading ChangeSet of Email templates and letter heads in salesforce!!

Hi Guys!!

Salesforce sometimes comes up  with very interesting scenarios . I don t know why it happens . but it should not never happen . Though it is bug or not . Sorry , You must be thinking what the hell am talking about . Ok lemme tell you.


If you are a administrator and working to create a change sets from one org to another org which are connected to each other. In that change sets you are about to send the email templates that you created that are HTML templates . then BEWARE!!!


Guys!
I ll share what all happened with me. Some of  you might not be surprised to know .

I got an issue while uploading a change set which contains two email templates . In the org in which I have created the email template i. is a HTML Email template with Letter Heads.

First, When I created a change set it just contained the Email templates without Letterheads. I uploaded it to a connected org which  does not have its associated Letter heads. I found that there is no letter heads found . Which is the obvious  case.  Also ,I didn get the letter heads field available.


Second, I uploaded another Change set to the connected org  which contains only letter heads which were associated with the Email templates which which I uploaded in last change set. Still I cant see letter heads field on email template I uploaded.  Although , Letter heads has been deployed. to this connected org. Also Letter heads was made available by checking the check box.


Third , When I uploaded the another change set with Email Templates and letter heads in one change set.  It is then Only I get Option to ad letter heads.


So, Make sure you upload theses components in same change set.

Thanks
Ankush

Sunday 23 November 2014

Simple Script for Calculating bumber of childs in Salesforce!

Hi Guys!!

Its been long time.  This time I am with very simple scenario. Must say salesforce is expanding day by day. Its really awesome, but its becoming very difficult to keep running salesforce, so many new features are being released with every release.

Any ways lets get back to buisness.

Scenario:- Say, I have many accounts and Contacts in my Org. And I simply wants to calculate the number of Contacts that are related to one account.

Solution:-

It is very easy . Just take a copy and a pen, and lay down the idea and think before writting the code.
prerequisite :- Get familiar with Workbench. 

In workbench go to the apex execute.  section , which is under the Utilities.



Map<id,List<Contact>> mapit = new Map<id,List<Contact>>();
for(Contact con : [Select id, AccountID from Contact]){
 If(Con.AccountId != null){
    mapit.put(con.AccountId, new List<Contact>([Select id from Contact where AccountId = : Con.AccountID]));
}
}
System.debug('=='+Mapit.keySet().size());
For(Account acc : [Select id from Account where id in :Mapit.keySet()]){
     system.debug('size='+mapit.get(acc.id).size());
    acc.NumberofLocations__c = mapit.get(acc.id).size();
}


NumberofLocations__c is a number field in account. 

Happy Coding
Ankush 

ankushsalesforce@gmail.com










oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...