Saturday, November 8, 2014

visual force page One Account Adding Multiple Opportunities.

Scenario: Based selecting market in Opportunity Creating multiple opprtunities.
    Ex:Selecting wgl,hyd,knr 3 markets creating 3 opportunity Records name same as Account name

******************VISUALFORCE PAGE****************************************

       <apex:page standardController="Account" extensions="OpportunitiesRecords">
 <apex:form >
    <apex:pageMessages />
    <apex:pageBlock title="Original Account" id="pb">
        <apex:pageBlockSection columns="2" id="sectionOrigOpp" >
              <apex:outputField value="{!acc.name}"/>
              <apex:outputField value="{!acc.CP_Hierarchy_Level_Type__c}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock title="Select Market">
          <apex:pageBlockSection >
               <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Opportunity Market" for="oppMarketMulti" />
                    <apex:inputField value="{!OPPS.Opportunity_Market_Multi__c}" required="true" id="oppMarketMulti"/>
               </apex:pageBlockSectionItem>  
          </apex:pageBlockSection>
          <apex:pageBlockButtons >
                 <apex:commandButton value="SaveOpportunity" action="{!addRecords}" rerender="saveForm" />
          </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>
    <apex:pageMessages id="msg" />
    <apex:form id="saveForm">
        <apex:pageBlock id="oppsToCreate"  title="Opportunities" rendered="TRUE">
          <apex:pageBlockSection columns="1">
            <apex:pageBlockTable value="{!oppsToCreateList}" var="anopp" width="700px">
                    <apex:column headerValue="Opportunity Name">
                        <apex:inputField value="{!anopp.Name}" required="true"/>
                    </apex:column>
                    <apex:column headerValue="Opportunity Name">
                        <apex:inputField value="{!anopp.ownerid}" required="true"/>
                    </apex:column>
                    <apex:column headerValue="Market">
                        <apex:outputField value="{!anopp.Opportunity_Market__c}"/>
                    </apex:column>
                    <apex:column headerValue="Close Date">
                        <apex:inputField value="{!anopp.CloseDate}" required="true"/>
                    </apex:column>
                    <apex:column headerValue="Core Product">
                        <apex:inputField value="{!anopp.Core_Product__c}" required="true"/>
                    </apex:column>
                    <apex:column headerValue="Sub Product">
                        <apex:inputField value="{!anopp.Sub_Product__c}" required="true"/>
                    </apex:column>
                    <apex:column headerValue="Stage">
                        <apex:inputField value="{!anopp.StageName}" required="true"/>
                    </apex:column>
                    <apex:column headerValue="Potential CV">
                        <apex:inputField value="{!anopp.Amount}" required="true"/>
                    </apex:column>
                    <apex:column headerValue="Currency">
                        <apex:inputField value="{!anopp.CurrencyIsoCode}" required="true"/>
                    </apex:column>
                    <!--apex:column headerValue="Account Name">
                        <apex:inputField value="{!anopp.AccountId}"/>
                    </apex:column -->
            </apex:pageBlockTable>
            </apex:pageblocksection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save Records" action="{!SaveRecords}" />
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
         
       </apex:pageBlock> 
    </apex:form>            
</apex:page>





*****************************CONTROLLER*************************************
public class OpportunitiesRecords {
    public Opportunity OPPS{get; set;}
    public Account acc{get; set;}
    public string  mal{set;get;}
    public String marketsString {get; set;}
    public List<String> selectedMarketsList {get; set;}
    public OpportunitiesRecords(ApexPages.StandardController opp){
         OPPS= new Opportunity();
         oppsToCreateList=new list<Opportunity>();
         mal=ApexPages.CurrentPage().getParameters().get('id');
         system.debug('AAAAAAAAAAAAAAAACCCCCCCCCCCCCOunt'+mal);
         acc=[select id,name,CP_Hierarchy_Level_Type__c from account where id=:mal];
    }
   
    public PageReference addRecords(){
        marketsString = OPPS.Opportunity_Market_Multi__c;
        selectedMarketsList = new List<String>();
        if (Utility.isNotNull(marketsString))
        {
            selectedMarketsList = marketsString.split(';');
        }
       
        //oppsToCreateList.add(OPPS);
       
        //## iterate over the selected markets and create an Opp for each one...
        for (Integer i=0; i<selectedMarketsList.size(); i++)
        {
            String themkt = selectedMarketsList.get(i);
           
            boolean alreadyAdded = false;
            //## see if the Market is already in the oppsToCreateList
            for (Opportunity tmpopp : oppsToCreateList)
            {
                if (themkt == tmpopp.Opportunity_Market__c)
                {
                    alreadyAdded = true;
                }  
            }
           
            if (themkt == OPPS.Opportunity_Market__c || alreadyAdded)
            {
                //## don't re-add the original opp to the list, and don't
                //## add the same record to the list multiple times.
            }
            else
            {
                Opportunity newopp = OPPS.clone(false);
                String thenom = acc.Name;
                //thenom = thenom + ' - ' + themkt;
                newopp.Opportunity_Market__c = themkt;
                newopp.accountid=mal;
                newopp.stagename='1 - Prospect Evaluation';
                newopp.closedate=date.today();
                newopp.RecordTypeId='012Z000000096eo';

                if (thenom.length() > 80)
                {
                    thenom = thenom.substring(0,80);
                }
                newopp.Name = acc.Name;
               
               
                oppsToCreateList.add(newopp);
               
            }
           
        }
       

        return null;
  }
  public PageReference doOppClone()
    {
       
System.Debug('    oppsToCreate:'+oppsToCreateList);
        if (oppsToCreateList != null && oppsToCreateList.size() > 0)
        {
            try
            {      
                insert oppsToCreateList;
            }
            catch (Exception e)
            {
                ApexPages.addMessages(e);
                return null;       
            }
        }
        return  new PageReference('/'+acc.Id);
    }
 
  public List<Opportunity> oppsToCreateList {get;set;}
}