function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Daniel MasonDaniel Mason 

Newbie help on Wrapper Class

Hi fellow Salesforcers

I need some help.

Aim : A user goes to Salesforce and marketing Object , scrolls to related list marketing materials. Click new. This  should then opens up a VF page which lists all records in material object.

The User will select what records they want. Upon clicking save,  the records chosen should  map back to the "Sales and marketing" object 

In my developer org i have created the following three objects ; 

Sales & marketing
Marketing materials
Material conjuction

Marketing Materials object has the following fields 

Product (text)
Item (text)
Active (check box)

Materials conjunction object has the following fields 
Master detail to sales and marketing oject 
master detail to materials object 

I have looked at intro to visual force and I have used the examples and tailored to my needs. To grasp the concept but I am a little confused . I have built this in my own Dev org more than happy to create a new user to walk through this.

Markup
 

<apex:page controller="test">
    <apex:form >
        <apex:pageBlock title="Select Product">

            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>

            <apex:pageBlockTable value="{!Materials}" var="MKT"> <!-- for loop of contact in Materials -->
                  <apex:column value="{!MKT.name}"/>       
                <apex:column value="{!MKT.Product__c}"/>
                <apex:column value="{!MKT.Item__c}"/>
                 <apex:column value="{!MKT.Quanity__c}"/>
                 <apex:column value="{!MKT.Active__c}"/>

                    <apex:column headerValue="Quanity">
                    <apex:inputField value="{!MKT.Quanity__c}"/>
                </apex:column>

            </apex:pageBlockTable>

        </apex:pageBlock>
    </apex:form>
</apex:page>
Controller
public with sharing class test
{
    public List<Materials__c> Materials {get;set;} //global variables

    //constructor
    public test()
    {
        Materials = [select ID,name,Product__c, Item__c,Quanity__c, Active__c from Materials__c where Active__c =true limit 10];
    }

    //save method
    public void save()
    {
        update Materials;
    }
}
This is the current outcome, however as you can see when i save its just update quantity value on the materials object
User-added image

Looking forward to your response 

Regards
Masond3
JeffreyStevensJeffreyStevens
Here are my quick notes on using a wrapper class....

Wrapper classes
Public class wrapperClassName {
  public      string      fieldName1        {get;set;}
  public      string      fieldName2        {get;set;}
}
 
Use….
Public  wrapperClassName  {get;set;}
…...
wrapperClassName = new wrapperClassName();
wrapperClassName.fieldName1 = ‘Value1’;
wrapperClassName.fieldName2 = ‘Value 2’;

 
swati_sehrawatswati_sehrawat
Hello Daniel,

Just for clarification, whatever user will select from Marketing materials which would be coming as list on visualforce page should be saved in "Material conjuction" and should be visible under  "Sales & marketing" as related list. Also quantity selected should create that many records of material selected or should just update a field on "Material conjuction" to show how many quantities are needed?
Daniel MasonDaniel Mason

Hello Swati, 

The user should go to the Sales & Marketing Object, click on a related list, this should then open up a VF page. The VF page should show all of the records from the Materials Object. The user should select the records and  the Quanity value, upon save this should then map back to  Sales & Marketing Object

swati_sehrawatswati_sehrawat
Hello Daniel,

You need to use wrapper class and your code should be something like:
 
public with sharing class test
{
    public List<Materials__c> Materials {get;set;} //global variables
	public List<materialWrapper> materialWrapperList {get;set;} //use this list on vf page and not Materials

    //constructor
    public test()
    {
        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.select = false;
			materialWrapperList.add(tempObj);
		}
    }

    //save method
    public void save()
    {
        list<Material_conjuction__c> recordToInsert = new list<Material_conjuction__c>();
		for(materialWrapper obj : materialWrapperList){
			if(obj.select == true){
				Material_conjuction__c temp = new Material_conjuction__c();
				temp.sales and marketing oject  = // put sales and marketing oject id here
				temp.materials object = obj.recordId;
				temp.quantity = obj.quantity; //create quantity field on conjunction object
			}
			recordToInsert.add(obj);
		}
		insert recordToInsert;
    }
	
	
	public materialWrapper{
		string recordId {get; set;}
		string name {get; set;}
		string product {get; set;}
		string item {get; set;}
		string quantity {get; set;}
		boolean select {get; set;}
		
		public void materialWrapper(){
			recordId = '';
			name = '';
			product = '';
			item = '';
			quantity = '';
			select = false;
		}
	}
}

 
swati_sehrawatswati_sehrawat
<apex:page controller="test">
    <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 value="{!MKT.select}"/>
				<apex:column value="{!MKT.name}"/>       
                <apex:column value="{!MKT.Product}"/>
                <apex:column value="{!MKT.Item}"/>
                <apex:column headerValue="Quanity">
                    <apex:inputField value="{!MKT.Quanity}"/>
                </apex:column>

            </apex:pageBlockTable>

        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Daniel MasonDaniel Mason
HI swati_sehrawat, If you got time can we have a quick GTM ? As i am confused and stuck
swati_sehrawatswati_sehrawat
swati.sehrawat19@gmail.com is my email address, lets continue over there.