1

How do I reference my wrapper class in my prepareshipment method? My goal was to set tNumber and location to = my fields on my shipment object.

In my VF page I want tNumber and Location to display Tracking_Number__c and Location__c during the edit field. I had set location and tnumber like this prior and it worked fine in my vf page but I have more variables i need to set as well and that method would take forever:

    public string location{
    get {
        if( location == null ) location = ship.Location__c;
        return location;
        }
    set; 

Current class I Have with wrapper:

   public  with sharing class shipmentController {

    public shipment__c ship {get;set;}
    public String shipId {get;set;}

    public class shipWrapper{
        public String tNumber {get;set;}
        public String location {get;set;}

    public shipWrapper(Shipment__c ship){
        this.tNumber = ship.Tracking_Number__c;
        this.location = ship.location__c;
    }

}
public shipmentController() {
        shipId = ApexPages.currentPage().getParameters().get('shipId');
        if (shipid == null) {
            ship = new Shipment__c();
        return;
    }

    ship = [select id, location__c, tracking_number__c
                    from shipment__c
                    where id = :shipid];
    }
    public PageReference prepareShipment(ShipWrapper ShipWrapper) {
        ship.tracking_Number__c = shipWrapper.tnumber;
        ship.Location__c = shipWrapper.location;
        update ship;  

Edited code:

 public  with sharing class shipmentController {

public shipment__c ship {get;set;}
public String shipId {get;set;}
public shipWrapper wrapper { get; set;}

public class shipWrapper{
    public String tNumber {get;set;}
    public String location {get;set;}

public shipWrapper(Shipment__c ship){
    this.tNumber = ship.Tracking_Number__c;
    this.location = ship.location__c;
      }

  }
    public shipmentController() {
     wrapper = new shipmentWrapper(ship);
    shipId = ApexPages.currentPage().getParameters().get('shipId');
    if (shipid == null) {
        ship = new Shipment__c();
    return;
}

ship = [select id, location__c, tracking_number__c
                from shipment__c
                where id = :shipid];
}
public PageReference prepareShipment() {
    ship.tracking_Number__c = Wrapper.tnumber;
    ship.Location__c = Wrapper.location;
    update ship;  

VF snippet:

   <apex:page  controller="shipmentController" showHeader="true" 
   sidebar="true">
   <apex:form >
   <apex:pageBlock Title="edit Shipment">
   <apex:pageMessages />
<apex:pageBlockSection Title="Shipment Information" columns="2">
<apex:inputText label="First Name" value="{!wrapper.tNumber}" 
 id="fname"/>
   <apex:inputText label="Last Name" value="{!wrapper.location}" 
  id="lname"/>
</apex:pageBlockSection>

  • Theres a reason for this, and it gets more complicated but in short We have a lot of intergration with out apps and stuff that I just need to set variables this way – adming32442 May 23 '18 at 2:03
  • Yes I know there is an easier way but there is other things that I can't fully state right now but thank you – adming32442 May 23 '18 at 2:41
1

You need a public variable that you can reference:

public shipWrapper wrapper { get; set; }

Which you initialize in your constructor:

public shipmentController() {
   ...
   wrapper = new shipmentWrapper(ship);
}

And then can access in prepareShipment:

public PageReference prepareShipment() {
    ship.STracking_Number__c = wrapper.tNumber;
    ...
}

Edit: You need to load the record, then initialize the wrapper:

if(shipid == null) {
    ship = new Shipment__c();
} else {
    ship = [select id, location__c, tracking_number__c
                   from shipment__c
                   where id = :shipid];
}
wrapper = new ShippingWrapper(ship);
  • I'm getting this error on my VF: Attempt to de-reference a null object after doing this – adming32442 May 23 '18 at 2:40
  • I have added my vf code and added class with your comments – adming32442 May 23 '18 at 3:01
  • I actually did initialize it, right under public shipmentController() { – adming32442 May 23 '18 at 3:06
  • @kh9876 Order of operations is important. You have to load the ship record first. I misread your code. – sfdcfox May 23 '18 at 3:07
  • You right, thanks it worked! will definitely let you know when I stir away from making it this hard. – adming32442 May 23 '18 at 3:09

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.