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.




Wednesday, 16 August 2017

Identify Current User Access To Record Using UserRecordAccess Object

Arpit Sharma
Salesforce provide UserRecordAccess object to identify access of user on set of records. We can query on this object based on user ID.

Key Points of UserRecordAccess
    1. This object is read only.
    2. It is available in API Version 24.0 and later.

Fields of UserRecordAccess 
    • RecordId - Id of record in which we need to check access, It is available in SELECT clause and WHERE clause. 
    • UserId - It is available in WHERE clause.
    • User Record Access Id - It is available in WHERE clause.
    • Maximum Access Level - It is used to identify maximum access of record having user(Picklist type). Value available in picklist.
        • None
        • Read
        • Edit
        • Delete
        • Transfer
        • All
    • HasTransferAccess(Boolean Type)
    • HasEditAccess(Boolean Type)
    • HasDeleteAccess(Boolean Type)
    • HasAllAccess(Boolean Type)

Sample Query
SELECT RecordId, HasEditAccess,HasReadAccess, HasTransferAccess, MaxAccessLevel,HasDeleteAccess
     ,HasAllAccess FROM UserRecordAccess
     WHERE UserId = UserInfo.getUserId()
     AND RecordId = :recordId     

References
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_userrecordaccess.htm
https://help.salesforce.com/articleView?id=000181207&type=1



  

Friday, 11 August 2017

Reports In Salesforce

Arpit Sharma
Why Report


Report is usually used to represent data in presentable form, Creating a report manually required lot of effort which included many question in mind i.e which type of data you required, what is the best way to represent data in report.

If you need data based on some criteria then there may be chances some unwanted data visible in report or some important data is not visible because of some human error. Report generated become more complicated when you work on large data. This is point where you need some automatic process to generate report for you business.

We will discuss here, about reports in Salesforce

Reports in Salesforce

Various tools are available in market to generate reports but if you are working in Salesforce then either you need to write custom code for represent that data or Salesforce itself provide most advance way to generate report by using just some point and click that does not require knowledge of code and reduce efforts and time

What Salesforce provide 
  1. We can set schedule time of report generation.
  2. Salesforce display data to user in report according to security model means user can't see data if user does not have permission of that data.
  3. In salesforce we can generate four type of reports
    • Tabular Reports
    • Summary Reports
    • Matrix Reports
    • Joined Reports 
Guys all above is basic idea about what is report, how it is useful for us and what Salesforce provide to us.

We will discuss above report types in details in upcoming tutorials

Feel free to comment if you have any question and suggestions in this tutorial.