Power Automate實用常見問題解答(FAQ)


我是微軟Dynamics 365 & Power Platform方面的工程師羅勇,也是2015年7月到2018年6月連續三年Dynamics CRM/Business Solutions方面的微軟最有價值專家(Microsoft MVP),歡迎關注我的微信公眾號 MSFTDynamics365erLuoYong ,回復395或者20200224可方便獲取本文,同時可以在第一間得到我發布的最新博文信息,follow me!

Dynamics 365 Customer Engagement已經強烈推薦使用Power Automate代替Workflow,我這篇文章主要收集常見的問題做解答。

1. Power Automate如何調用Dynamics 365 Customer Engagement中的操作(Action)?

可以登錄 https://make.powerapps.com/ 或者 https://flow.microsoft.com ,打開左邊的Solutions ,新建或者打開已有Solution。

 

 

 然后新建Flow。

 

 

調用操作時候,注意Connector 要選擇 Common Data Service (current environment ).

 

 然后可以選擇 Perfrom a bound action 或者 Perform an unbound action 進行調用即可。如果選不到,確認action已經激活,然后看看 https://***.crm.dynamics.com/api/flow/openapi 這個url訪問是否報錯。

 

 

2. 如何獲取選項集字段的顯示文本。

我開始以為直接查詢的時候獲取不到,以為只能獲取到存儲值,后來經高人指點是可以的。

之前我的方法很笨,就是再查詢一次stringmap實體,比如 https://***.crm.dynamics.com/api/data/v9.1/stringmaps?$filter=objecttypecode eq 'ly_demo' and attributename eq 'statuscode' and langid eq 1033 and attributevalue eq 1 。

更好的辦法是直接從trigger返回的body中獲取值,注意選項集字段若為statuscode,需要寫成 _statuscode_label。

需要使用表達式輸入,例如: triggerOutputs()?['body/_lvo_addresschangetype_label'] ,當然不要全部手寫,可以選一個字段值,然后選擇 Peek Code,查看使用的表達式,然后自己用表達式來輸入,修改自動生成的表達式為自己想要的字段值即可。

如果是通過 Get a record等查詢方式,請通過類似 outputs('Your_Action_Name')?['body/ly_optionsetfieldname@OData.Community.Display.V1.FormattedValue'] 的表達式來獲取。

 

 3.如何獲取當前環境的base url。

這個url很重要,我們在郵件或者別的地方插入鏈接讓用戶可以點擊打開Dynamics 365的記錄就需要這個。

參考文章 Quick Tip: CDS Base URL in Flow 可以知道從前面的查詢中獲取body中的@odata.id元素的值,然后分割下獲取第一元素值即可,表達式類似:

first(split(outputs('Get_Creators_primary_emailaddress')?['body']['@odata.id'],'/api/')) 。

或者

first(split(outputs('Get_Creators_primary_emailaddress')?['body']['@odata.context'], '/api/'))

我一般用一個變量將這個值保存起來,假設變量名是CurrentEnvBaseUrl ,那么插入url使用的表達式就類似:

concat(variables('CurrentEnvBaseUrl'),'/main.aspx?pagetype=entityrecord&etn=ly_demo&id=',triggerOutputs()?['body/ly_demoid'])

Power Automate中可以用到的函數參考文檔:Reference guide to using functions in expressions for Azure Logic Apps and Power Automate

Dynamics 365 Customer Engagement通過URL打開記錄,窗體,視圖等請參考官方文檔: Open forms, views, dialogs, and reports with a URL  

4.如何查詢的時候同時獲取查找字段的其他信息(含主屬性信息)。

使用Common Data Service (current environment) 這個connector中的Get a record或者List records查詢記錄的時候,默認情況下對於查詢字段只返回類似如下的三個屬性:

  "_modifiedby_value@Microsoft.Dynamics.CRM.lookuplogicalname": "systemuser",
  "_modifiedby_value@odata.type": "#Guid",
  "_modifiedby_value": "21c43fb9-f446-ea11-a816-000d3a3788d7",

 

如果我要獲取查找字段的主屬性值甚至其他值呢?如果我要獲取modifiedby這個人主屬性fullname的值甚至其郵箱呢?當然,通過再一次查詢肯定可以。

我這里介紹的是另外一種方法,原理可以參考我的文章 Dynamics 365 We API ODATA語法根據父記錄查詢子記錄,根據子記錄查詢父記錄(附上根據團隊,隊列名稱查成員)

具體設置類似如下:

 

 然后你可能會問如何獲取這個值?對於使用Get a record,類似的表達式是 outputs('Get_a_record')?['body/modifiedby']['internalemailaddress'] 

對於使用List records,類似的表達式是 items('Apply_to_each')?['modifiedby']['internalemailaddress'] 。

也不用特別記憶,看自動生成的語法是什么樣的,自己修改下即可。

5.能調用自定義工作流活動嗎?

我認為當前不能,不代表后續不能。 對比可以參考官方文檔,https://docs.microsoft.com/en-us/power-automate/replace-workflows-with-flows ,Power Automate的功能在不斷增強。

6.能發送郵件並顯示在D365中的Timeline嗎?

在Power Automate中可以通過Outlook這個Connector來發送郵件,但是這種郵件與Dynamics 365的關系不大。有些時候我們希望能調用Dynamics 365中Workflow那種發送郵件的能力,發送的郵件能和D365的記錄關聯,並展示在關聯記錄的Timeline中。答案是可以的,詳情可以參考 Send Dynamics 365 Emails from Power Automate  或者 Creating and Sending an Email from Dynamics 365 using Power Automate。我總結下重點,也是創建 Email Messages 這個實體記錄,收件人可以通過 Activity Party Attribute Name 設置為To,然后在其對應的 Activity Party Attribute Value中設置值來設置,可以設置為類似 /contacts(@{triggerOutputs()?['body/contactlid']}) 這種。郵件的主題當然是Subject字段,內容是Description字段,關於字段會出現很多類似 Regarding (Opportunities) 的字段,找到你要管理的實體類型,設置為類似 /contacts(@{triggerOutputs()?['body/contactlid']}) 這種格式。發送郵件是通過 Common Data Service (current environment ) 這個Connector通過 Perform an bound action來做的,調用的Action Name是SendEmail,參考下圖。如果要將收件人設置為文本字段的值,可以參考我這個博文 Dynamics 365中使用工作流發郵件讓其可以發往文本字段指定的郵箱 注冊一個插件來實現。

 

 7.有類似child flow的功能嗎?

答案是有,2019年10月份GA的,可以參考官方文檔 Call Child Flows ,博文可以參考 October Updates for Microsoft Flow

一般對於child flow我選擇Power Apps作為觸發器,如果child flow有輸出參數,那么它的最后一個action一般是Respond to a Power App or flow 或者 Response。

parent flow調用child flow的時候選擇Flows連接器下面的 Run a Child Flow 這個action來調用。

 8.異常處理一般如何做?

可以參考 Advanced Error Handling With Power Automate , Microsoft Flow Error HandlingTry-Catch pattern in Microsoft Flow  。

主要使用result函數,函數參考見 Reference guide to using functions in expressions for Azure Logic Apps and Power Automate

參考官方文檔:Handle errors and exceptions in Azure Logic Apps  。

還需要考慮一些limit,官方文檔請參考:Limits and configuration information for Azure Logic Apps 

我的如下可以供參考:

用到的表達式分別是:

result('Main_Scope')

createArray('Failed', 'TimedOut')

item()['status'])

body('Filter_array')

items('Apply_to_each_result')['name']

items('Apply_to_each_result')['status']

items('Apply_to_each_result')['code']

items('Apply_to_each_result')['outputs']['body']['error']['message']

 

 

 9.查詢CDS步驟如何判斷是否返回了記錄?

可以使用類似的表達式 length(body('Query_Accont')?['value']) 來判斷,其中Query_Accont是Common Data Servie (current)這個connector的List records查詢步驟的名稱,你輸入名稱中的空格請以_替換。比如查詢步驟名稱你更改為了Query Account,那么用的時候是用Query_Accont 。

 10.利用Trigger Conditions拆分flow,減少flow的運行

我們做的時候常見的觸發條件是字段的變更,這樣做的話,一般字段變化就會觸發一次。如果可以在字段變更后,自己可以加一些條件來限制flow是否運行就更好了,減少flow的運行次數。

如果flow分支多,比較復雜,維護起來就比較麻煩,可以直接拆分flow稱為多個,根據不同的條件觸發不同的flow呢?

利用Trigger Conditions是可以做到的,可以參考 Power Automate Trigger Conditions made EASY 和 Microsoft Power Automate Flow Trigger Conditions

就是在trigger中點擊如下右上角的【...】符號,在出來的菜單中選擇 【Settings】。

 

在Trigger Conditions文本輸入框中輸入表達式,類似 @equals(triggerBody()?['_ly_accountid_value'],'a6d94ece-384c-ea11-a812-000d3a378f47') 這種,然后點擊【Done】按鈕。

 

這個Trigger Condition可以加多行的,如果某行中要判定是某個值中的一個,可靠考慮類似下邊寫法:

@contains(createArray('1','552590009','552590006','552590007'), string(triggerBody()?['statuscode']))

 11.判斷列表是否包括/不包括某個值

可以使用createArray函數放在判斷的左邊,中間的操作符使用 contain 或者 does not contain ,右邊是要比較的值或者表達式。比如我下圖用的是:

表達式分別是:

createArray('CHINA','JAPAN','INDIA')

toUpper(outputs('Get_request_info')?['body/ly_countryname'])

  

12.我只需要查詢結果的第一行記錄進行處理如何寫表達式避免循環?

前面步驟使用Common Data Service (Current) 這個Connector 的List records這個action做查詢,並且指定了Top Count 為1,也就是會最多返回一條記錄。

后面的步驟可以用這樣的表達式 outputs('retrieve_config_entity_of_myconfigur')?['body/value'][0]['ly_value']  來獲取返回結果中的某個字段的值。

 

13. 多種方式可以訪問Common Data Service服務,推薦哪種?

推薦使用 Common Data Service (current environment) connector ,官方文檔描述如下,鏈接為 Create an automated flow by using Common Data Service (current environment)

 Important

There are three connectors available to connect to Common Data Service. This article covers the recommended Common Data Service (current environment) connector for connecting to Common Data Serivce. The Common Data Service connector and the Dynamics 365 connector are also available for use if you cannot use the recommended connector.

Common Data Service (current environment) connector 沒有Throttling Limits章節,而Common Data Service connector則有,如下圖所示:

 

 

 不過截至2020-07-26,Common Data Service (current environment) connector 並不支持中國大陸的雲服務。

 

 

14. flow運行慢,如何排查?

請參考官方文檔:Troubleshooting Slow Running Flows

 

15. 可以通過Web API來查詢flow的信息嗎?

答案是可以的,請參考官方文檔:Power Automate Web API ,比如查詢目前開啟的flow:

GET https://luoyongdemo.crm5.dynamics.com/api/data/v9.1/workflows?$filter=category eq 5 and statecode eq 1

 

16. 判斷字符串為空后傳遞null值的表達式如何寫?

請參考下面寫法,因為如果是CDS更新記錄,對於查找字段,傳遞空字符串會報錯,可以傳遞null。

If(equals(variables('CurrencyId'),''),null,variables('CurrencyId'))

 

17. 針對日期時間字段進行查詢

可以參考 Advanced | Flow of the Week: Filtering Data with OData ,采用類似如下的Filter Query:

ly_orderdate gt addDays(utcnow('yyyy-MM-ddTHH:mm:ssZ'),-30)

 

18. 如何搜索flow的運行記錄(run history)

請參考官方博文: Download flow run history, build advanced recurring schedules and more . 也就是在查看All run history的時候,有 Download CSV功能,不過一次最多導出100個run history.

 

19.Canvas App中調用flow傳遞參數的常用方法

最好是傳遞Json數據,我這里提供我常用的方法,調用flow使用類似的代碼:

ClearCollect(
    colFilter,
    {
        StartDate: dpStartDate.SelectedDate,
        EndDate: dpEndDate.SelectedDate
    }
);
ClearCollect(
    colAggregate,
    GetEntityRecordCountFlow.Run(JSON(First(colFilter))).outResult
);

 

在flow里面添加一個Data Operation 下面的Parse JOSN action,類似如下,Content通過【Ask in PowerApps】來添加,然后點擊【Generate from sample】按鈕來生成Schema。

 

 

下圖是一個完成設置的例子。

 

 

可以看到后面步驟可以通過Dynamic content來獲取解析出來的輸入參數的值。

 

 

20.Canvas App中調用flow獲得JSON返回值的常用方法

在flow的最后步驟,添加一個Request 中的 Response 這種action。

 

 

 

 

記得將Status Code設置為200,Body設置為要返回的JSON內容,然后一定要點擊 【Show advanced options】,再點擊【Generate from sample】按鈕來生成Schema。這樣就可以了。

 

21.轉義Dataverse中多行文本中的換行符的方法

發郵件如果包括了多行文本字段的值,會發現換行丟了,如果要保留的話,參考下面的表達式。

uriComponentToString(replace(uriComponent(triggerOutputs()?['body/lvo_commentsdetails']),'%0A','<br/>'))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM