Showing posts with label CRM 2016 Online. Show all posts
Showing posts with label CRM 2016 Online. Show all posts

Jan 4, 2017

Calling third party Web service from Dynamics CRM online plug-in

Just thought of sharing this important code snippet. Please have a closer look at Binding Configuration part which is the essence of the exercise.

public class PostUpdateTransaction : IPlugin
{
  public void Execute(IServiceProvider serviceProvider)
  {
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
    ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    if (context.Depth > 1)
    {
        return;
    }

    try
    {
        BasicHttpBinding myBinding = new BasicHttpBinding();
        myBinding.Name = "BasicHttpBinding_Service";
        myBinding.Security.Mode = BasicHttpSecurityMode.Transport;
        myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
        myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

        EndpointAddress endPointAddress = new EndpointAddress(@"https://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.asmx");
        ClassLibrary1.XXXXX.XXXClient serviceClient = new ClassLibrary1.XXXXX.XXXClient(myBinding, endPointAddress);
        string xmlRequest = @"XXX";

        string result = serviceClient.<Method>(xmlRequest);
        XmlDocument resultXML = new XmlDocument();
        resultXML.LoadXml(result);

     }
     catch (Exception ex)
     {
        throw new InvalidPluginExecutionException(ex.Message);
     }
     finally
     {

     }
  }
}

Dec 20, 2016

Migrate Connections using Kingswaysoft Adapter

We all use Kingswaysoft Adapter to migrate data to Dynamics CRM. Just thought of sharing related tricks.

Here I reveal how to migrate connection, which can be little tricky than other entities. Important part is we need Connection roles are configured correctly. In my example, Employee and Employer Roles needs to be configured as illustrated below.





Account (i.e. Organisation in given example) is Employer and Contact (i.e. Individual in given example) is Employee. See how they associated with each other and enabled for particular entities.
Now see the fields we need to map with relevant information. We pass correct texts for Roles and Guids for related records. We need to pass object type code as well.


Actual fields we are mapping here as below;

Record1id = Connected To
Record1roleid = Role (To)
Record2id = Connected From
Record2roleid = Role (From)

For Roles need to resolve with Primary field as below since we are passing the names. For the safe side I enable the "Ignore Case". Still we need to make sure spellings are correct.

Object Type Code for Custom entities
This is tricky since this value can be different based on environment. Just check the URL of an open record of same type to obtain it.

Dec 8, 2016

Passing and Receiving values (parameters) in Custom workflow

Just thought of posting this code snippet on input/output parameters in CWF. In most of the places it explains well on how to declare the parameters, but not using of get, set methods in the code.

Here how we declare them;

[RequiredArgument]
[Input("Salary")]
public InArgument<Double> SalaryVal { get; set; }

[OutputAttribute("Tax")]
public OutArgument<Double> TaxVal { get; set; }

...and this is how you associate to the code within the Execute method;

protected override void Execute(CodeActivityContext executionContext)
{
//...........
//...........

Double _salaryVal = SalaryVal.Get(executionContext);

// Calculation goes here
// and return to _taxVal

TaxVal.Set(executionContext, _taxVal);

}