+ Start a Discussion
Marcel GómezMarcel Gómez 

Set Order.BillingCity on test method not working !!!!

Hi everybody,
I'm having an issue when testing a simple class that is intended to copy the Billing and Shipping Addresses from an Order object to a custom object. The thing is, I set address fields on the Order, insert the Order and everything is fine. But later, when I perform a Select query on the same Order, Address fields are not returned on the result object.

Here is my code
 
Order order = new Order();

		order.BillingCity 		= 'bcity';
		order.BillingCountry 	= 'bcountry';
		order.BillingPostalCode = 'bpostalcode';
		order.BillingState 		= 'bstate';
		order.BillingStreet 	= 'bstreet';
		
		order.ShippingCity 		= 'scity';
		order.ShippingCountry 	= 'scountry';
		order.ShippingPostalCode= 'spostalcode';
		order.ShippingState 	= 'sstate';
		order.ShippingStreet 	= 'sstreet';

		order.AccountId 		= account.Id;
		order.OpportunityId	= opp.Id;
		order.Status 			= status;

		order.EffectiveDate 	= Date.today();
		order.EndDate 		= Date.today().addDays(365);

		insert order;

		system.debug('---------------------------- Order: ' + order);
		system.debug('---------------------------- Query Order: ' + [select 
         																BillingAddress,
         																ShippingAddress,

         																BillingCity,
         																ShippingCity,

         																AccountId,
         																Status,
         																EffectiveDate,
         																EndDate
     																FROM Order
     																WHERE Id =: order.Id]);

if you execute this code, you'll see that the first system.debug invokation will show the Order object as it is in memory, while the second system.debug line, will show the Order with all fields set but those related to Billing and Shipping Addresses (BillingCity, ShippingStreet, etc), also fields BillingAddress and ShippingAddress are null.
How is that possible? Is there any workaround for this situation?
My main goal is been able to assert that Order.BillingCity == CustomObject.BillingCity__c.

Thank you so much for your time.
Karan Shekhar KaulKaran Shekhar Kaul
Address fields returns a object rather than text. May be explicitylt use address field in query.


Order order = new Order();

        order.BillingCity         = 'bcity';
        order.BillingCountry     = 'bcountry';
        order.BillingPostalCode = 'bpostalcode';
        order.BillingState         = 'bstate';
        order.BillingStreet     = 'bstreet';
        
        order.ShippingCity         = 'scity';
        order.ShippingCountry     = 'scountry';
        order.ShippingPostalCode= 'spostalcode';
        order.ShippingState     = 'sstate';
        order.ShippingStreet     = 'sstreet';

        order.AccountId         = account.Id;
        order.OpportunityId    = opp.Id;
        order.Status             = status;

        order.EffectiveDate     = Date.today();
        order.EndDate         = Date.today().addDays(365);

        insert order;

        system.debug('---------------------------- Order: ' + order);
        system.debug('---------------------------- Query Order: ' + [ Select BillingCity,BillingPostalCode ,BillingStreet,BillingState         ,BillingCountry,ShippingCity,ShippingStreet,ShippingCountry,AccountId,Status,EffectiveDate,EndDate FROM Order
                                                                     FROM Order
                                                                     WHERE Id =: order.Id]);
Marcel GómezMarcel Gómez
Hi Karan, thank you for your reply. If you noticed it, I'm actually querying the BillingCity and ShippingCity of the Order, but in the 2nd debug line, the Order does not have a value on mentioned fields (BillingCity and ShippingCity). Try that out on a test method and see the debug outputs to verify what I'm saying. Thanks again.
Karan Shekhar KaulKaran Shekhar Kaul
Are you inserting account & opportunity as well in test method. Would be good if you can paste whole test class. I tried it in my org.Works for me
Marcel GómezMarcel Gómez
Yes Karan, I'm setting both the Account and Opportunity of the Order. I'm curious now that you said it works on your org.
Check on my debug log to verify what I'm saying, let me know if you can see it well.

User-added image

Notice that the second line corresponds to the second debug statement, which is the data retrieved from database. BillingAddress and ShippingAddress are NULL, meaning that setting the address "sub-fields" does not alter the Address compound field. On the other hand, I'm setting the address "sub-fields" but in the record retrieved from database those fields are not even present. How is that possible?

Check on my test class:
@isTest
public with sharing class OrderConversionUtility_Test {

 static Order createOrder(Account account, Opportunity opp, String status) {
    Order order = new Order();

    order.BillingCity     = 'bcity';
    order.BillingCountry   = 'bcountry';
    order.BillingPostalCode = 'bpostalcode';
    order.BillingState     = 'bstate';
    order.BillingStreet   = 'bstreet';
    
    order.ShippingCity     = 'scity';
    order.ShippingCountry   = 'scountry';
    order.ShippingPostalCode= 'spostalcode';
    order.ShippingState   = 'sstate';
    order.ShippingStreet   = 'sstreet';

    order.AccountId     = account.Id;
    order.OpportunityId    = opp.Id;
    order.Status       = status;

    order.EffectiveDate   = Date.today();
    order.EndDate       = Date.today().addDays(365);

    insert order;
    
    //in this line Order.BillingCity = 'bcity'
    system.debug('---------------------------- Order: ' + order);

    //in this line Order.BillingCity = NULL (WHY?)
    system.debug('---------------------------- Query Order: ' + [select 
                                         BillingCity,
                                         ShippingCity,

                                         AccountId,
                                         Status,
                                         EffectiveDate,
                                         EndDate
                                     FROM Order
                                     WHERE Id =: order.Id]);

    return order;
  }

@isTest
  static void test_ConvertOrder_To_SalesOrder() {
    Test.startTest();

    //assume these methods work fine
    Account account = createAccount();
    Opportunity opp = createOpportunity(account);

    Order order = createOrder(account, opp, 'Draft');

    Test.stopTest();
    
    //this method synchronize the Order Address with separated Address fields on a custom object
    Sales_Order__c salesOrder = 
    OrderConversionUtility.Sync_Order_SalesOrder(order);

    //this assertion fails because the Sync_Order_SalesOrder method (above) reloads the Order from database and the issue happens: Order.BillingCity = NULL
    system.assertEquals(order.BillingCity, salesOrder.BillingCity__c);

   }
}

Thank you!
Karan Shekhar KaulKaran Shekhar Kaul
Hmm.Strange. It works for me..I can see billing city..

Try using system.runas



@isTest
  static void test_ConvertOrder_To_SalesOrder() {
  system.runas(new user(id=userinfo.getUserId())){
      Test.startTest();

    //assume these methods work fine
    Account account = createAccount();
    Opportunity opp = createOpportunity(account);

    Order order = createOrder(account, opp, 'Draft');

    Test.stopTest();
    
    //this method synchronize the Order Address with separated Address fields on a custom object
    Sales_Order__c salesOrder = 
    OrderConversionUtility.Sync_Order_SalesOrder(order);

    //this assertion fails because the Sync_Order_SalesOrder method (above) reloads the Order from database and the issue happens: Order.BillingCity = NULL
    system.assertEquals(order.BillingCity, salesOrder.BillingCity__c);

  }

   }
Karan Shekhar KaulKaran Shekhar Kaul
User-added image