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.

Friday 8 September 2017

System.FinalException: SObject row does not allow errors

Arpit Sharma
During Development various type of exception occurred, One of the exception that I have faced is System.FinalException: SObject row does not allow errors. In this tutorial we will discuss reason of this exception.

Points that need to be considered

Where Occurs - This exception occurs during execution of trigger.

Reason of Occurrence - Salesforce provide addError method to mark a record with custom message and prevent any DML operation from occurring.

But this method is applicable only on records that is part of trigger context variable means Trigger.old and Trigger.new. It means We can use this method only in triggers and only on Trigger.old and Trigger.new context variable.

So Let's take an example if we have trigger on account object and we want to stop processing of particular record on particular condition then we can use addError method.

Here is example 

Correct Example
trigger AccountTrigger on Account(before update) {
    for(Account acc : Trigger.new){
        if(acc.Name == 'Working Code'){
            acc.addError('This add error working fine.');
        }
    }
}

Incorrect Example
trigger AccountTrigger on Account(before update) {
    List<Account> accList = [SELECT Id,Name 
                             FROM Account 
                             WHERE Id IN :Trigger.old];
    for(Account acc : accList){
         if(acc.Name == 'Non Working Code'){
             acc.addError('This add error statment will not execute trigger will throw exception i.e.System.FinalException: SObject row does not allow errors');
         }  
    }
}

In First Example we are using addError method on record of trigger context variable but in second case we first reterive record from database then add error on database record which is not allowed in salesforce and salesforce throws error System.FinalException:SObject row does not allow errors.


Tuesday 5 September 2017

How To Stop Deletion Of Parent Record If Child Record Is Exist in salesforce Using Configuration

Arpit Sharma
In Salesforce if we want to create one to many relationship(one parent record have many child) then one way is to we can use lookup relationship feature of salesforce.

In case of lookup relationship If we want to stop deletion of parent record then we don't need to worry about it, We can configure it from look relationship configuration page.

We can configure it during lookup field creation and edit.

Salesforce provide us two option during lookup field configuration, we need to select second option to stop deletion of parent.

1. During parent record deletion set child lookup field value to blank.
2. During parent deletion stop deletion of parent.

But we can't select first option if we set lookup field to required on child object
because in that case we can't set field to blank because field is required on child record. You can see option in screenshot.



   
If we try to delete parent record then salesforce gives following error. 

Your attempt to delete Parent Record could not be completed because it is associated with the following Child Records. If the following table is empty, it is because you do not have access to the records restricting the delete.

So for deletion of parent record we need to delete all child record first.