In continuation to my previous post Close D365 Quote Using Power Automate let us see how to automatically close an opportunity in D365 on closure of Quote.
Before jumping on to the flow, let us gather some pre-requisite information regarding quote status and status reason.
Create a flow with triggering point as Quote Update
Check if status & status reason of the quote using condition component. In the below example i am checking if the quote is ‘closed’ as ‘cancelled’
If the above condition is true, let us get the related opportunity of the quote (user Get Record action). Choose Item Identifier = Opportunity (from “When Quote is Updated” action)
In this current example, we might assume single quote and single opportunity. In case of bulk operations if an opportunity has multiple open quotes and it is best to always check if there are any other open quotes left before closing the opportunity.
We should close the opportunity only if there are no active quotes (or draft quotes). Let us see how to check if there are any open quotes for the opportunity. To check this, let us get all the quotes for the opportunity using “List items” action.
The filter query to get this:
statecode eq 1 and opportunityid/opportunityid eq <<Opportunity>>
Choose opportunity from “Get Opportunity” action
The above will help us to get all the active quotes related to the opportunity. These should ideally be equal to zero. Let us check that condition using “Condition Action”. Using the “List Related Quotes” let us count the number of records using the following expression
In addition, I am also checking if the Opportunity (from “Get Opportunity”) status is Open
If both the defined conditions are true, let us define an action to close the opportunity i.e. Create Opportunity Close.
Choose related opportunity from “Get Opportunity”.
Closed On from “When Quote is Updated”
Status and Status reason as desired based on requirement. (i chose as lost and cancelled respectively)
To conclude we did the following:
- Triggered on Quote Update
- Checked if Quote is Closed
- Retrieved the Opportunity
- Checked if there are any active quotes
- Then Closed the Opportunity by creating Opportunity Close