I've been struggling with the following problem for a few days now...
What I want to do: I'm trying to create a visual force page and controller to 'swap' 2 fields when a list button 'Swap Decscriptions' is clicked. The solution is partially working, I have created a VF page and controller class to handle the button click, but I'm having some issues with accessing the data properly.
The objects involved are all custom: Sales_Order__c -> Sales_Order_line__c -> Item__c
Once the Sales Order page is opened I added a button to the Sales Order Line related list. When the Swap button is clicked the Amazon_CAD_Item__c and Flow_CAD_Item__c are to be swapped for all lines.
Amazon_CAD_Item__c and Flow_CAD_Item__c are Lookups to the Item Object. A self-reference I guess, and what I seem to have a lot of trouble with.
VF Page:
<apex:page standardController="PBSI__PBSI_Sales_Order__c" extensions="SwapFieldsController" action="{!swapFields}">
</apex:page>
Apex Class:
public class SwapFieldsController {
private ApexPages.StandardController stdCtrlr;
public SwapFieldsController(apexpages.StandardController inStd)
{
stdCtrlr = inStd;
}
public PageReference swapFields()
{
PBSI__PBSI_Sales_Order__c salesObj = (PBSI__PBSI_Sales_Order__c)stdCtrlr.getRecord();
ID salesOrderId = salesObj.Id;
system.debug('SO Id:' +salesOrderId);
PBSI__PBSI_Sales_Order__c soList = [select id, name, (select name, PBSI__Item__c from PBSI__Sales_Order_Lines__r) from PBSI__PBSI_Sales_Order__c where id = :salesOrderId];
list<PBSI__PBSI_Sales_Order_Line__c> lineList = soList.PBSI__Sales_Order_Lines__r;
system.debug('order line list:' +lineList);
for(PBSI__PBSI_Sales_Order_Line__c ol : lineList){
List<PBSI__PBSI_Item__c> updateItemList = new List<PBSI__PBSI_Item__c>();
PBSI__PBSI_Item__c item = new PBSI__PBSI_Item__c(id=ol.PBSI__ITEM__c); //item Nevers seems to get instantiated
// all of the data below return nulls...
system.debug('Item Object:' +item);
string tmp = item.Amazon_CAD_Item__c;
system.debug('tmp:' +tmp);
system.debug('Amazon:' +item.Amazon_CAD_Item__c);
system.debug('Flow:' +item.Flow_CAD_Item__c);
//update item;
}
return stdCtrlr.view();
}
}
Any help or insights are appreciated. I am also open to different ways of approaching a solution for this. Thanks in advance!