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
RedZoneRedZone 

Apex Code to allow adding inventory equipment via button like 'add product' functionality

I am attempting to basically replicate the product object and functionality for a custom object I created called 'Inventory' on the opportunity records. I have created a button called 'add inventory' to the inventory custom object on the opportunity but I am not great with APEX or Java and I need the button to bring up a multi-select list of all of the 'inventory equipment' which have been uploaded as records on another custom object called 'inventory equipment'. It is literally exactly what the add product button does but I need it to bring up all of the inventory equipment records and relate them to the opportunity.

 

Can anyone help on this??

cmlcml

As per my understanding of your problem, you have a object Inventory which holds all the products and then you have a 'Inventory equipment' object which is a child of Opportunity similar to 'OpprtunityLineItems'. Now you want to add Inventory to opprtunity as 'Inventory Equipment'. Correct?

 

If this is the case,

First first override your 'add inventory' button to redirect to a visualforce page, where you query for all Inventor records and then wrap it in Wrapper class. Wrapper class is required to track which inventory record is getting selected. Then on submit add those inventory records as 'inventory Equipment' records where parent will be related Opportunity.

 

Thanks

Regards

Chandra

RedZoneRedZone

That is the basics - I have an object called Inventory Equipment that holds all of the inventory items and then I have an object called 'inventory' so that they are able to add a new record for each separate time we need to track inventory for that opportunity, the object 'inventory' is related to the opportunity in a master-detail relationship and the 'inventory equipment' is related to the 'inventory' object.

 

Your suggestion is very helpful but my issue is with the coding so I need to figure out how to code that in the way you just explained.

 

Best,

 

Kelsey

RedZoneRedZone

Also, not sure if it is easier or not, but I could basically replicate the product object and make the inventories theyre own pricebook, so when he adds an inventory object he has to choose the 'inventory' pricebook and add them in the same way we add products in. But again, I don't know the coding for that nor can I find it anywhere.

 

-Kelsey

cml26sepcml26sep

I am not sure if i got your problem exactly. but anyway you might need to use wrapper class to keep track of what invetory Items are selected by user.

 

Here is the sample wrapper class you can use.

 

Public class InventoryItemsWrapper

{

 private InventoryItems__c inventoryItem=null;

 private Boolean isSelected=null;

 

 public InventoryItemsWrapper( InventoryItems__c inventoryItem,Boolean isSelected)

 {

  this.inventoryItem=inventoryItem;

  this.isSelected=isSelected;
 }

 

 public InventoryItems__c getInventoryItem()

{

 return this.inventoryItem;

}

public  void setInventoryItem(InventoryItems__c inventoryItem)

{

 this.inventoryItem=inventoryItem;

}

 public Boolean getIsSelected()

{

 return this.isSelected;

}

public  void setIsSelected(Boolean isSelected)

{

 this.isSelected=isSelected;

}

 

}

 

Now in your apex page you can populate a list of aobve wrapper class  and when user selects any inventory item and click add , you need to extract inventory Item from that wrapper and create a new inventory record and attach it to your Opportunity.

Your apex page would look like as below :

 

<apex:page controller="OpptyInventoryController" >

<apex:form>

<apex:pageBlock id="InventoryItemsBlock">

  <apex:pageBlocktable items="{!inventoryItemsWrapperList}" var="item">

     <apex:column > 
          <apex:inputCheckbox value="{!item.isSelected}"/>
       </apex:column>
       <apex:column value="{!item.inventoryItem.Name}" headerValue="InventoryItem"/> 

    </apex:pageBlockTable>

    <apex:commandButton value="Add Inventory Item" action="{!addInventoryItems}"  rerender="OpportunityInventoryBlock"/>

</apex:pageBlock>

<apex:pageBlock id="OpportunityInventoryBlock" rendered="{!isInventoriesExists}">

 <apex:pageBlocktable items="{!inventoryList}" var="inventory">

        <apex:column value="{!inventory.Name}" headerValue="InventoryItem"/> 
        <apex:column value="{!inventory.<other Filed name from inventory Onbject>}" /> 

    </apex:pageBlockTable>

 <apex:pageBlock>

</apex:form>

 

</apex:page>

 

Your controller would look like this :

 

public class OpptyInventoryController

{

  public List<InventoryItemsWrapper> inventoryItemsWrapperList{get;set;}

  public List<Inventory__c> inventoryList{get;set;}

  public Boolean isInventoriesExists{get;set;}

  public Id oppId{get;set;}

 

  public OpptyInventoryController()

 {  

     oppId=ApexPages.currentPage().getParameters().get('id'); 

// id parameters you would be passing on click on 'Add Inventory' button at opportunity page.

     List<InventoryItems__c> inventoryItemsList=[select Id,Name from InventoryItems__c]; // you can modify query as per your requirement.

     for(InventoryItems__c item: inventoryItemsList)

     {

       InventoryItemsWrapper itemWrapper=new InventoryItemsWrapper(item,false);

       inventoryItemsWrapperList.add(itemWrapper);

      }

     inventoryList=[select Id,Name from Inventory__c where Related_Opportunity__c =:oppId];

     if(inventoryList!=null && inventoryList.size()>0)

    {

     isInventoriesExists=true;

     }

     else

    {

     inventoryList=new List<Inventory__c>();

     isInventoriesExists=true;

     }

 }

  public PageReference addInventoryItems()

  {

     for(InventoryItemsWrapper itemWrapper: inventoryItemsWrapperList)

     {

       if(itemWrapper.getIsSelected())

      {

        // initialize inventory record with invenory item record.

        Inventory__c inventoryItem=new Inventory__c(Name=itemWrapper.getInventoryItem().Name,Related_Opportunity__c=oppId);

        inventoryList.add(inventoryItem);

        }

     }

    

    if(inventoryList!=null && inventoryList.size()>0)

    {

      upsert inventoryList;

      isInventoriesExists=true;

     }

   return null;

  }

 

}

 

May be i wouldn't have covered your full functionality but i feel this could be your statign point and you can extend it to meet your requirement.