1.select left side values and click on add button after click on update button is shows below image
if select any vlaue from right side click remove button and click on update it shows update market lists
==============visualforce page code=====================================
<apex:page standardController="Account" extensions="multipleRecordsOnOpportunity" sidebar="false">
<apex:form >
<apex:pageMessages escape="true"/>
<apex:pageBlock title="Account Details" rendered="{!flag}">
<apex:pageBlockSection columns="2">
<apex:outputField value="{!acc.name}"/>
<apex:outputField value="{!acc.CustomerPriority__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Select values" id="pb" rendered="{!flag}">
<apex:panelGrid columns="3" id="abcd">
<apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:100px" size="5">
<apex:selectOptions value="{!unselectedvalues}" />
</apex:selectList>
<apex:panelGroup >
<br/>
<apex:commandButton action="{!selectclick}" value="add" style="width:100px" reRender="pb"/>
<br/><br/>
<apex:commandButton action="{!unselectclick}" value="remove" style="width:100px" reRender="pb"/>
</apex:panelGroup>
<apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:100px" size="5">
<apex:selectOptions value="{!SelectedValues}" />
</apex:selectList>
</apex:panelGrid>
<apex:pageBlockButtons >
<apex:commandButton value="UpdateMarket" action="{!updateMarkets}" reRender="form"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
<apex:form id="form" rendered="{!flag}">
<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 Owner Name">
<apex:inputField value="{!anopp.ownerid}" required="true"/>
</apex:column>
<apex:column headerValue="Market">
<apex:outputField value="{!anopp.OrderNumber__c}"/>
</apex:column>
<apex:column headerValue="Close Date">
<apex:inputField value="{!anopp.CloseDate}" 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="Account Name">
<apex:inputField value="{!anopp.AccountId}"/>
</apex:column -->
</apex:pageBlockTable>
</apex:pageblocksection>
<apex:pageBlockButtons >
<apex:commandButton value="SaveRecords" action="{!SaveRecords}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
===========controller=================================
public with sharing class multipleRecordsOnOpportunity {
Public List<string> leftselected{get;set;}
public List<String> selectedMarketsList {get; set;}
Public List<string> rightselected{get;set;}
public list<selectoption> getPickLstValue(){return null;}
public Set<String> originalvalues = new Set<String>{'A','B','C','D','E','F','G','H','I'};
public set<string> leftvalues = new set<string>();
public set<string> rightvalues = new set<string>();
public account acc{set;get;}
public opportunity opp {set;get;}
public boolean flag{set;get;}
public list<opportunity> oppsToCreateList{set;get;}
// constructor
public multipleRecordsOnOpportunity(ApexPages.StandardController controller) {
opp= new opportunity();
leftselected = new List<String>();
rightselected = new List<String>();
leftvalues.addall(originalvalues );
selectedMarketsList= new list<string>();
oppsToCreateList=new list<Opportunity>();
rightselected = new List<String>();
string str=ApexPages.CurrentPage().getParameters().get('id');
system.debug('99999999999999999999999999999'+str);
try{
acc=[select id,name,CustomerPriority__c from account where id=:str limit 1];
if(acc.CustomerPriority__c=='High'|| acc.CustomerPriority__c=='Medium'){
flag=true;
}
else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Multiple Opportunities can only be created from High and medium from parent account.');
ApexPages.addMessage(myMsg);
}
}
catch(Exception e){
ApexPages.addMessages(e);
}
}
// after click on add buttone display vlaues in Right side panel
public List<SelectOption> getSelectedValues(){
List<SelectOption> options1 = new List<SelectOption>();
List<string> tempList = new List<String>();
tempList.addAll(rightvalues);
tempList.sort();
for(String s : tempList)
options1.add(new SelectOption(s,s));
system.debug('1111111111111111111111'+options1);
return options1;
}
// by defult left side values or after click on remove values in left panel
public List<SelectOption> getunselectedvalues(){
List<SelectOption> options = new List<SelectOption>();
/*Schema.DescribeFieldResult fieldResult = opportunity.opportunity_multi__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for(Schema.PicklistEntry f : ple){
if(f.getValue() != 'A' && f.getValue() != 'b' ){
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
}*/
List<string> tempList = new List<String>();
tempList.addAll(leftvalues);
tempList.sort();
for(string s : tempList)
options.add(new SelectOption(s,s));
system.debug('00000000000000000000000000000'+options);
return options;
}
// aaction function of add button add values in right side
public PageReference selectclick(){
rightselected.clear();
for(String s : leftselected){
leftvalues.remove(s);
rightvalues.add(s);
}
system.debug('22222222222222222222222'+leftvalues);
system.debug('33333333333333333333333'+rightvalues);
return null;
}
// after click on Remove button display vlaues in panel
public PageReference unselectclick(){
leftselected.clear();
for(String s : rightselected){
rightvalues.remove(s);
leftvalues.add(s);
}
system.debug('4444444444444444444444444'+leftvalues);
system.debug('5555555555555555555555555'+rightvalues);
return null;
}
// clone Records Based number of markets selecting(Ex:selecting c,d,e clones 3 records)
public PageReference updateMarkets(){
oppsToCreateList.clear();
selectedMarketsList.clear();
selectedMarketsList.addall(rightvalues);
system.debug('000000000000000000000'+rightvalues+selectedMarketsList);
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_multi__c)
{
alreadyAdded = true;
}
}
if (themkt == opp.opportunity_multi__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 = opp.clone(false);
Newopp.OrderNumber__c = themkt;
newopp.accountid=acc.id;
newopp.ownerId=UserInfo.getUserId();
newopp.stagename='1 - Prospect Evaluation';
newopp.closedate=date.today();
newopp.Name =acc.name;
oppsToCreateList.add(newopp);
}
}
system.debug('1111111111111111111111111111111111111'+oppsToCreateList);
return null;
}
public PageReference SaveRecords(){
if (oppsToCreateList != null && oppsToCreateList.size() > 0)
{
try
{
insert oppsToCreateList;
}
catch (Exception e)
{
ApexPages.addMessages(e);
return null;
}
}
return new PageReference('/'+acc.Id);
}
}
====================Test class for above========================
@isTest()
public class TestmultipleRecordsOnOpportunity{
static testMethod void myUnitTest_creat_multiple_Opportunity()
{
// This method for successful adding Multiple OpportunityRecords to Account
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c ='mp';
insert a1;// Creating Mock Data For Account
Opportunity o1 = new Opportunity();
o1.Name = 'testo1prodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprod';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100000000;
insert o1;// Creating Mock Date For Opportunity
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
//Adding visualforce page multipleRecordsOnOpportunity To Testclass
ApexPages.StandardController sc =new ApexPages.StandardController(a1);// Initializing Standard Controller
multipleRecordsOnOpportunity myPageCon =new multipleRecordsOnOpportunity (sc);// Adding Extension to Standard controller
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
list<SelectOption> testoptions=myPageCon.getSelectedValues();
mypagecon.unselectclick();
List<SelectOption> testoptions11=myPageCon.getunselectedvalues();
myPagecon.selectclick();
list<SelectOption> testoptionss=myPageCon.getSelectedValues();
list<String> originalvalues = new list<String>{'A','B','C'};
myPageCon.updateMarkets();// calling method updateMarkets
myPageCon.selectedMarketsList=originalvalues ;
myPageCon.updateMarkets();// calling method updateMarkets checking loop conditons
myPageCon.SaveRecords();// calling method newRecordsInsertMethod
Test.stopTest();
}
static testMethod void myUnitTest_creat_multiple_Opportunity1()
{
// This method for successful adding Multiple OpportunityRecords to Account
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='medium';
insert a1;// Creating Mock Data For Account
Opportunity o1 = new Opportunity();
o1.Name = 'testo1prodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprodprod';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100000000;
insert o1;// Creating Mock Date For Opportunity
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
//Adding visualforce page multipleRecordsOnOpportunity To Testclass
ApexPages.StandardController sc =new ApexPages.StandardController(a1);// Initializing Standard Controller
multipleRecordsOnOpportunity myPageCon =new multipleRecordsOnOpportunity (sc);// Adding Extension to Standard controller
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
list<String> originalvalues = new list<String>{'A','B','C','D','E','F','G','H','I'};
myPageCon.updateMarkets();// calling method updateMarkets
myPageCon.selectedMarketsList=originalvalues ;
myPageCon.updateMarkets();// calling method updateMarkets checking loop conditons
myPageCon.SaveRecords();// calling method newRecordsInsertMethod
Test.stopTest();
}
static testMethod void myUnitTest_MassCloneFail()
{
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='High';
insert a1;
Opportunity o1 = new Opportunity();
o1.Name = 'testo1';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100;
insert o1;
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+a1.Id);
Test.setCurrentPageReference(pageRef);
// create an instance of the controller
ApexPages.StandardController sc =
new ApexPages.StandardController(o1);
multipleRecordsOnOpportunity myPageCon = new multipleRecordsOnOpportunity (sc);
list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
list<String> originalvalues = new list<String>{'A','B','C','D','E','F','G','H','I'};
myPageCon.selectedMarketsList =originalvalues ;
myPageCon.updateMarkets();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
Test.stopTest();
}
static testMethod void myUnitTest_MassCloneFail1()
{
Account a1 = new Account();
a1.Name = 'testa1';
a1.CustomerPriority__c='low';
insert a1;
Opportunity o1 = new Opportunity();
o1.Name = 'testo1';
o1.AccountId = a1.Id;
o1.StageName = 'test';
o1.CloseDate = Date.today();
o1.Amount = 100;
insert o1;
Test.startTest();
PageReference pageRef = new PageReference('/apex/multipleRecordsOnOpportunity ?id='+null);
Test.setCurrentPageReference(pageRef);
// create an instance of the controller
ApexPages.StandardController sc =
new ApexPages.StandardController(o1);
multipleRecordsOnOpportunity myPageCon = new multipleRecordsOnOpportunity (sc);
//myPageCon.selectedMarketsList= 'UK;France';list<SelectOption> testoptions=myPageCon.getSelectedValues();
List<SelectOption> testoptions1=myPageCon.getunselectedvalues();
myPagecon.selectclick();
mypagecon.unselectclick();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
myPageCon.opp.Name = '';
myPageCon.SaveRecords();
Test.stopTest();
}
}
No comments:
Post a Comment