Hope all is well.
i am a newbie to visual force and apex and have looked at various documentation online. (Especially Visualforce in Practice, however i really need some SF expertise from the community to help me get this over the line, as i dont know how to proceed.
Required outcome : User goes to the sales_and_marketing__c object, scrolls to the related list called Materials_Junction__c. Upon clicking the"new" button the following visual page below should be presented. The user would then select the appropriate products (using the tickbox) and adding an quantity number. Upon pressing save, the values selected should be mapped back to the sales_and_marketing__c object,
Visual force page
<apex:page controller="testWrapper">
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID){
var inputCheckBox = document.getElementsByTagName("input");
for(var i=0; i<inputCheckBox.length; i++){
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
inputCheckBox[i].checked = obj.checked;
}
}
}
</script>
<apex:form >
<apex:pageBlock title="Select Product">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!materialWrapperList}" var="MKT"> <!-- for loop of contact in Materials -->
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
</apex:facet>
<apex:inputCheckbox value="{!MKT.selectB}" id="inputId"/>
</apex:column>
<!-- <apex:column value="{!MKT.selectB}"/>-->
<apex:column value="{!MKT.name}"/>
<apex:column value="{!MKT.Product}"/>
<apex:column value="{!MKT.Item}"/>
<apex:column headerValue="Quantity">
<apex:inputText value="{!MKT.quantity}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
APEX CLASS
public with sharing class testWrapper
{
public List<Materials__c> Materials {get;set;}
public List<materialWrapper> materialWrapperList {get;set;}
public testWrapper()
{
materialWrapperList = new List<materialWrapper>();
Materials = [select ID,name,Product__c, Item__c,Quanity__c, Active__c from Materials__c where Active__c =true limit 10];
for(Materials__c obj : Materials)
{
materialWrapper tempObj= new materialWrapper();
tempObj.recordId = obj.id;
tempObj.name = obj.name;
tempObj.product = obj.Product__c;
tempObj.item = obj.Item__c;
tempObj.quantity = obj.Quanity__c;
tempObj.selectB = false;
materialWrapperList.add(tempObj);
}
}
//save method
public void save()
{
list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
for(materialWrapper obj : materialWrapperList)
{
Materials_Junction__c temp ;
if(obj.selectB == true)
{
temp = new Materials_Junction__c();
temp.sales_and_marketing__c = 'a032000000VQaRT';
temp.Materials__C= obj.recordId;
temp.quantity__C = obj.quantity;
recordToInsert.add(temp);
}
//recordToInsert.add(temp); you are adding element outside the if condition that the reason for save button error
}
insert recordToInsert;
}
public class materialWrapper
{
public string recordId {get; set;}
public string name {get; set;}
public string product {get; set;}
public string item {get; set;}
public Decimal quantity {get; set;}
public boolean selectB {get; set;}
public void materialWrapper()
{
recordId = '';
name = '';
product = '';
item = '';
quantity = 0.0;
selectB = false;
}
}
}