1

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;
        }
    }
}
  • That workbook does not appear to have any examples that would help you implement this use case. I added a few links that should help you. – Adrian Larson Sep 5 '16 at 21:05
3

If you want to create a List button, you need to specify standardController and recordSetVar attributes on your <apex:page> tag. You cannot use the controller attribute in conjunction with these attributes, so you will have to modify your code to fit within the extensions attribute instead.

<apex:page
    standardController="MyJunction__c"
    recordSetVar="records"
    extensions="MyExtension">
    <!--content-->
</apex:page>

When creating an extension for a List Button, you must have a constructor that accepts the StandardSetController created by the page.

public with sharing class MyExtension
{
    public MyExtension(ApexPages.StandardSetController controller)
    {
        // do stuff
    }
}

Related reading:

0

After providing examples above, i was able to do the following

Controller :

<apex:page standardController="Materials_Junction__c" extensions="testWrapper">
function selectAllCheckboxes(obj,receivedInputID){ var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i
<apex:form >
<apex:pageMessages />
    <apex:pageBlock title="Select Product">
{!marketingJunction.sales_and_marketing__c}
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
        </apex:pageBlockButtons>

        <apex:pageBlockTable value="{!materialWrapperList}" var="MKT">
            <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.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 class ;

public with sharing class testWrapper

{ public List Materials {get;set;} public List materialWrapperList {get;set;} public Materials_Junction__c marketingJunction {get;set;} public testWrapper(ApexPages.StandardController controller) //public testWrapper() {
marketingJunction = (Materials_Junction__c) controller.getRecord(); materialWrapperList = new List(); 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 Pagereference save()
{
    list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
    try{
    for(materialWrapper obj : materialWrapperList)
    {
     Materials_Junction__c temp ;
        if(obj.selectB == true)
        {
            temp = new Materials_Junction__c();
            temp.sales_and_marketing__c = marketingJunction.sales_and_marketing__c;
            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; 
    return new Pagereference('/'+marketingJunction.sales_and_marketing__c);
    }catch(Exception e){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,e.getMessage()));
    return null;
    }       
}


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;
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.