Showing posts with label Connection. Show all posts
Showing posts with label Connection. Show all posts

Friday, October 1, 2021

Power Automate: Send Mail With Other Account

 Hello Friends,

Welcome back with another post on Power Automate. Couple of days back, one interviewer asked me if we can send mail from Power Automate using mail account other than the creator of that Power Automate. I was not sure about that therefore I said that I don't know but as per my assumption, the person should be added as co-owner. 😞

Later on, I tried to find out if this could be possible. Surprisingly, I was correct but the answer was not complete. Because, first of all, the user should be co-owner. After then, in Connections, we have to switch the connection of outlook with that user's account connection. Let's try-

  1. First of all, create a simple list in SharePoint. I have created with name "SendOtherMail" having the default "Title" column.
  2. Now click on App Launcher (9 dots at top left corner) and click on Power Automate link.
  3. It will open the Power Automate portal. Click on + Create.
  4. Choose Automated cloud flow. Then provide a suitable name to the flow and choose trigger as When an item is created (SharePoint). Click on Create.
  5. It will create the flow and then ask you to choose the Site address and list name upon which it will work.
  6. Now add a step "Send an email (V2) Office 365 Outlook".
  7. Provide the input to the fields- "To", "Subject", "Body".
  8. If we execute the flow now, it will send email with the current owner capacity. But we want to send email from other account. So, we will save this flow as of now and will come back to it's details page by clicking on back arrow.
  9. Now click on Edit link of Owners section (#1 in below screenshot) to add a new co-owner.
  10. Search the new co-owner and add it.
  11. Then click on Manage Connections link in the Embedded connections section.
  12. It will load a page where all currently active connections will be displayed.
  13. If the added co-owner's outlook connection is getting displayed then click on ellipses (3 dots) ahead to that connection and choose Switch account. Otherwise click on + New connection link provided at the top and then choose Office 365 Outlook >> Create.
  14. It will the login credentials (Pick an account). Ask the user to provide credentials to create the connection.
  15. Once the connection get established, come back to the connections list.
  16. Look for the connection recently added and click on ellipses ahead of that connection. Choose Switch account from the popup list. When you click on Switch account, it will again show a popup where login accounts will be displayed. Choose the other user account.
  17. This way, outlook account has been switched.
  18. Now click on My flows >> Shared with me. As the flow has been shared with other user therefore, it will now appear in Shared with me tab.
  19. Click on the flow name and click on Edit.
  20. Expand the Send an Email action. Click on Show advanced options.
  21. Fill in the other user's email in From (Send as) input box and now Save the flow.
  22. Flow is now ready.
  23. Add an item in SharePoint list.
  24. Come back to Power Automate and check the Run history.
  25. As it can be seen that the flow has executed successfully.
  26. Now check the mail box.
  27. Hurray! The mail is received from other user's account. Now, if you ask the user to check his/her Sent Items in Outlook, he/she will find that the mail is reflecting in his/her Sent Items.
  28. This way, we can achieve this functionality.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

PowerApps: Working Offline

 Hello Friends,

Welcome back with another post on PowerApps. As we know, that PowerApps generally works in connected mode (internet connectivity). But sometimes, we face a situation where fields associates does not find any network connectivity in remote area. In such scenarios, this app does not work. For such scenarios, there is a workaround. We can check the network connectivity and if there is no network, then the app should remains working using mobile local cache and as soon as the network gets available, the app should sync data with data source. Now, the question is, how do we do it? Let's try-

  1. The steps we will follow are-
    1. The OnStart function of App will be used to check if the network connection is available. If Yes, we will load the data from data source and update the same in local cache otherwise we will load the data from local cache.
    2. We will add a simple Text box with a Button to insert new records. If the connection is available, it will be saved in data source otherwise it will be saved in local cache.
    3. We will add a Timer control which will repeatedly check if the connection is available or not. If available, it will sync the data saved in local cache to data source. 
  2. So, let's start-
  3. First of all, we will create a list and insert one record. I have created a list named "DataForOffilneApp" and inserted one record.
  4. Open the PowerApps platform and create a new PowerApp.
  5. By default we will get a blank screen provided in this app.
  6. Now click on Data link from left navigation and add the list we have created above.
  7. Click on App from Tree view and select the OnStart function.
  8. Now, write below code in the function box provided:-
  9. If(
        Connection.Connected,
        ClearCollect(
            collDataForOfflineApp,
            DataForOfflineApp
        ),
        ClearCollect(
            collDataForOfflineApp,
            []
        );
        LoadData(
            collDataForOfflineApp,
            "OfflineCacheData"
        );
        
    );
    ClearData("OfflineCacheData");
    SaveData(
        collDataForOfflineApp,
        "OfflineCacheData"
    );
    
  10. Let me explain this code (map the explanation with highlighted color)-
    1. First of all, we are checking if the network connection is available.
    2. If available, then we are saving the data from data source to a local collection.
    3. If not available then we are clearing the local collection and then we are loading the it with the data saved in local cache with key as "OfflineCacheData".
    4. Then we will clearing the local cache.
    5. Lastly, we will be saving (basically updating irrespective of connection available or not) the local collection data into cache.
  11. Now add a gallery and assign local collection "collDataForOfflineApp' as it's data source with Layout as "Title" only.
  12. As the App >> OnStart does not work in Desktop mode, therefore I have added a button and added the same code for OnSelect property of button.
  13. Now play the app and click on button to load the collection. Once done. Stop the play mode and come back to development mode.
  14. Here again click on gallery and click on Edit link for fields mapping. Map the filed Title to get displayed dat in gallery.
  15. One part of the functionality is completed. (I had changed the display text of button to Load Data)
  16. Now we will work on inserting the data in connected/disconnected mode.
  17. For this, we have added a label, textbox and a button.
  18. Add below code in function window for OnSelect property of Insert button.
  19. If(
        Connection.Connected,
        Patch(
            DataForOfflineApp,
            Defaults(DataForOfflineApp),
            {Title: TextInput1.Text}
        );
        ClearCollect(
            collDataForOfflineApp,
            DataForOfflineApp
        );
        Reset(TextInput1);
        ,
        Collect(
            colOfflineTempDataInsert,
            {Title: TextInput1.Text}
        );
        Reset(TextInput1);
        UpdateContext({varPostOfflineData: true});
        
    );
    
  20. Let's discuss the code-
    1. If network connection is available then insert the data directly into data source.
    2. Reload the local collection.
    3. If network connection is not available, then insert the data in a local collection which will be synced to data source once the network will be available.
    4. If network connection is not available, then set a local variable to true which will be used to check and sync the local collection to data source once the network will be available.
  21. This was the second part of functionality. Now start the third and last part - a timer which will sync the data upon network connection availability.
  22. Add a timer control. Set the Duration to 60000 miliseconds (by default it is set to the same value). Set "Repeat" and "Auto Start" property to true. Now place the below code in function box for OnTimerStart property-
  23. If(
        Connection.Connected,
        If(
            varPostOfflineData,
            UpdateContext({varPostOfflineData: false});
            ForAll(
                colOfflineTempDataInsert,
                Patch(
                    DataForOfflineApp,
                    Defaults(DataForOfflineApp),
                    {Title: Title}
                )
            );
            ClearCollect(
                colOfflineTempDataInsert,
                []
            );
        )
    )
    
  24. Let's explain-
  25. First of all we are checking if the network connection is available. If YES, then we are checking if we have something to sync (by checking the value of varPostOfflineData variable). If all result to true, then we reset the variable so that on next timer schedule, the process should get skipped as dat is going to sync here.
  26. Then we are inserting all the offline records to the data source and lastly, we are resetting the temp collection to blank.
  27. Now publish the app and execute on mobile.
  28. Currently, I am having one record. I will insert one more record in connected mode. Then I will disconnect the connection and insert one more record. Then I will re-establish the connection and wait for data to get sync. Let's try-
  29. Inserting one record in connected mode-

  30. Now, I had disconnected the network connection and inserted a record. It didn't get reflected on screen. Waited for the timer to get completed and restarted. Then I reconnected the network.
  31. As you can see the screenshot, the timer is at 29 seconds. I just waited for it's completion. The moment it gets completed and restarted, the sync process gets started automatically and the record, I had inserted during offline mode, gets inserted in data source.
  32. This way, we can achieve the offline mode functionality in PowerApps.
  33. However, I personally don't recommend it in case if the data source is having huge amount of data because the performance of App as well as the device will get affected once we save this huge data in local cache.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !