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
JayNicJayNic 

Using standard getters and setters assistance

Hi,

 

I am attempting to use standard getters and setters in apex for some re-usable methods, but I'm having a little concern over whether or not I am doing it correctly... Please see the following code

public with sharing class shipment_ext{
/*
Logic extenders for the shipment object 
*/
    ////////////// GET_LINE_ITEMS //////////////   
    //Get a map of Shipemnt Id to a list of its child Outbound Line Items by passing in Shipment Ids
    map<id,list<ShipmentLineItemOutbound__c>> outboundLineItems;
    //Generic setter
    public void setOutboundLineItems(map<id,list<ShipmentLineItemOutbound__c>> pMap) {
        this.outboundLineItems = pMap;
    }
    //Get a map of Shipments to outbound line items by passing in a set of shipment ids
    public map<id,list<ShipmentLineItemOutbound__c>> getOutboundLineItems(set<id> pIds) {
        if(this.outboundLineItems == null) {
            map<id,list<ShipmentLineItemOutbound__c>> shpos = new map<id,list<ShipmentLineItemOutbound__c>>();
            if(!pIds.isEmpty()) {
                for(ShipmentLineItemOutbound__c shpo : [SELECT id, Shipment__c FROM ShipmentLineItemOutbound__c WHERE Shipment__c IN :pIds]) {
                    if(shpos.get(shpo.Shipment__c) == null) {
                        shpos.put(shpo.Shipment__c , new list<ShipmentLineItemOutbound__c>());
                    }
                    shpos.get(shpo.Shipment__c).add(shpo);
                }
            }
            this.setOutboundLineItems(shpos);
        }
        return this.outboundLineItems;
    }
}

 

 

Is this best practice?

There is a line in the Apex Dev guide that says:

  • We recommend that your get accessor should not change the state of the object that it is defined on.

I don't know what that means... 

But if I am doing it, how would I get around it?

 

the idea is that I will eventually overload the method eg:

public map<id,list<ShipmentLineItemOutbound__c>> getOutboundLineItems() {
    set<id> ids = new set<id>();
    for(Shipment__c s : [SELECT id FROM Shipment__c LIMIT 10000]){
        ids.add(s.id);
    }
    return getOutboundLineItems(ids);

}

 

Is there a best practice way for me to write this that I am not following?

 

Thanks

JohnSchultzJohnSchultz

I think the idea behind that best practice would be that you would do the initialization of outboundLineItems in your constructor (or in another method that the constructor calls). At least that's how I interpret that line in the developer's guide.

JohnSchultzJohnSchultz

Your class could then look like:

public with sharing class shipment_ext{
/*
Logic extenders for the shipment object 
*/
    ////////////// GET_LINE_ITEMS //////////////   
    //Get a map of Shipemnt Id to a list of its child Outbound Line Items by passing in Shipment Ids
    map<id,list<ShipmentLineItemOutbound__c>> outboundLineItems;
    //Generic setter
    public void setOutboundLineItems(map<id,list<ShipmentLineItemOutbound__c>> pMap) {
        this.outboundLineItems = pMap;
    }
    //Get a map of Shipments to outbound line items by passing in a set of shipment ids
    public map<id,list<ShipmentLineItemOutbound__c>> getOutboundLineItems(set<id> pIds) {
        return this.outboundLineItems;
    }
    
    public shipment_ext() {
        initOutboundLineItems();
    }
    
    private void initOutboundLineItems() {
        if(this.outboundLineItems == null) {
            map<id,list<ShipmentLineItemOutbound__c>> shpos = new map<id,list<ShipmentLineItemOutbound__c>>();
            if(!pIds.isEmpty()) {
                for(ShipmentLineItemOutbound__c shpo : [SELECT id, Shipment__c FROM ShipmentLineItemOutbound__c WHERE Shipment__c IN :pIds]) {
                    if(shpos.get(shpo.Shipment__c) == null) {
                        shpos.put(shpo.Shipment__c , new list<ShipmentLineItemOutbound__c>());
                    }
                    shpos.get(shpo.Shipment__c).add(shpo);
                }
            }
            outboundLineItems = shpos;
        }
    }
}