Last Successful Login on Contact record - PowerApps Portal Custom Code


Updating the Last Successful Portal Login on Contact record by using Custom Code

 

I understand that this feature is deprecated and should be replaced with Azure Application Insights.


However, we still require the ability to search by Last Login date using Advanced Find (e.g., "look for contacts where last login is today"). This worked well with the now deprecated feature but (I think) can't be done using Application Insights because the data isn't stored on the contact record.


 

I have implemented the functionality by using the Portals Content snippet to add jQuery code and calling Power Automate Flow in the Portal Sign-in Page to update the Last Login field to (almost) replicate the old behavior.  


 

How it works:
               In the below content snippet code, we will get username which user enters in the textbox and after clicking on sign in button then it triggers a power automated flow from content snippet and it will update the contact record with current time and date.

 

 

Step 1:  We need to create a Content Snippet like below

 

·       Open the Portal Management app.

·       Go to Portals > Content Snippets.

·       To create a new snippet, select New.

·       Enter name as: Account/SignIn/PageCopy

·       Select your website.

·       Enter Display Name:  Use same name

·       Select type as HTML for this example. You can also select text instead.

·       Select a language.

 


 


Value:
In this you need to copy paste the below jQuery code to call power Automate Flow

 

<script>

    $(document). ready(function () {// To Load the document

        $("#submit-signin-local"). click(function () {

            username = $("#Username").val();// To get user name in the sign in page

            if ($("#Username").val() != '' && $("#PasswordValue").val() != '')

{// checking user deatils

                //Make username as a string object.

                var userName = '{"userName": "' + username + '"}';

 

                var req = new XMLHttpRequest();

                var url = HttpFlowUrl;//Here you need to copy paste the flow URL from the below flow..

                req.open("POST", url, true);

                req.setRequestHeader('Content-Type', 'application/json');

                req.send(userName);

              }

        });

       });

</script>

 

Here is the code Screenshot:

 


 

Step 2:  We need to create Power Automate Flow to Update a contact record

 

Select trigger as when a HTTP request is Received

 



Request Body JSON Schema add this below schema:


{

    "type""object",

    "properties": {

        "userName": {

            "type""string"

        }

    }

}


In the next stage we need to get contacts and filter the contact who is logging into the portal by using username.

 


 

In the next step loop the above contacts and update the filtered contact record.

 


In the contact entity I want to update a custom date field, Which I have created in the contact entity as below.

 


Here Last logged in is custom date field updating with current date and time.

By this whenever a user logged into the portal, it will trigger the flow and update the contact.


In the next Article Handling the Http URL by using Environment Variable, without changing the content snippet code in another environment just update the environment variable in the exported environment.

 

 


Thank You………. cheers








Comments

Post a Comment

Popular posts from this blog

Get Dynamics 365 free trial

Last logged in Capture B2C Authentication in Portal Custom Code