You need to sign in to do that
Don't have an account?
Marcel 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
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.
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.
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]);
Check on my debug log to verify what I'm saying, let me know if you can see it well.
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:
Thank you!
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);
}
}