Saturday, January 3, 2015

Adding Multiple Contact, Opportunities to Account with action status and Jquery example.

Adding Multiple Contact, Opportunities to Account with action status and Jquery example.


Hi,

In this post i am going to show how to change the back ground of the page when command button (Save) is clicked, to achieve this i used JQuery with Action Status.

 Steps:    
1). Include JQuery library in VF page.
2). Create command button with action status
3). Calling JQuery methods from action stats 


Output: 




Once the user clicks on the save button changing the back ground with JQuery and CSS.


Visualforce Page:

<apex:page controller="AddContactsOpportunitiesCLS" id="thePage" action="{!init}">

<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"/>
<script>
    j$ = jQuery.noConflict();
    
    function showLoadingDiv() {
        var newHeight = j$("[id$=theFm]").css("height");//Just shade the body, not the header
        j$("[id$=loading-curtain-div]").css("background-color", "black").css("opacity", 0.50).css("height", newHeight).css("width", "90%");
    }
    function hideLoadingDiv() {
        j$("[id$=loading-curtain-div]").css("background-color", "black").css("opacity", "1").css("height", "0px").css("width", "90%");
    }
    
</script>
<style>
    #loading-curtain-div {
        height:0px;
        width:100%;
        position:absolute;
        z-index:5;
        //-webkit-transition: all 0.30s ease-out;
        //-moz-transition: all 0.30s ease-out;
    }
</style>



 <apex:form id="theFm">
   <div id="loading-curtain-div"/>
   <apex:pageBlock title="Account--Contacts--Opportunities Creation" >
   <apex:actionStatus id="save-lead-status" onstart="showLoadingDiv();" onstop="hideLoadingDiv();"/>
    <apex:pageblockButtons >
     <apex:commandButton value="Save" action="{!Save}" status="save-lead-status" reRender="theFm" />
     <apex:commandButton value="Cancel" action="{!Cancel}" immediate="true"/>
    </apex:pageblockButtons>
     
     
     <apex:pageBlockSection id="pbs" columns="2" title="Account Details">      <apex:inputField value="{!acc.Name}"/>
      <apex:inputField value="{!acc.accountNumber}"/>
     <apex:inputField value="{!acc.Type}"/>
     <apex:inputField value="{!acc.Industry}"/>
     </apex:pageBlockSection>
     
     <apex:pageBlockSection title="Contact Details" columns="1" collapsible="false">
       <apex:outputPanel id="contactHead">
      <apex:variable value="{!0}" var="rowNum"/>  
        <apex:pageBlockTable value="{!waConList}" var="eachRecord"> 
         <apex:column headerValue="Action">
        <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromConList}" rendered="{!rowNum > 0}" rerender="contactHead" immediate="true" status="conRemoveLink" >
             <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
         </apex:commandLink>
         <apex:actionStatus id="conRemoveLink" >
                <apex:facet name="start" >
                  <img src="/img/loading.gif" />                    
                </apex:facet>
         </apex:actionStatus>
         <apex:variable var="rowNum" value="{!rowNum + 1}"/>
      </apex:column>
      <apex:column headerValue="First Name">
                            <apex:inputField value="{!eachRecord.record.FirstName}" required="true"/>
       </apex:column>
       <apex:column headerValue="Last Name">
                            <apex:inputField value="{!eachRecord.record.LastName}" required="true"/>
       </apex:column>
       <apex:column headerValue="Phone">
                            <apex:inputField value="{!eachRecord.record.phone}" required="true"/>
       </apex:column>
       <apex:column headerValue="Email">
                            <apex:inputField value="{!eachRecord.record.email}" required="true"/>
       </apex:column>
      
        </apex:pageBlockTable>
        <apex:commandButton value="Add More" action="{!addNewRowToConList}" rerender="contactHead" Status="conStatusId" immediate="true" />
        <apex:actionStatus id="conStatusId" >
                <apex:facet name="start" >
                  <img src="/img/loading.gif" />                    
                </apex:facet>
         </apex:actionStatus>
     </apex:outputPanel>
     </apex:pageBlockSection>
     
     
     <!-- Opportunity Table Goes here -->
          <apex:pageBlockSection title="Opportunity Details" columns="1" collapsible="false">
       <apex:outputPanel id="opportunityHead">
      <apex:variable value="{!0}" var="rowNum"/>  
        <apex:pageBlockTable value="{!waOppList}" var="eachRecord"> 
         <apex:column headerValue="Action">
        <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromOppList}" rendered="{!rowNum > 0}" rerender="opportunityHead" immediate="true" status="oppRemoveLink" >
             <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
         </apex:commandLink>
         <apex:actionStatus id="oppRemoveLink" >
                <apex:facet name="start" >
                  <img src="/img/loading.gif" />                    
                </apex:facet>
         </apex:actionStatus>
         <apex:variable var="rowNum" value="{!rowNum + 1}"/>
      </apex:column>
      <apex:column headerValue="Name">
                            <apex:inputField value="{!eachRecord.record.Name}" required="true"/>
       </apex:column>
       
       <apex:column headerValue="Amount">
                            <apex:inputField value="{!eachRecord.record.Amount}" required="true"/>
       </apex:column>
       <apex:column headerValue="Stage">
                            <apex:inputField value="{!eachRecord.record.StageName}" required="true"/>
       </apex:column>
       <apex:column headerValue="CloseDate">
          <apex:inputField value="{!eachRecord.record.closeDate}" required="true"/>
       </apex:column>
       
       
      
        </apex:pageBlockTable>
        <apex:commandButton value="Add More" action="{!addNewRowToOppList}" rerender="opportunityHead" Status="oppStatusId" immediate="true" />
        <apex:actionStatus id="oppStatusId" >
                <apex:facet name="start" >
                  <img src="/img/loading.gif" />                    
                </apex:facet>
         </apex:actionStatus>
     </apex:outputPanel>
     </apex:pageBlockSection>
   </apex:pageBlock>

 </apex:form>

</apex:page>


Controller Class:

public class AddContactsOpportunitiesCLS {

public Account acc {get;set;}
public List<WrapperContactsList> waConList {get;set;}
public List<WrapperOpportunitiesList> waOppList {get;set;}
public Integer rowToRemove {get;set;}

public AddContactsOpportunitiesCLS (){
acc= new Account();
}

public void init(){
if(waConList ==null){
waConList = new List<WrapperContactsList>();
}
if(waOppList ==null){
 waOppList = new List<WrapperOpportunitiesList>();
}

}

public void addNewRowToConList(){
waConList = AddContactsOpportunitiesHelperCLS.addNewRowToContactList(waConList );
    
}

public void removeRowFromConList(){
  waConList = AddContactsOpportunitiesHelperCLS.removeRowToContactList(rowToRemove ,waConList );

}

public void addNewRowToOppList(){
     waOppList = AddContactsOpportunitiesHelperCLS.addNewRowToOpportunityList(waOppList );
}

public void removeRowFromOppList(){
   waOppList = AddContactsOpportunitiesHelperCLS.removeRowToOpportunityList(rowToRemove,waOppList );
}


Public class WrapperContactsList{
        public Integer index {get;set;}
        public Contact record{get;set;}
}

 public PageReference Cancel() {
  PageReference prf = new PageReference('/apex/AddContactsOpportunitiesVF');
  prf.setRedirect(true);
  return prf;
 }

public PageReference Save() {
 PageReference prf;
 boolean flag;
  if(acc.Name !=null && acc.Name !=''){
   upsert acc;
   if(acc.id != null ){
    flag = AddContactsOpportunitiesHelperCLS.save(acc.id,waConList,waOppList);
    if(flag){
      prf = new PageReference('/apex/AddContactsOpportunitiesVF');
      prf.setRedirect(true);
     
     }
   }
  }
   return prf;
}


public class WrapperOpportunitiesList{
        public Integer index{get;set;}
        public Opportunity record{get;set;}



}

Controller Helper Class:

Public class AddContactsOpportunitiesHelperCLS{

Public static List<AddContactsOpportunitiesCLS.WrapperContactsList> addNewRowToContactList(List<AddContactsOpportunitiesCLS.WrapperContactsList> waConObjList){
        AddContactsOpportunitiesCLS.WrapperContactsList newRecord = new AddContactsOpportunitiesCLS.WrapperContactsList();
        Contact newContactRecord = new Contact();        
        newRecord.record = newContactRecord;
        newRecord.index = waConObjList.size();
        waConObjList.add(newRecord);
        return waConObjList;
}


public static List<AddContactsOpportunitiesCLS.WrapperContactsList> removeRowToContactList(Integer rowToRemove, List<AddContactsOpportunitiesCLS.WrapperContactsList> waConList){
        waConList.remove(rowToRemove);
        return waConList;
}

/*  adding opportunities to the list */
Public static List<AddContactsOpportunitiesCLS.WrapperOpportunitiesList> addNewRowToOpportunityList(List<AddContactsOpportunitiesCLS.WrapperOpportunitiesList> waOppObjList){
        AddContactsOpportunitiesCLS.WrapperOpportunitiesList newRecord = new AddContactsOpportunitiesCLS.WrapperOpportunitiesList();
        Opportunity newOpportunityRecord = new Opportunity();        
        newRecord.record = newOpportunityRecord;
        newRecord.index = waOppObjList.size();
        waOppObjList.add(newRecord);
        return waOppObjList;
}



public static List<AddContactsOpportunitiesCLS.WrapperOpportunitiesList> removeRowToOpportunityList(Integer rowToRemove, List<AddContactsOpportunitiesCLS.WrapperOpportunitiesList> waOppList){
        waOppList.remove(rowToRemove);
        return waOppList;
}
    
public static boolean save(String accId,List<AddContactsOpportunitiesCLS.WrapperContactsList> waConList  ,List<AddContactsOpportunitiesCLS.WrapperOpportunitiesList> waOppList) {
  boolean statusFlag = true;    
  system.debug('==waConList==>'+waConList.size());
  system.debug('==waOppList==>'+waOppList.size());
    
    try{
        
        List<Contact> contactRecordsToBeInserted = new List<Contact>();
        if(waConList !=null && !waConList.isEmpty()){
            for(AddContactsOpportunitiesCLS.WrapperContactsList eachRecord : waConList ){
                Contact conTemp = eachRecord.record;
                conTemp.AccountId = accId;
                contactRecordsToBeInserted.add(conTemp);
               
          }
            system.debug('==contactRecordsToBeInserted==>'+contactRecordsToBeInserted.size());
            upsert contactRecordsToBeInserted;
        }
    List<Opportunity> opportunityRecordsToBeInserted = new List<Opportunity>();
     if(waOppList != null && !waOppList.isEmpty()) {
       for(AddContactsOpportunitiesCLS.WrapperOpportunitiesList eachRecord : waOppList ){
                Opportunity oppTemp = eachRecord.record;
                oppTemp.AccountId = accId;
                opportunityRecordsToBeInserted.add(oppTemp);
               
            }
         
          system.debug('==opportunityRecordsToBeInserted==>'+opportunityRecordsToBeInserted.size());
            upsert opportunityRecordsToBeInserted;
     }
        
    }catch(Exception e){
        system.debug('Exception occure in save method..of helper classs-->'+e);
        statusFlag =false;
    }
       
    return statusFlag;
    
    }    



}




Enjoy the coding...


0 comments:

Post a Comment

 
| ,