Showing posts with label Salesfroce. Show all posts
Showing posts with label Salesfroce. Show all posts

Sunday, December 30, 2018

Use of Inherited Sharing in Apex Class


This post explains the use of inherited sharing in apex class
If you specify inherited sharing Keyword on an Apex class,  which allows the class to run in the sharing mode of the class that called it. Using inherited sharing enables you to pass security review and ensure that your privileged Apex code is not used in unexpected or insecure ways.

  1. An Apex class with inherited sharing runs as with sharing when used as a Visualforce page controller, Apex REST service, or an entry point to an Apex transaction.
  2. An Apex class with Inherited Sharing is being called from some other class which is having without sharing setting, then it will run in without sharing mode.
Example
This example declares an Apex class with inherited sharing and a Visualforce invocation of that Apex code. Because of the inherited sharing declaration, only opportunities for which the running user has sharing access are displayed. If the declaration is omitted, even opportunities that the user has no rights to view are displayed due to the insecure default behavior of omitting the declaration.
Apex Class
public inherited sharing class OpportunityMethods {
    public list<Opportunity> getAllTheOpps(){
        return [SELECT Id, Name, StageName FROM Opportunity];
    }
}
Visualforce page
<apex:page controller="OpportunityMethods">
    <apex:repeat value="{!getAllTheOpps}" var="record">
        {!record.Name}
    </apex:repeat>
</apex:page>

Happy Learning!!

Tuesday, August 28, 2018

How to get the Failure Record Ids From Database.SaveResult[]

if You Need To check The Success Record Ids and Failure Record Ids.
If DML Operation is Success Will Get Record Id From Database By Using getId()  Method in Database.SaveResult Class.
If any DML Operation Fails while Processing The Record no way to Get the Failure Record Id in
Database.Error Class

To Get the Failure Record Id, We can use List index Property to get the Id of the failed record.

Check Bellow code
list<Account> lstAccToUpdate = new list<Account>();
list<Account> lstAccounts = [Select id, Name, Industry From Account limit 10];
for(Account aIterator : lstAccounts) {
    aIterator.Industry = 'Banking';
    lstAccToUpdate.add(aIterator);
}
if(lstAccToUpdate != null && !lstAccToUpdate.isEmpty()) {
    Database.SaveResult[] result = Database.update(lstAccToUpdate,false);
    for(Integer i=0; i < result.size(); i++) {
        if(result.get(i).isSuccess()) {
            System.debug('Records are updated Successfully');
        } 
        else if(!result.get(i).isSuccess()) {
            Database.Error errors =  result.get(i).getErrors().get(0);
            System.debug('Error Occurs While Processing The Record'+errors.getMessage());
            System.debug('Failure Record Ids While Updating'+result.get(i).Id);
        }
    }
}

Let us know if you have any queries.
Happy Learning!!