Saturday, 30 September 2017

VisualForce - loadOnReady attribute on apex:includeScript

Arpit Sharma


Salesforce provide us apex:includeScript tag to include script on visual force page.


In this post we will discuss about loadOnReady attribute of apex:includeScript tag.

Role of loadOnReady attribute in apex:includeScript - 

1. loadOnReady attribute have two value i.e false and true.

2. Default value of this attribute is false.

3. If value of this attribute is false then script load immediately.

4. If value of this attribute is true then script will load when page is ready.


Let us take an example to understand loadOnReady attribute

1. When loadOnReady attribute is false then we get angular at both place outside of onload and inside the on onload.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<apex:page >
  <!-- External Scripts -->
      <apex:includeScript loadOnReady="false" value="{!$Resource.angular_js_script}" />
  <!-- External Scripts -->
  <script>
      alert(typeof angular);//we will get angular here.
      window.onload = function(){
          alert(typeof angular);// we will get angular here.
      }
  </script>
  
</apex:page>

2. When loadOnReady attribute is true then we get angular only in inside the on load.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<apex:page >
  <!-- External Scripts -->
      <apex:includeScript loadOnReady="false" value="{!$Resource.angular_js_script}" />
  <!-- External Scripts -->
  <script>
      alert(typeof angular);//we will get undefined here means angular is not loaded here.
      window.onload = function(){
          alert(typeof angular);// we will get angular here.
      }
  </script>
  
</apex:page>

ReferencesSalesforce docs

Saturday, 23 September 2017

A useful String method : normalizeSpace

Arpit Sharma


Salesforce release a very good method of string which is required in string manipulation during development i.e normalizeSpace().

Functionality of normalizeSpace Method - This method normalizes the following white space characters: space, tab, new line, carriage return, and form feed.

Remove leading white space characters, trailing white space characters, and repeating white space characters from string.

Example of normalizeSpace Method -

1
2
String unnormalizeString = ' Salesforce       \t  \n   \r  \f Normalize Example ';
System.assertEquals('Salesforce Normalize Example',unnormalizeString.normalizeSpace());//return true

Explanation of normalizeSpace Example 

1. In above example unnormalizeString string variable contains space on starting, space on end, multiple spaces in between and spacial character (tab (\t), new line (\n), carriage return (\r), and form feed (\f)).

2. In second line we have applied normalizeSpace() method on unnormalizeString string to remove all spaces and special character, so over statment will successfully passed without failing assert statement.It means string is normalized completely

So this is all about string normalization method.

Let me know if you have any suggestion, doubt, question.




Tuesday, 19 September 2017

Can We Set The Owner Of Record To User Who Does Not Have Object Permission?

Arpit Sharma


In this tutorial we will see, can we set the owner of a record to a user who does not have object permission?

Salesforce provide us a feature to change owner of record but one question come in my mind, can we change owner if user does not have object permission?

So I try below example

1. Created two user u1 and u2.
2. Created one object TestOwner.
3. Given all permission to profile of u1 for TestOwner object.
4. Removed all permission from profile of u2 for TestOwner object.
5. Login to user u1 and created record of TestOwner.
6. Now try to set owner of record to u2.
7. Salesforce stop me change the owner of record to u2 and display message i.e new owner of the record must have at least “Read” permission on TestOwner.

So to change owner of record, new user must have at least read permission on profile for object of that record.

Sunday, 10 September 2017

How To Stop Deletion Of Parent Record If Child Record Exist In Salesforce Using Triggers.

Arpit Sharma
In this tutorial we will see how we can stop deletion of parent record basis on condition. As we have already learn Simple Example i.e Stop deletion of parent record using configuration.

Now let's come on this, Suppose we have a scenario in which we need to stop deletion of parent record when child record exist basis on condition either on parent or child, we can handle this scenario by using trigger.


Now Let's take on example 

Suppose we have Account Object and Contact object and if we want to stop deletion of parent account record if any child contact record name starts with "Test".

We can handle this scenario using trigger having before delete event

Below is sample code snippet of above scenario.

trigger AccountTrigger on Account (before delete) {
    //reterieve all contacts releated account that is currently deleting
    Map<Id,Account> accountWithContacts = new Map<Id,Account>([SELECT 
            ID,(SELECT ID,Name FROM Contacts) 
            FROM Account 
            WHERE ID IN:Trigger.old]);
    //iterate over trigger.old context variable record        
    for(Account accRecord : Trigger.old){
       //reterive contact records associate with account
       for(Contact accContact :accountWithContacts.get(accRecord.ID).contacts){
           //if contact start with Test then stop deletion of account record
           if(accContact.Name.StartsWith('Test')){
               accRecord.addError('You cannot delete this account because contact associate with this account have name test.');
           }
       }
    }  
}

Guys share this tutorial if you like it.