Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Create a task after account creation ?

1. Delete the class library and add new class with name Create task after
account creation.
2. Add References System.Runtime.Serialization And Microsoft.Xrm.Sdk.dll

1. Write Business Logic


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

namespace Create_a_task_after_account_creation
{
public class Create_Task_After_Creation : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
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.


IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

try
{
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{

// Obtain the target entity from the input parameters.


Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "account")
return;
EntityReference taskRegarding = null;

if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectId = new Guid(context.OutputParameters["id"].ToString());//GUID

string regardingobjectidType = "account";

taskRegarding = new EntityReference(regardingobjectidType, regardingobjectId);

Guid createdRecordId = createEntityRecord("task", taskRegarding, service);


}

}
}
catch (Exception ex)
{
}

public Guid createEntityRecord(string entityName, EntityReference regardingObject, IOrganizationService


crmService)
{
// Create a task activity to follow up with the account customer in 7 days.
Entity followup = new Entity(entityName);//task

followup["subject"] = "Send e-mail to the new customer.";


followup["description"] = "Follow up with the customer. Check if there are any new issues that need
resolution.";
followup["scheduledstart"] = DateTime.Now.AddDays(7);
followup["scheduledend"] = DateTime.Now.AddDays(7);
followup["category"] = regardingObject.LogicalName;//account
followup["regardingobjectid"] = regardingObject; // account gUID

return crmService.Create(followup);
}

}
}

1. Sign in And Build. Open File explorer And Take dll Project.
2. Register Plugin
Update a child records(Contact) via Fetch Xml In Account Entity

Create a Company and city column by using advance find. Update Siva Reddy and
Neelavathi City as Banglore.

Update Contacts Via Fetch Xml


1. Create a plugin

"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+


" <entity name='contact'>"+
" <attribute name='fullname' />"+
" <attribute name='telephone1' />"+
" <attribute name='contactid' />"+
" <attribute name='parentcustomerid' />"+
" <attribute name='address1_city' />"+
" <order attribute='fullname' descending='false' />"+
" <filter type='and'>"+
" <condition attribute='parentcustomerid' operator='eq' uiname='Amazon' uitype='account'
value='{BA5550BE-7A93-EB11-B1AC-0022486E7FE2}' />"+
" <condition attribute='address1_city' operator='eq' value='Banglore' />"+
" </filter>"+
" </entity>"+
"</fetch>"

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

namespace Contact_update_via_Fetch_Xml_In_Account
{
public class Contact_Update_via_fetch_Xml_in_Account : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
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.


IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


try
{

if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{

// Obtain the target entity from the input parameters.


Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "account")
return;

Guid accountId = context.PrimaryEntityId; // Account guid

string Name = entity.GetAttributeValue<string>("name");// Account name


//var name = formcontext.getAttribute("name").getValue();

if (accountId != Guid.Empty)
{
EntityCollection contactRecords = getAllRelatedContacts(accountId, service);
{
foreach (Entity contact in contactRecords.Entities)
{
contact["firstname"] = "Bhargav";
contact["lastname"] = Name;//A. Datum
service.Update(contact);
}
}
}

}
}
catch (Exception ex)
{
}

public EntityCollection getAllRelatedContacts(Guid accountId, IOrganizationService service)


{
var _fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical'
distinct='false'>" +
" <entity name='contact'>" +
" <attribute name='fullname' />" +
" <attribute name='telephone1' />" +
" <attribute name='contactid' />" +
" <attribute name='parentcustomerid' />" +
" <attribute name='address1_city' />" +
" <order attribute='fullname' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='parentcustomerid' operator='eq' uiname='Amazon' uitype='account'
value='" + accountId + "' />" +
" <condition attribute='address1_city' operator='eq' value='Banglore' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection contacts = service.RetrieveMultiple(new FetchExpression(_fetchXML));

return contacts;

}
}

PLUGIN DEBUGER
Trigger The Function in Dynamics 365

Copy The Log file and past it into the Plugin Dll
Getting Primary Contact ID and Name Then Job Title

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

namespace GettingPrimaryContactIDandNamethenJobTitle
{
public class GettingPrimaryTitle : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
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.


IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

try
{
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "account")
return;
Guid AccountID = context.PrimaryEntityId; // accoutID

EntityReference primaryConatcID = (EntityReference)entity.Attributes["primarycontactid"];

Guid contactId = primaryConatcID.Id;


//string contactName = primaryConatcID.Name;

Entity contact = service.Retrieve("contact", contactId, new ColumnSet("lastname",


"middlename", "jobtitle"));

string title = contact.GetAttributeValue<string>("jobtitle");


//return contact.GetAttributeValue<string>("lastname");

EntityReference taskRegarding = null;


if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectId = new Guid(context.OutputParameters["id"].ToString());//GUID

string regardingobjectidType = "account";

taskRegarding = new EntityReference(regardingobjectidType, regardingobjectId);

Guid createdRecordId = createEntityRecord("task", taskRegarding, service, title);


}

}
}
catch (Exception ex)
{
}

}
public Guid createEntityRecord(string entityName, EntityReference regardingObject,
IOrganizationService crmService, string title)
{
// Create a task activity to follow up with the account customer in 7 days.
Entity followup = new Entity(entityName);//task

followup["subject"] = title;
followup["description"] = "Follow up with the customer. Check if there are any new issues that
need resolution.";
followup["scheduledstart"] = DateTime.Now.AddDays(7);
followup["scheduledend"] = DateTime.Now.AddDays(7);
followup["category"] = regardingObject.LogicalName;//account
followup["regardingobjectid"] = regardingObject; // account gUID

return crmService.Create(followup);
}

Query By Attribute

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

namespace Update_Purchasing_Assistant_With_Title_Name_Developer_with_QA
{
public class Update_Developer_with_Query_Attribute : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
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.


IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{

// Obtain the target entity from the input parameters.


Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "contact")
return;
// Create query using querybyattribute
QueryByAttribute query = new QueryByAttribute("contact");
query.ColumnSet = new ColumnSet("fullname", "address1_city", "jobtitle");
// Attribute to query
query.Attributes.AddRange("jobtitle");
// Value of queried attribute to return
query.Values.AddRange("Purchasing Manager");

EntityCollection results = service.RetrieveMultiple(query);

if (context.Depth == 1)
{
foreach (Entity contact in results.Entities)
{
contact["telephone1"] = " Purchasing officer";
service.Update(contact);

}
}
}
}
catch (Exception ex)
{
}

}
}
}

You might also like