Saturday, July 31, 2021

Custom Workflow activity to get and set partylist lookup field using C# code

Follow the step-by-step process to Get the value from the email activity From field and set the value in the customer type lookup field.

In my scenario, I want to Get the value from the email activity From partylist field and Set the value in the custom field which is of Customer Type. (i.e. From & Customer)

 Step by step follow below custom activity code:

// Declare InArgument & OutArgument

//Pass email input argument to workflow
        [RequiredArgument]
        [Input("Email")]
        [ReferenceTarget("Email")]
        public InArgument<EntityReference> Email { get; set; }

//Return output parameter which is of contact type in my case
        [ReferenceTarget("contact")]
        [Output("customer")]
        public OutArgument<EntityReference> customer { get; set; }


Write below code inside execute method:


var email = Email.Get(context);
var emailDetails = service.Retrieve(email.LogicalName, email.Id, new ColumnSet("from"));
                    if (emailDetails.Contains("from") && emailDetails["from"] != null)
                    {
                        EntityCollection FromCollection = emailDetails.GetAttributeValue<EntityCollection>("from");


//To get the from reference and set on the Email customer lookup

                        EntityReference fromContact = GetFromContact(FromCollection);
                        customer.Set(context, fromContact);
                    }

//Method to get the value from From field and Set it to Customer Lookup

public EntityReference GetFromContact(EntityCollection fromCollection)
        {
            EntityReference contact = null;
            try
            {
                if (fromCollection != null && fromCollection.Entities.Count > 0)
                {
                    var from = fromCollection.Entities.First();

//To get the entity type
                    var partyId = from.GetAttributeValue<EntityReference>("partyid");
                    tracingService.Trace("PartyId: " + partyId.Id + " PartyId LogicalName: " + partyId.LogicalName);
                    if (partyId.LogicalName == "contact")
                    {

//To set the customer lookup
                        contact = new EntityReference(partyId.LogicalName, partyId.Id);
                    }
 
                }
            }
            catch (Exception ex)
            {
            }
            return contact;

        }


Build, Sign and register the workflow activity code using the Plugin registration tool

Create an OOB Workflow Activity and add the input variable and set the output variable to update.


Thank you..

Keep Learning..!!

Friday, July 30, 2021

Get and Set activityparty entity and Partylist field using C# code in D365

This blog will give you overview on activity Party entity and partylist fields.

Within Dynamics 365 we have activity party entity as well as partylist fields, an activity party represents a person or group associated with an activity. An activity can have multiple activity parties. 
There are 11 activity party types in Dynamics 365 for Customer Engagement (Sender "From", ToRecipient, CCRecipient, BccRecipient, RequiredAttendee, OptionalAttendee, Organizer, Regarding, Owner, Resource, Customer).

CRM Activity PartyList field is a special type of lookup field which refers to multiple entities. To hold or store these multiple entities references, Microsoft has provided one special type of entity named ActivityParty.

ActivityParty, has one special field called PartyId. which has the capability to store multiple entities references like user, contact, account, lead, queue etc.

Hence, if you want to get/set value in PartyList Field, you need to interact with ActivityParty PartyId field.

Remember, while getting or setting value in party list fields (to, from cc, bcc, regarding etc). You should use ActivityParty Entity and PartyId field. 

1. To get the value from Party List TO and FROM fields:

Retrieve the value from PartyId field of ActivityParty entity, which holds the record’s Guids which was selected in Email’s FROM and TO fields.

2. To set the value in Party List TO and FROM fields:

First pass the value in PartyId field of ActivityParty entity and then set it’s EntityReference to partylist FROM and TO fields.

Note: Use the dynamics value in place of hardcoded values.

Get partyList fields value using C# code:

public static void GetPartyList(string emailRecordId)
        {
            emailRecordId = "C472398F-592F-E561-A939-000S4AF24369";
            // Retrieve email record
            Entity email = service.Retrieve("email", new Guid(emailRecordId), new ColumnSet("from", "to"));
 
            // get value from partylist - 'FROM' field
            EntityCollection from = email.GetAttributeValue<EntityCollection>("from");
            if (from != null && from.Entities.Count > 0)
            {
                foreach (var item in from.Entities)
                {
                    EntityReference partyId = item.GetAttributeValue<EntityReference>("partyid");
                    string addressUsed = item.GetAttributeValue<string>("addressused");
                }
            }
 
            // get value from partylist - 'TO' field
            EntityCollection to = email.GetAttributeValue<EntityCollection>("to");
            if (to != null && to.Entities.Count > 0)
            {
                foreach (var item in to.Entities)
                {
                    EntityReference partyId = item.GetAttributeValue<EntityReference>("partyid");
                    string addressUsed = item.GetAttributeValue<string>("addressused");
                }
            }

        }

//-----------------------------------------------------------------------------------------------

Set value in Party List Fields using C# (Single Recipient)

//Set partyList field when there is single recipient
        public static void SetPartyList()
        {
            // Declare party list entity to set value in FROM & TO field
            Entity from = new Entity("activityparty");
            Entity to = new Entity("activityparty");
 
            // Set value in FROM & TO field
            from["partyid"] = new EntityReference("systemuser", new Guid("D7241D9E-FCEC-4C6B-80D0-A2C5B1588D5"));
            to["partyid"] = new EntityReference("account", new Guid("475B158C-4C6C-E511-8C63-72D8D9E47BA8"));
 
            // Declare party list entity to set value in FROM field
            Entity email = new Entity("email");
 
            // Insert value in email FROM & TO field
            email["from"] = new Entity[] { from };
            email["to"] = new Entity[] { to };
 
            //Set regarding object property (i.e. The entity record, which u want this email associated with)
            EntityReference regardingObject = new EntityReference("contact", new Guid("6C3BE98B-88DF-E311-B8E5-6HGE55B8B676"));
 
            email.Attributes.Add("regardingobjectid", regardingObject);
 
            //Set subject & body properties
            email.Attributes.Add("subject", "Email subject created for Single recipients");
            email.Attributes.Add("description", "This is Email description for Single recipients");
 
            //Create email activity
            Guid emailID = service.Create(email);

        }

Set value in Party List Fields using C# (Multiple Recipient)

//Set partyList field when there is Multiple recipient

        public static void SetPartyList()
        {
            // Set value in FROM party list field
            Entity from1 = new Entity("activityparty");
 
            // two accounts inside the TO field
            Entity to1 = new Entity("activityparty");
            Entity to2 = new Entity("activityparty");
            // two contacts inside the TO field
            Entity to3 = new Entity("activityparty");
            Entity to4 = new Entity("activityparty");
 
            // set value in FROM field
            from1["partyid"] = new EntityReference("systemuser", new Guid("D72D8D9E-FCEC-4C6B-8340-A2CB9FAA88D5"));
 
            // set multiple values in TO field
            to1["partyid"] = new EntityReference("account", new Guid("475B158C-541C-E511-80D3-3863BB347BA8"));
            to2["partyid"] = new EntityReference("account", new Guid("A8A19CDD-88DF-E311-B8E5-6C3BE5A8B200"));
            to3["partyid"] = new EntityReference("contact", new Guid("25A17064-1AE7-E611-80F4-E0071B661F01"));
            to4["partyid"] = new EntityReference("contact", new Guid("48A0E5B9-88DF-E311-B8E5-6C3BE5A8B980"));
            Entity email = new Entity("email");
 
            //Set regarding object property (i.e. The entity record, which u want this email associated with)
            EntityReference regardingObject = new EntityReference("contact", new Guid("48A0E9B9-88DF-E311-B8E5-6C3BE5A8B980"));
 
            email.Attributes.Add("regardingobjectid", regardingObject);
 
            // Insert value in FROM & TO field
            email["from"] = new Entity[] { from1 };
            email["to"] = new Entity[] { to1, to2, to3, to4 };
 
            //Set subject & body properties
            email.Attributes.Add("subject", "Email created from Console for Multiple recipients");
            email.Attributes.Add("description", "Email created from Console or Multiple recipients");
 
            //Create email activity
            Guid emailID = service.Create(email);

        }


References: Click HereMS Docs ActivityParty

Thank you!!!

Keep CRMing... 

Happy Learning.....

Thursday, July 8, 2021

Plugin code to update associated record in D365

How to update multiple associated records using D365 plugin code?

Here in this scenario, I have a custom entity called "Sample" and it's having a 1:N relationship with a Case entity. Now I want to update few fields on the "Sample" record and it should automatically update few fields on the Case record once click on the save record.

Let's proceed... 

1. Get the Sample record Id first which can be passed to fetch XML conditions to update the only related record to this ID.

2. Follow the code below, fetch the case record using advance find, convert it to C# format and add it variable as shown
     -> Pass the formated fetch XML to service.retrieveMultiople to fetch the record collection
    -> Add it to an entity collection and read and update records one by one
    -> Call the service.update with entity object in it.

Entity esample;

DateTime createdon = esample.GetAttributeValue<DateTime>(Sample.Createdon);//"createdon"
DateTime modifiedon = esample.GetAttributeValue<DateTime>(Sample.Modifiedon);//"modifiedon"
            try
            {
               
                var fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='incident'>
                                    <attribute name='incidentid' />
                                    <attribute name='createdon' />
                                    <attribute name='modifiedon' />
                                    <order attribute='createdon' descending='false' />
                                    <filter type='and'>
                                      <condition attribute='new_sampleid' operator='eq' value='{0}' />
                                    </filter>
                                  </entity>
                                </fetch>";
                fetchXml = string.Format(fetchXml, esample.Id);//Pass sample record id index
                caseResult = service.RetrieveMultiple(new FetchExpression(fetchXml));
                if (caseResult != null && caseResult.Entities.Count > 0)
                {
                    foreach (var caseRecord in caseResult.Entities)
                    {
                        Entity eCase = new Entity(Cases.EntityLogicalName);//"incident" entity logical name
                        eCase.Id = caseRecord.Id;
                        eCase[Cases.CreatedonDatetime] = createdon;//eCase["createdon"]
                        eCase[Cases.ModifiedonDatetime] = modifiedon;//eCase["modifiedon"]
                        service.Update(eCase);
                    }
                }

            } 


The record gets updated go and verifies the associated record.

Click here to learn more

Thats' all

Keep CRMing...

Wednesday, July 7, 2021

Retrieve label name of optionset value from link-entity fetch XML D365 C# Code

Follow the below process step by step to get the label name from the option set attribute from the linked entity.

Country and city are two related entities. In the city, there is an option set field value available. With help of advanced find, I have generated the fetch XML query based on my requirement. 

Note: you have to use an 'alias' to retrieve the label name as shown below.

To get option set value with alias:

OptionSetVal= ((OptionSetValue)((AliasedValue)enitycollection.Entities[0].Attributes["a_optionsetaliasname"]).Value).Value;

-----------------------------------------------------------------------------------------------------

var fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='new_country'>
                                    <attribute name='new_countryid' />
                                    <attribute name='new_cityid' />
                                    <order attribute='new_cityid' descending='false' />
                                    <filter type='and'>
                                      <condition attribute='new_countryid' operator='eq' value='{0}' />
                                    </filter>
                                    <link-entity name='new_city' from='new_cityid' to='new_cityid' visible='false' link-type='inner'>
                                      <attribute name='new_cityoptionset' alias='a_cityoptionset'/>
                                    </link-entity>
                                  </entity>
                                </fetch>";
                fetchXml = string.Format(fetchXml, CountryId);//countryid index value dynamically
                Result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                if (Result != null && Result.Entities.Count > 0)
                {
                    if (Result.Entities[0].Contains("a_citycode"))
                    {
                        var OptionSetValLabel = stationResult.Entities[0].FormattedValues["a_citycode"];
                    }

                }


Step 1:

 var OptionSetValLabel = stationResult.Entities[0].FormattedValues["a_citycode"];

Step 2:

public static string GetOptionSetValueLabel(string entityName, string fieldName, int optionSetValue, IOrganizationService service)
        {
 
            var attReq = new RetrieveAttributeRequest();
            attReq.EntityLogicalName = entityName;
            attReq.LogicalName = fieldName;
            attReq.RetrieveAsIfPublished = true;
 
            var attResponse = (RetrieveAttributeResponse)service.Execute(attReq);
            var attMetadata = (EnumAttributeMetadata)attResponse.AttributeMetadata;
 
            return attMetadata.OptionSetVal.Options.Where(x => x.Value == optionSetValue).FirstOrDefault().Label.UserLocalizedLabel.Label;
 

        }


Click on the link to learn more about the plugin.


That's all...

Thank you...

Keep Learning.

Tuesday, July 6, 2021

Retrieve link entity attribute value from fetch XML in dynamics 365 using Plugin C# Code

The following example code shows how to get related entities Country and City, City Code using fetch XML link-entity

Note: Here you need to specify the ‘alias’ name which you can keep different than ‘attributename’ and use the same alias name in GetAttributeValue while fetching value. let's go into detail by looking into the below example.

var citycode = countryResult.Entities[0].GetAttributeValue<AliasedValue>("a_citycode").Value.ToString();

Follow the below example to understand more on how to retrieve the link entity attribute value. In this example I am using fetch XML to get the record based on my requirement you can use advanced find to retrieve the fetch XML query as per your requirement. 

var fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='new_country'>
                                    <attribute name='new_countryid' />
                                    <attribute name='new_cityid' />
                                    <order attribute='new_cityid' descending='false' />
                                    <filter type='and'>
                                      <condition attribute='new_countryid' operator='eq' uitype='new_country' value='{0}' />
                                    </filter>
                                    <link-entity name='new_city' from='new_cityid' to='new_cityid' visible='false' link-type='outer' alias='a_8e4b53ab387aea11a811000d3a8b2b00'>
                                      <attribute name='new_citycode' alias='a_citycode'/>
                                    </link-entity>
                                  </entity>
                                </fetch>";
                fetchXml = string.Format(fetchXml, CountryId);// pass dynamics country id in the first index {0}
                var countryResult = service.RetrieveMultiple(new FetchExpression(fetchXml));
               
                if (countryResult != null && countryResult.Entities.Count > 0)
                {
                    var citycode = countryResult.Entities[0].GetAttributeValue<AliasedValue>("timezonediff").Value.ToString();
                }


Click here to learn more about Plugin dynamics 365

That's all..

Thank you .. keep learning..

Write, Register and Debug a plugin step by step in Dynamics 365

 📝Writing, registering, and debugging a plug-in in Dynamics 365 

In this blog, I will be showing How to Write, Register, and Debug a Plug-in in dynamics 365? 
Follow the complete step by step process written below:

Short Overview:
  1. Create a .NET Framework Class library project in Visual Studio
  2. Add the Microsoft.CrmSdk.CoreAssemblies NuGet package to the project
  3. Implement the IPlugin interface on classes that will be registered as steps.
  4. Add your code to the Execute method required by the interface
    1. Get references to services you need
    2. Add your business logic
  5. Sign & build the assembly
  6. Test the assembly
    1. Register the assembly in a test environment
    2. Add your registered assembly and steps to an unmanaged solution
    3. Test the behavior of the assembly
    4. Verify expected trace logs are written
    5. Debug the assembly as needed
Note: If you want to learn in a deep about plugin development click here.
            

Begin:


Writing a Plug-in