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!!

Monday, August 27, 2018

How To Debug The Database.getQueryLocator(query)

If you Need to debug The Data in Database.getQueryLocator(query) use Bellow Sample Code to Debug the Data.
The Return Type Of The Database.getQueryLocator(query) is Database.QueryLocator.
Open Anonymous Window in Developer Console Copy and Paste Bellow Code.

Sample Code

String strQuery;
strQuery = 'Select Id.Name,Industry From Account limit 10';
Database.QueryLocator qu = Database.getQueryLocator(strQuery);
// Get an iterator
Database.QueryLocatorIterator it = qu.iterator();
// Iterate the records
while(it.hasNext()) {
    Account a = (Account)it.next();
    System.debug('====Account Values===='+a);
}

Result:
Let us Know If You Have any Queries.
Happy Learning !!!

Sunday, August 26, 2018

Dynamically Add Row / Remove Row in PageBlock Table in Visualforce Page

In this Post, Explore More about the How To Add Row or Remove Row in PageBlockTable in Visualforce Page

To achieve this Functionality I Created The One Simple Apex Controller and Visualforce Page.

Apex Class
public class AddOrRemoveRowController {
    private Integer counter = 0;
    public list<WrapperData> lstWrapperData {get;set;}
    
    public AddOrRemoveRowController() {
        lstWrapperData =  new list<WrapperData>();
        addRow();
    }
    
    // Adding Rows 
    public PageReference addRow() {
        counter++;
        lstWrapperData.add(new WrapperData(new Account(), counter));
        return null;
    }
    
    // Removing The Rows
    public PageReference removeRow() {
        Integer iRemoveRow = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
        for(Integer i=0; i<lstWrapperData.size(); i++) {
            if(lstWrapperData[i].iIndex == iRemoveRow ) {
                lstWrapperData.remove(i);     
            }
        }
        return null;
    }
    
    // Wrapper Class
    public class WrapperData {
        public Account objAcc {get;set;}
        public Integer iIndex {get;set;}
        
        public WrapperData(Account objAcc, Integer iRow) {
            this.objAcc = objAcc;
            this.iIndex = iRow;
        }
    }
}
Visualforce Page
<apex:page controller="AddOrRemoveRowController" tabStyle="Account">
    <apex:form >
        <apex:pageBlock >
            <apex:outputPanel id="showpageBlockData" >
                <apex:pageBlockSection columns="1" title="Dynamically Add or Remove Row" collapsible="false">
                    <apex:pageBlockTable value="{!lstWrapperData}" var="wd" id="pgb" >
                        <apex:column headerValue="Account Name">
                            <apex:inputField value="{!wd.objAcc.Name}" />
                        </apex:column>
                        <apex:column headerValue="Industry">
                            <apex:inputField value="{!wd.objAcc.Industry}" />
                        </apex:column>
                        <apex:column headerValue="Phone">
                            <apex:inputField value="{!wd.objAcc.Phone}" />
                        </apex:column>
                        <apex:column headerValue="Account Type">
                            <apex:inputField value="{!wd.objAcc.Type}" />
                        </apex:column>
                        <apex:column > 
                            <apex:commandLink value="Add Row" action="{!addRow}" reRender="pgb" style="color:#61afe8;" />&nbsp;&nbsp;||&nbsp;&nbsp;
                            <apex:commandLink value="Remove Row" action="{!removeRow}" reRender="pgb" style="color:red;" >
                                <apex:param value="{!wd.iIndex}" name="index" />
                            </apex:commandLink>
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>           
            </apex:outputPanel> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Happy Learning!!!

Saturday, August 11, 2018

Enable Custom List Settings In Org

From  Spring '18 release, By Default Custom List Settings Are Greyed Out,
If You Are Trying to create List Type No way to select List Type By Default Is Shows Only Hierarchy Type.


To Enable List Type Custom Settings, In Your Org
Path:

 Setup > Schema Settings > Manage List Custom Settings Type

Enable Manage List Custom Settings Type.



Once You Enabled The Toggle you are able to create list type Custom Settings in Your Org.



Let us know if you have any queries!

Happy Learning!!!


Monday, August 6, 2018

How To Check Active Validation Rules Based On Selected Object Using SOQL

If You want to Check Active Validation Rules on selected Object.
Use Below Simple SOQL Query, This Query Fetch the Active Validation Rule on a Your Select-Object.
SOQL Query:
Select id,Active,Description,EntityDefinition.DeveloperName,ErrorDisplayField,ErrorMessage From ValidationRule where EntityDefinition.DeveloperName='<Object Name>' and active = true
Note: Use Tooling API while Querying.
If You are Using Developer Console

Check the Screen Shot For More Information


Let Us Know If  You Have Any Queries.

Happy Learning!!