Wednesday, 18 February 2015

How to overide standard salesforce save button?

Hi guys!

Yesterday, I was working on overiding the standard save and cancel button. I know there are many blogs you ll find easily on google.

Anyways, am just sharing just to add a simple way(I think , u may find more easier  :P:P) of overiding . Lets jump to scenario.

Scenario:-  Say, I have a custom visualforce page abc and from there I have given a button on that page. When that button is pressed, it should take us to standard edit page of account. After clicking the save button it should take us to custom visualforce page. lets say to xyz page.



So , you guys are aware of  URL parameters that are appended when you open standard detail page of account inedit mode it is like :-

https://na15.salesforce.com/001/e?retURL=%2F001%2Fo

So, what happen is after you fill the details. and hit save button. it takes you to the record itself . It is the standard salesforce  functionality.

Lets see what these parameters are meant for:-

1. 001 - it is id of account object.
2. 001/e?-  It stands for edit mode .
3. retURL - It is the url when you click the cancel button
4. saveURL - It is the URL for landing page.

So, my requirement is that I have to redirect from my custom visulforce page to account edit page.
to do this - in your page refrnce method append this

pagereference pr= new pagereference('/001/e?retURL=/apex/AccountSearch&saveURL=/apex/CreateContac').setRedirect(true);

Thanks
Ankush






Sunday, 1 February 2015

How salesforce matches Contact to the case when an email is received in Email-To-Case

Hi Guys,

I have been working on salesforce's Email to case Functionality since December. I cant say am perfect in it , but have got a good hold as of now.

Let me just walk you through how salesforce Email-To-Case functionality works. Though , ll not brief here about setting email to case .

So, when an customer sends an email to salesforce org. Then a case is created in the salesforce.

Let say, You have a contact  with a name xyz and his email address is xyz@abc.com. So, when he ll send an email to raise a case, his case will be created and and his contact will be asscoiciatd  with this case.

And how salesforce does this, all explationaltion is given here in salesforce know;ledge article.

CLICK HERE.


Thursday, 15 January 2015

Trigger on Parent Object to copy the fields on its child record in Salesforce!!

Hi Guys!!

Its been long time since I have been here. My obsession with salesforce has taken next level.

Still, I love writing triggers. I know all these are very small triggers. But it may help beginners who are trying to connect to salesforce.

Anyways, lets start with another trigger(Only for beginners) .

Scenario:-

I have to replicate a field of parent object to all its child records. It is very simple scenario for experience people.

Here is the code for you folks....

Trigger replicateFieldsOnChild on Parent (After update){
 Set<id> setOfParentId = new Set<Id>();
 for(Parent pt : trigger.new){
  setOfParentId.add(pt.id);
 }
 List<Child> listChild = new List<Child>([Select id, parentId from child where ParentId in: setOfParentId]);
 List<Child> updatedlistChild = new List<Child>
 for(Parent pt : trigger.new){
  for(Child ch : listChild){
   ch.field = pt.field;
   updatedlistChild.add(ch);
  }
 }
 Update updatedlistChild;
}


Remember :-  There are may ways of doing a job . You may find more simpler and efficient way. keep Exloring!


Happy coding 

Cheers
Ankush
ankushsalesforce@gmail.com

















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










Thursday, 23 October 2014

Trigger On Attachment Object Salesforce!!

Hi Guys!!

Hope you doing well.

One of my blog reader asked me to share code of trigger on Attachment object . Although , I have already written a trigger on attachment object. For the same click here. but this time I have got a different scenario.

Caution Notice :P :- This blog is meant for beginners like me, who are still learning  salesforce or must say it is for those who are loving salesforce.

Scenario:-  When an attachment is inserted under an account or a contact or Lead  etc.Now, on the basis of a custom field on parent of attachment object, it should check whether any other record exist with same value of field . If there exist some record then this lead should get deleted and and the attachment should be attached to older one. 

So , lets begin with it . Lets Say , I have acustom field on Lead object name as My_name__c on lead objec. On the basis of this field I ll check whether there exist some records or not. so lets Jump to code .




trigger DeleteLeadIFNewMatchesOld on Attachment (Before insert) {
    Set<Id> setOfNewLead = new Set<Id>();
    Set<String> setOfMyname = new Set<String>();
    Set<Id> setOfDeleteLeads = new Set<Id>();
    for(attachment a : trigger.new ){
        setOfNewLead.add(a.ParentId);
    }
    Map<Id,lead> mapOfNewLead = new Map<Id,Lead>([Select id , My_name__c from Lead where id in :  setOfNewLead]);
    Map<Id,Lead> mapOfOldLead = new Map<Id,Lead>([Select id , My_name__c from Lead where  id not in :  setOfNewLead]); 
//    Map<Id,String> = new Map<Id,String>();
    System.debug(mapOfOldLead);
     //if(mapOfNewLead.get(a.ParentId).My_name__c == mapOfOldLead.get())
        for(Lead ld : mapOfNewLead.values()){
           //System.debug('myname'+myname);
            System.debug('mapOfNewLead.values()'+mapOfNewLead.get(ld.Id).My_name__c);
            String myname = mapOfNewLead.get(ld.Id).My_name__c;
            setOfMyname.add(myname);
        }
        for(attachment a : trigger.new ){
            System.debug(setOfMyname);
            System.debug('==19'+mapOfNewLead.get(a.ParentId).My_name__c);
                System.debug('===');
                //Id testId = [Select Id from Lead where My_name__c in]
                for(Id getIdMap : mapOfOldLead.keySet()){
                    if(setOfMyname.contains(mapOfOldLead.get(getIdMap).My_name__c)){
                        setOfDeleteLeads.add(a.ParentId);
                        a.ParentId = getIdMap;
                }

            }
        }
        List<Lead> deleteList = [Select id from Lead where Id in : setOfDeleteLeads];
        delete deleteList;
}




try this Code and then let me know how can I improve this code . I have made it quite complex. 
ThanksAnkush 

Cheers
ankushsalesforce@gmail.com


Please give a look at  my awesome friends Blog piyush Blog

























My AI Channel

Hi Folks,  Its been a while posted any post on Salesforce. I feel like Salesforce can waith but new AI hype cant, need to go with the flow. ...