0

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!

1

When you instantiate an sObject variable with the Id of an existing record:

        PBSI__PBSI_Item__c item = new PBSI__PBSI_Item__c(id=ol.PBSI__ITEM__c); //item Nevers seems to get instantiated
        

It does not inherit field values from the source variable or record, so this:

     // 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);            

is the expected outcome.

If you want the new record to inherit fields, make sure to query the fields you want, and then clone the record:

    PBSI__PBSI_Sales_Order__c soList = [select id, name, (select name, PBSI__Item__r.Amazon_CAD_Item__c, PBSI__Item__r.Flow_CAD_Item__c from PBSI__Sales_Order_Lines__r) from PBSI__PBSI_Sales_Order__c where id = :salesOrderId];

// ...

item = ol.PBSI__Item__r.clone(true, false, false, false);

// Change field values and run update.
  • Hi David, Thanks for taking the time to look at this! It was very helpful and I was able to complete the rest of the work. I'm going to spend some more time on query relationships, I think. – Patrick W Nov 26 '20 at 18:03

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.