Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Null Reference Exception when cloning existing Sales Order

(0) ShareShare
ReportReport
Posted on by 2
We have a business need to clone existing sales orders in Dynamics 365 Sales Hub. I created a new button in the main grid command bar for the 'Orders' entity called 'Clone Order' and added a new component library that holds some Power Fx code that is triggered when clicking this button. (Code is below. I am not a developer, so please let me know if there is a better way to accomplish this.)

The button works as intended as long as our custom 'Created By' field has a value. This is a lookup field to the 'AAD User' table. We use this field for reporting as well as to filter by user for manager order approval. We went this route as the majority of our employees do not have Dynamics licenses. They create and submit sales orders through a custom Canvas app, and that app grabs the user and does a lookup against the 'AAD User' table to populate the 'Created By' field.
 
The problem comes in when an order created inside Dynamics is cloned and 'Created By' is blank as it is not a required field (although we could make it one). When this field is blank and we click 'Clone Order', we get the following error:
 
/Business Process Error: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Dynamics.CDS.AADPlugins.GraphApiClient.ExecuteGraphApiRequest/
 
I don't know where to go from there. I have tried various If() statements evaluating if the field is blank, but it hasn't worked. One workaround is abandoning the Power Fx code and triggering a Power Automate Flow with a new column called 'Clone Trigger' (or something). However, I like the Power Fx route better if it's possible to get around the error. 
 
Any help is appreciated!
 
If(    CountRows(Self.Selected.AllItems) = 1,    Notify(/The selected order is being cloned./),    Notify(/The selected orders are being cloned/));ForAll(    Self.Selected.AllItems As selectedOrder,    With(        {            clonedOrder: Patch(                Orders,                Defaults(Orders),                {                    Name: Left(                        selectedOrder.Name,                        (Len(selectedOrder.Name) - 5)                    ) & RandBetween(                        10000,                        99999                    ),                    Customer: AsType(                        selectedOrder.Customer,                        Accounts                    ),                    'Price List': selectedOrder.'Price List',                    Owner: selectedOrder.Owner,                    'Created By': If(                        IsBlank(selectedOrder.'Created By'),                        Blank(),                        selectedOrder.'Created By'                    )                }            ),            selectedOrderProducts: Filter(                'Order Products',                Order.Order = selectedOrder.Order            )        },        ForAll(            selectedOrderProducts As selectedOrderProduct,            Patch(                'Order Products',                Defaults('Order Products'),                {                    Quantity: selectedOrderProduct.Quantity,                    'Price Per Unit': selectedOrderProduct.'Price Per Unit',                    'Existing Product': selectedOrderProduct.'Existing Product',                    'Manual Discount': selectedOrderProduct.'Manual Discount',                    Description: selectedOrderProduct.Description,                    Order: clonedOrder,                    Unit: selectedOrderProduct.Unit,                    'Product type': selectedOrderProduct.'Product type',                    'Property Configuration': selectedOrderProduct.'Property Configuration',                    Name: selectedOrderProduct.Name,                    Pricing: selectedOrderProduct.Pricing                }            )        )    ));If(    CountRows(Self.Selected.AllItems) = 1,    Notify(/The selected order has been cloned successfully./),    Notify(/The selected orders have been cloned successfully./))
 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,387 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans