Search This Blog

Tuesday, July 14, 2015

RollUpSummary Exampe Trigger

In the sample code below, we are updating sum field(custom field) of account with sum of opportunities(amount field of the opportunity is to be sumed) related to that particular account

Sample Trigger Code :
trigger RollUpExample on opportunity (after insert,after update,after delete) {
   if(trigger.isInsert) {
    Set<id> accountIdSet = new Set<id>();
    Map<object,decimal> accountWithOppSumMap = new Map<object,decimal>();
    List<account> accountList = new List<account>();
      for (opportunity opp: Trigger.new)
        {
            if(opp.accountid!= null)
            {
                accountIdSet.add(opp.accountid);
                system.debug('ksk'+accountIdSet);
            }
        }
    AggregateResult[] groupedResults  = [SELECT SUM(amount) aver FROM opportunity where accountid in :accountIdSet];

    for (AggregateResult ar : groupedResults) {
        accountWithOppSumMap.put(accountIdSet,Integer.valueOf(ar.get('aver')));
        system.debug('ksk'+accountWithOppSumMap);
    }
    for(account acc :[select id,sum__c from account where id in :accountIdSet]) {
        acc.sum__c = string.valueOf(accountWithOppSumMap.get(accountIdSet));
        accountList.add(acc);
    }
    update accountList;
   }
 
    if(trigger.isUpdate) {
    Set<id> accountIdSet = new Set<id>();
    Map<object,integer> accountWithOppSumMap = new Map<object,integer>();
    List<account> accountList  = new List<account>();
    for (opportunity opp: Trigger.new) {
        if(opp.accountid != null) {
            accountIdSet.add(opp.accountid);
        }
    }
    AggregateResult[] groupedResults  = [SELECT SUM(amount )aver FROM opportunity where accountid in :accountIdSet];

    for (AggregateResult ar : groupedResults) {
        accountWithOppSumMap.put(accountIdSet,Integer.valueOf(ar.get('aver')));
        system.debug('ksk'+accountWithOppSumMap);
    }
    for(account acc  :[select id,sum__c from account where id in :accountIdSet]) {
        acc.sum__c= string.valueof(accountWithOppSumMap.get(accountIdSet));
        accountList.add(acc);
    }
     update accountList;
    }
 
    if(trigger.isDelete) {
        Set<id> accountIdSet = new Set<id>();
        Map<object,decimal> accountWithOppSumMap = new Map<object,decimal>();
        List<account> accountList = new List<account>();
        for (opportunity opp: Trigger.old) {
            if(opp.accountid != null)
            {
                accountIdSet.add(opp.accountid);
               // system.debug('ksk'+accountIdSet);
            }
        }
        AggregateResult[] groupedResultaccountIdSet  = [SELECT SUM(amount)aver FROM opportunity where accountid in :accountIdSet];
        for (AggregateResult ar : groupedResultaccountIdSet)
        {
            accountWithOppSumMap.put(accountIdSet,Integer.valueOf(ar.get('aver')));
        }
        for(account acc  :[select id,sum__c from account where id in :accountIdSet])
        {
            acc.sum__c = string.valueof(accountWithOppSumMap.get(accountIdSet));
            accountList.add(acc );
        }
        update  accountList;
    }
}

Monday, July 6, 2015

Salesforce Live

Follow the below Link

Salesforce Live

Search records based on picklist field(for eg:search lead records based on status filed)

Apex Class:
public class SearchLeads {
    public Lead newLead  {get;set;}
    public list<lead> leadList  {get;set;}
    public SearchLeads () {
        newLead = new Lead();
    }
    public list<lead> doSearch() {
        leadList =[select id,name,Status  from lead where status =: newLead.status];
        return null;
    }
}

Visualforce Page :
<apex:page controller="SearchLeads">
<apex:form >
Staus : <apex:inputField value="{!newLead.status}"/>
<apex:commandButton value="Search" action="{!doSearch}"/>
    <apex:pageBlock title="List of Leads">
        <apex:pageBlockTable value="{!leadList}" var="item">
            <apex:column value="{!item.name}"/>
            <apex:column value="{!item.status}"/>
        </apex:pageBlockTable>
   </apex:pageBlock>
</apex:form>
</apex:page>

output :

Return type of an Apex action method must be a PageReference. Found: visualforce.el.VisualforceArrayList

Apex Class :
public class SearchLeads {
    public Lead newLead  {get;set;}
    public list<lead> leadList  {get;set;}
    public SearchLeads () {
        newLead = new Lead();
    }
    public list<lead> doSearch() {
        leadList =[select id,name from lead where status =: newLead.status];
        return leadList ;
   }
}

Visual Force Page :
<apex:page controller="SearchLeads">
<apex:form>
Staus : <apex:inputField value="{!newLead.status}"/>
<apex:commandButton value="Search" action="{!doSearch}"/>
</apex:form>

</apex:page>
When we run this vf page and click on "Search" button,we will get this error
"Return type of an Apex action method must be a PageReference. Found: visualforce.el.VisualforceArrayList" because the apex action method i.e DoSearch()
return type is list<lead> not pagereference as shown in image below

So, to avoid this error change return type from list<lead> to "Pagereference" as shown in the code below.
public class SearchLeads {
    public Lead newLead  {get;set;}
    public list<lead> leadList  {get;set;}
    public SearchLeads () {
        newLead = new Lead();
    }
    public pagereference  doSearch() {
        leadList =[select id,name,Status  from lead where status =: newLead.status];
        return null;
    }
}

2nd Solution :
We can also change doSearch() as shown below.
public list<lead> doSearch() {
        leadList =[select id,name from lead where status =: newLead.status];
        return null;
}
Now run this page,you won't get any error.
Enjoy Coding...

Error: Compile Error: Incompatible element type

This type of errors occur when we are assigning value of one type  to other.
For example.

public class IncompatibleType {
public list <account> accountList {get;set;}
 public IncompatibleType () {
    for(account acc:[select id,name from account]) {
        accountList.add(acc.id);
    }
   
 }
}

In the above piece of code, we have declared accountList as list of account (list<account>) , but we are adding " id" to that list.So we will get the following error when we save that class.
Error: Compile Error: Incompatible element type Id for collection of Account at line 5 column 9.
See the image for reference.


To avoid this type of errors,make sure you are assigning value of same type.
Replace above code like this,It will get saved without any error.

public class IncompatibleType {
public list <account> accountList {get;set;}
 public IncompatibleType () {
    for(account acc:[select id,name from account]) {
        accountList.add(acc);
    }
   
 }
}




Thursday, July 2, 2015

SObject row was retrieved via SOQL without querying the requested field

Generally this type of error will occur when we are referring a field which is not present in SOQL query.
For example :
Apex Class:
public class UpdateLeads {
    public void updateLeads() {
        list<lead> leadList = [select id,name from lead ];
        list<lead> leadList1 = new list<lead>();
        for(lead l :leadList ) {  
        l.lastname=l.lastname+'Test';
        leadList1.add(l);
        }
        update leadList1;
        system.debug('swetha'+leadList1);
    }
}

Visualforce Page :
<apex:page controller="UpdateLeads">
<apex:form >
 <apex:commandButton value="Save" action="{!updateLeads}"/>
</apex:form>
</apex:page>

In the above example whenever we click on "save" button we get the above error as shown in the image below
In  apex class above, we are updating  lastname field of lead which is not retrieved or not present in SOQL query.So we got this error.To avoid this type of error,make sure you are retrieving all required fields from SOQL query which are referred in apex class.
change the above query like this
list<lead> leadList = [select id,name,lastname  from lead ];

Enjoy Coding....

Error: Content is not allowed in trailing section

Usually we get this error when ever we are writing some text outside of <apex:page> and  </apex:page> tags.

For example :

<apex:page>
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  This is your new Page
  <!-- End Default Content REMOVE THIS -->
</apex:page>Testing

In the above code sample, "Testing" is written outside of <apex:page> tag.So we got that error.
To avoid this type of error, make sure your code is written with in <apex:page> and </apex:page> tags.

Wednesday, July 1, 2015

How to make apex:inputText field required in visualforce page

Apex Class :
global class InputText {
    public string oppName {get;set;}
    public string term {get;set;}
    public InputText () {

    }

    public void saveRecord() {
        system.debug('oppNameoppName'+oppName );
               
    }
}

Visualforce Page :

<apex:page Controller="InputText" docType="HTML-5.0">
    <apex:form >
        <apex:pageBlock title="Input Text Field Required" mode="edit" id="block1">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!saveRecord}" value="Save" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Opportunity" columns="2">
            
            <apex:pageBlockSectionItem > Opportunity Name
               <apex:outputPanel layout="block" style="float: left">
                      <apex:outputPanel >
                            <div class="requiredInput"><div class="requiredBlock"/>
                                 <apex:inputText value="{!oppName}" id="oppNameId" />              
                            </div>
                      </apex:outputPanel>                               
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
                 
           <apex:pageBlockSectionItem > Term
           <apex:outputPanel layout="block" style="float:left">
                  <apex:outputPanel >
                        <div class="requiredInput"><div class="requiredBlock"/>
                             <apex:inputText value="{!term}" id="termId" />              
                        </div>
                  </apex:outputPanel>                               
           </apex:outputPanel>
           </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output :