Update Contact address fields Associated to Accounts when account fields are updated using Plug-in.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace ClassLibrary3
{
public class Class2 : IPlugin
{

    public void Execute(IServiceProvider serviceProvider)
    {
        // Extract the tracing service for use in debugging sandboxed plug-ins.  
        // If you are not registering the plug-in in the sandbox, then you do  
        // not have to add any tracing service related code.  
        ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        // Obtain the execution context from the service provider.  
        IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));
        // Obtain the organization service reference which you will need for  
        // web service calls.  
        IOrganizationServiceFactory serviceFactory =
            (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
        // The InputParameters collection contains all the data passed in the message request. 

        if (context.MessageName.ToLower() == "update")
        {
            var target = context.InputParameters["Target"] as Entity;

            Entity eAccount = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("address1_line1", "address1_line2", "address1_line3", "address1_city", "address1_stateorprovince", "address1_postalcode", "address1_country"));
            string new2 = eAccount.Attributes.Contains("address1_city") ? eAccount.GetAttributeValue<string>("address1_city") : String.Empty;
            string new3 = eAccount.Attributes.Contains("address1_stateorprovince") ? eAccount.GetAttributeValue<string>("address1_stateorprovince") : String.Empty;
            string new4 = eAccount.Attributes.Contains("address1_line1") ? eAccount.GetAttributeValue<string>("address1_line1") : String.Empty;
            string new5 = eAccount.Attributes.Contains("address1_line2") ? eAccount.GetAttributeValue<string>("address1_line2") : String.Empty;
            string new6 = eAccount.Attributes.Contains("address1_line3") ? eAccount.GetAttributeValue<string>("address1_line3") : String.Empty;
            string new7 = eAccount.Attributes.Contains("address1_postalcode") ? eAccount.GetAttributeValue<string>("address1_postalcode") : String.Empty;
            string new8 = eAccount.Attributes.Contains("address1_country") ? eAccount.GetAttributeValue<string>("address1_country") : String.Empty;

            string FetchAssociatedContacts = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                                       "<entity name='contact'>" +
                                       "<attribute name='fullname'/>" +
                                     "<attribute name='contactid' />" +
                                       "<attribute name='address1_line1' />" +
                                        "<attribute name='address1_line2' />" +
                                         "<attribute name='address1_line3' />" +
                                          "<attribute name='address1_postalcode' />" +
                                          "<attribute name='address1_city' />" +
                                          "<attribute name='address1_stateorprovince' />" +
                                          "<attribute name='address1_country' />" +
                                       "<order attribute='fullname' descending='false' />" +
                                       "<filter type='and'>" +
                                       "<condition attribute='parentcustomerid' operator='eq' value='" + target.Id + @"'/>" +
                                       "</filter>" +
                                       "</entity>" +
                                       "</fetch>";


            EntityCollection AssociatedContacts = service.RetrieveMultiple(new FetchExpression(FetchAssociatedContacts));
            if (AssociatedContacts.Entities.Count>=0)
            {
                foreach (var item in AssociatedContacts.Entities)
                {
                    Entity Company = new Entity(item.LogicalName,item.Id);
                    Company["address1_line1"] = new4;
                    Company["address1_line2"] = new5;
                    Company["address1_line3"] = new6;
                    Company["address1_postalcode"] = new7;
                    Company["address1_city"] = new2;
                    Company["address1_stateorprovince"] = new3;
                    Company["address1_country"] = new8;
                    service.Update(Company);
                }

            }

        }

    }

}

}

Update Contact address fields Associated to Accounts when account fields are updated using Java Script.

function UpdateAddress(executionContext){
var formContext = executionContext.getFormContext();
var Add1=formContext.getAttribute(“address1_line1”).getValue();
var Add2=formContext.getAttribute(“address1_line2”).getValue();
var Add3=formContext.getAttribute(“address1_line3”).getValue();
var city=formContext.getAttribute(“address1_city”).getValue();
var state_province=formContext.getAttribute(“address1_stateorprovince”).getValue();
var postal=formContext.getAttribute(“address1_postalcode”).getValue();
var country=formContext.getAttribute(“address1_country”).getValue();

var GUID=formContext.data.entity.getId();

Xrm.WebApi.online.retrieveMultipleRecords(“contact”, “?$filter=_parentcustomerid_value eq ‘” + GUID + “‘”).then(
function success(results) {
for (var i = 0; i < results.entities.length; i++) {
var contactid = results.entities[i][“contactid”];
var data =
{
“address1_line1”: Add1,
“address1_line2”: Add2,
“address1_line3”: Add3,
“address1_city”: city,
“address1_stateorprovince”: state_province,
“address1_postalcode”: postal,
“address1_country”: country
}

      Xrm.WebApi.online.updateRecord("contact",contactid, data).then(
      function success(result) {
      Xrm.Utility.alertDialog("Data Updated");
      },
      function(error) {
      Xrm.Utility.alertDialog(error.message);
      }
      );

    }
},
function(error) {
    Xrm.Utility.alertDialog(error.message);
}

);
}

Auto-Populate Email Signature based on the Queue name using Javascript

Below is the code that fulfilled the requirement;

While started working for this requirement, breaking down the requirement into pieces help me to develop code and Voila! it worked.

Don’t let fear , it pulls you back instead step ahead even there are ditches but ultimately the path paves you to a red carpet.

function setSignature(executionContext) {
    var formContext = executionContext.getFormContext();
    var name = formContext.getAttribute("from").getValue()[0].name;

    var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct ='false'>" +
        "<entity name='emailsignature'> " +
        "<attribute name='title' /> " +
        "<attribute name='emailsignatureid' /> " +
 "<attribute name='safehtml' /> " +
        "<filter type ='and'> " +
        "<condition attribute='title' operator='eq' value='" + name + "'/>" +
        "</filter> " +
        "</entity> " +
        "</fetch>";
    fetchXML = "?fetchXml=" + encodeURIComponent(fetchXML);

   Xrm.WebApi.retrieveMultipleRecords("emailsignature", fetchXML).then(
        function success(result) {
                var desc = formContext.getAttribute("description").getValue();
                if (desc.includes(result.entities[0].safehtml)) {
                    formContext.getAttribute("description").setValue(desc);
                }
                else {
                    if (desc != null) {
                        desc += "<br>" + result.entities[0].safehtml;
                        formContext.getAttribute("description").setValue(desc);
                    }
                    else {
                        formContext.getAttribute("description").setValue(result.entities[0].safehtml);
                    }
                }
                
            },
        function (error) {
            console.log("failed with error: ", error);
            return null;
        }
    );
}

Passing values from Multiselect slicer to DAX measure

Using IN operator in DAX we achieved this functionality on selecting multiple values from Month and Year slicer we are able to display Revenue and Budget fields.

Power Query:

Monthly Rev (Multiselect) =

VAR SelectedMonths = ALLSELECTED ( Calender1[month] )

var selectedyear=ALLSELECTED(systemusers[UserYear])

RETURN

CALCULATE(SUM(DR_StravaInvoicesbyGLCategory[Extended Price]),FILTER(DR_StravaInvoicesbyGLCategory,DR_StravaInvoicesbyGLCategory[Month] IN SelectedMonths),FILTER(DR_StravaInvoicesbyGLCategory,DR_StravaInvoicesbyGLCategory[Year] IN selectedyear))

Similarly, to a measure having no month fields in the data we are able to solve this by using STRINGCOMPARE.

Power Query :

MonthBud(multi) =

VAR SelectedMonths = ALLSELECTED ( Calender1[month] )

var selectedyear=ALLSELECTED(systemusers[UserYear])

var mon1=IF(ISFILTERED(Calender1[month]),CONCATENATEX(ALLSELECTED(Calender1[month]),Calender1[month],”,”))

var mon2=CALCULATE(IF(CONTAINSSTRINGEXACT(mon1,”January”),CALCULATE(SUMX(strava_goals,strava_goals[strava_january])),0)+IF(CONTAINSSTRINGEXACT(mon1,”February”),CALCULATE(SUMX(strava_goals,strava_goals[strava_february])),0)+IF(CONTAINSSTRINGEXACT(mon1,”March”),CALCULATE(SUMX(strava_goals,strava_goals[strava_march])),0)+IF(CONTAINSSTRINGEXACT(mon1,”April”),CALCULATE(SUMX(strava_goals,strava_goals[strava_april])),0)+IF(CONTAINSSTRINGEXACT(mon1,”May”),CALCULATE(SUMX(strava_goals,strava_goals[strava_may])),0)+IF(CONTAINSSTRINGEXACT(mon1,”June”),CALCULATE(SUMX(strava_goals,strava_goals[strava_june])),0)+IF(CONTAINSSTRINGEXACT(mon1,”July”),CALCULATE(SUMX(strava_goals,strava_goals[strava_july])),0)+IF(CONTAINSSTRINGEXACT(mon1,”August”),CALCULATE(SUMX(strava_goals,strava_goals[strava_august])),0)+IF(CONTAINSSTRINGEXACT(mon1,”September”),CALCULATE(SUMX(strava_goals,strava_goals[strava_september])),0)+IF(CONTAINSSTRINGEXACT(mon1,”October”),CALCULATE(SUMX(strava_goals,strava_goals[strava_october])),0)+IF(CONTAINSSTRINGEXACT(mon1,”November”),CALCULATE(SUMX(strava_goals,strava_goals[strava_november])),0)+IF(CONTAINSSTRINGEXACT(mon1,”December”),CALCULATE(SUMX(strava_goals,strava_goals[strava_december])),0),FILTER(strava_goals,strava_goals[strava_fiscalyear] IN selectedyear),FILTER(strava_goals,strava_goals[statecode]==0))

return mon2

Azure Devops

How to create a new project in devops from Power Automate

Today, I have come across a task of creating new project in Azure devops but it should be done through actions using Power Automate (a.k.a flows).After spending ample amount of time to do research and experiments we finally got a solution to create the whole new project in devops.

“Arriving at the solution or not, after failing for 100 times enriches one’s knowledge than quitting at the early stage”

  • First, we need to create a new flow in the power automate : In the left panel go to—> my flows—>new flow—>select any of the below(automated flow, instant flow, scheduled flow, desktop flow).
  • Using flows, we need to define triggers and actions for the sample purpose i choose manual trigger as shown below
Manually trigger a flow
  • Now, to connect to devops, we need to use connectors in which some are free and some are premium but to connect to devops we will use “HTTP” connector which is a premium service btw
  • Select HTTP connector then it will appear as below:
Getting process data from devops organization
  • In the above, we are retrieving various processes data from the devops organization which we are going to use in creation of new project using “GET” method.
  • Below is the link to have a detailed info to use get method in devops:
  • https://docs.microsoft.com/en-us/rest/api/azure/devops/core/projects/get?view=azure-devops-rest-5.1
  • Authentication is necessary by using basic authorization option here we need to provide User name of the organisation and in the password field we need to use PAT(Personal Access Tokens).
  • To create these Personal access tokens , jump to azure devops on the top right of the ribbon one can find profile area there we can find PAT.
  • To have full access of devops create a personal access token and store it securely as once created it won’t be displayed and the expiry of these token are 30 days.
  • Using those PAT in the password field of basic authentication for sending http request to devops this ensures successful creation of connection and using the GET function and URI we can get the response of all the process in your organisation as shown below:
Response request
HTTP request using POST method
  • In the sketched area one should enter the organization and in the body we should enter the json format commands to create project.
  • sample body :

{

“name”: “FabrikamTravel”,

“description”: “Frabrikam travel app for Windows Phone”,

“capabilities”: {

“versioncontrol”:{

“sourceControlType”: “Git”

},

“processTemplate”:{

“templateTypeId”: “6b724908-ef14-45cf-84f8-768b5384da45”

}

}

}

In the above sample code use your chosen id from process which you get using http GET request.

  • In this request also give all the basic authorization fields then the response after running the flow will be as follows:
HTTP response using POST Method

Here, new project creation done successfully as see the id, status and url of the new project but status is still showing not set because as it takes some time to do the operation.

To check whether it is created successfully are not , after some time send a http request to show list of projects in your devops organisation using http get method :

GET https://dev.azure.com/{organization}/_apis/projects?api-version=5.1

Using the above url in http request the status of new project can be known.

Hope you find this content of the blog useful and pls comment