Monday, April 22, 2013

Access all the fields of a related object instantly after insert.

Hi guys, few days back while writing test code for my class I found a weird problem with an awesome solution.

The scenario was, I have inserted an Account 'My Company' and on next line inserting Contact associating it with this Account 'My Company' by just providing an Account id of an Account 'My Company'.

Now, I was expecting when I debug an Contact.Account.Name I will get 'My Company' but was surprised to see 'Null' because, I have already linked my Contact to an Account. I was getting Account Id of that Account in debug then why I can't access Account Name or any other Account field except Id.

To check this out, just paste the below code in your developer console:

 Account objAccount = new Account( Name = 'My Company',  
                                   Website = 'www.mycompany.com'  
                                   );  
 insert objAccount;
   Contact objContact = new Contact( FirstName = 'Praful',                                    LastName = 'Gadge',                                    AccountId = objAccount.Id                                    );   insert objContact;
    //objContact.Account = objAccount;
   system.debug('----objContact.AccountId----'+objContact.AccountId);   system.debug('----objAccount.Name----'+objAccount.Name);   system.debug('----objContact.Account.Name----'+objContact.Account.Name); 

After digging for many hours we got an answer that we have only assigned AccountId to an Contact.AccountId but all other fields of that Account are not yet connected/related to Contact. To connect all fields of a Contact to the Account we can directly associate an object to the related object, like:

objContact.Account = objAccount;

Now we can access all the fields of an Account. To check this uncomment a commented line in the above code.

It means we can associate a whole object to a related object to access all its fields instantly after an insert DML.

Sunday, March 31, 2013

Remove buttons from Rich Text Area

Remove Buttons from Rich text area

We can find so many blogs to add buttons in Rich Text Area, but when I googled for removing particular button from Rich Text Area I found that there is no one who had posted or knows this.

So here it comes, the solution:
  • Basically I am going to use is JavaScript
  • Using this I am gonna hide those button elements from RTA
  • As you can see the JS, I am calling the function onLoad and searching for element 'cke_9'. It's nothing  but the HTML element Id of button
  • If on load also we gets the element as null, we will call this function after 10 Seconds until we find the button/RTA
  • Then, once we get that button we can make particular button invisible.

Include this JavaScript in your Visualforce page at bottom:
  window.onload=function(){  
   setTimeout(  
     function removeToolbar(){  
       if(document.getElementById('cke_9') != null)  
       {  
         //hideEle('cke_6'); //Undo and Redo  
         hideEle('cke_9'); //Bold, Italic, Underline, StrikeThrough  
         hideEle('cke_14'); //Link, Image  
         hideEle('cke_17'); //Allign Buttons(Left, Center, Right)  
         hideEle('cke_21'); //Bullet and indent buttons  
       }  
       else  
         setTimeout(removeToolbar, 1000);  
     }  
   , 1000);  
 };  
 function hideEle(eleId)  
 {  
      if(document.getElementById(eleId) != null )  
          document.getElementById(eleId).style.display = 'none';  
 }  




  • Someone must be confused about how to get button Ids?
    • If we look at attached screenshot, can easily identify the Id of button we want to remove.
    • Basically there are 5 group of buttons, each group having an distinct Id.
    • If you want to remove particular button just add its number to its group Id.
    • For example:
      • To remove only "Underline" button from second group:
      • We first take its group Id = "cke_9" and then its number in a group 3, so 9 + 3 = 12 so,
        we will pass the "cke_12" to hideEle function



Guys, let me know your feedback on this. As it was much appreciated by end user who were thinking this ain't possible.