0

So I have two custom Objects. One is Called Order and One is Called Inventory. Inventory will contain a list of items that are currently available and I would like to create a button of sorts on the Order that will open a window that displays all available items on the inventory object. Those items can then be selected and they will have their Order Lookup Field populated with the Order that started this whole process.

I understand how to write a wrapper class for this, the only thin I am curious on is passing in the ID of the order record that originally started the process and putting that value into the lookup field on the inventory records that get selected.

Any thoughts? Current Wrapper Class Below:

public class InventoryWrapperClassController {
public List<Truck_Inventory__c> truckList {get; set;}

public List<Truck_Inventory__c> getTrucks() {
    if(truckList == null) {
        truckList = new List<Truck_Inventory__c>();
        for(Truck_Inventory__c t: [SELECT Id, Buyers_Order__c, Last_6_of_VIN__c, Make__c, Model__c, Year__c from Truck_Inventory__c WHERE Buyers_Order__c = null]) {
            truckList.add(new Truck_Inventory__c(t));
        }
    }
    return truckList;
}

public PageRefernce processSelected() {
    List<Truck_Inventory__c> selectedTrucks = new List<Truck_Inventory__c>();

    for(Truck_Inventory__c truck: getTrucks()) {
        if(truck.selected == true) {
            selectedTrucks.add(truck.trucks);
        }
        truckList=null;
        return null;
    }

    public class Trucks {
        public Truck_Inventory__c trucks {get; set;}
        public Boolean selected {get; set;}

        public Trucks(Truck_Inventory__c t) {
            trucks = t;
            selected = false;
        }
    }
}
}
1

Just use a StandardController and you will be able to reference the record Id. Pulling it into the records you wish to create should be straightforward at that point.

Page

<apex:page standardController="Order__c" extensions="MyExtension">
    <!-- additional markup -->
</apex:page>

Extension

public with sharing class MyExtension
{
    final Id orderId;
    public MyExtension(ApexPages.StandardController controller)
    {
        orderId = controller.getId();
    }
    public PageReference save()
    {
        List<ChildObject> children = new List<ChildObject>();
        for (Wrapper wrapper : selectedWrappers)
        {
            children.add(new ChildObject(Order__c=orderId, /*other fields*/));
        }
        // dml
    }
}
| improve this answer | |
  • Adrian, your code is much different than what I have but it looks a lot cleaner lol. Do you think you can explain a little more in depth what your class is doing? Or I can share with you what I have and you can help me fix it? – cmmoutes13 Nov 30 '16 at 19:35
  • Well, I haven't layed out any selection mechanism or anything, just focused on how to save the records with the lookup populated accordingly. That's the part you were struggling with, no? If you want to add code anywhere, include it in your OP. – Adrian Larson Nov 30 '16 at 19:37
  • I added my current class. – cmmoutes13 Nov 30 '16 at 19:39

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.