I am beginner in Salesforce and I have to verify if the field Order.ShippingCountry
is filled using the OrderId
.
I would like to reuse this method below to fetch the Order.ShippingCountry
. But I was wondering if it is a good practice to use Order
as a return. Ideally, do I have to create another method to return a List<Order>
?
public class OrderSelector {
public static Order getOrderAndOrderItemsById(Id orderId){
return [SELECT Id,OwnerId,EffectiveDate,ShippingCountry,
AccountId,Account.Name
(SELECT Id, Quantity, UnitPrice,
Product2.ProductCode,Product2.Name,Product2.Family
FROM OrderItems)
FROM Order
WHERE Id = :orderId];
}}
Using the current version of the method above, my method will be like it:
public class ValidateOrder {
public class ValidateOrderException extends Exception{}
private static void validateShippingCountryByOrderId(Id orderId) {
Order varOrder = OrderSelector.getOrderAndOrderItemsById(orderId);
if(varOrder.ShippingCountry.isEmpty()){
throw new ValidateOrderException(Label.InvalidateDeleteDocument);
}
}
}
Is my code following best practices? Or would it be better to apply List<Order> varOder
? Also, as a good practice, would it be better to create another method in OrderSelector
class to fetch only the Order.ShippingCountry
and not all these fields?