Skip to main content

Notifications

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 671 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans