0
votes

I have a CRM Dynamics plugin that fires on an Update of a boolean change, it fails to fire when I select Yes on the Two Option control, Please find my code below and advice where I may be going wrong.

namespace WebCall.Plugin
{
  public class WebCallTrigger : IPlugin
  {
    /// <summary>
    /// Plugin to initiate a web call from CRM using MaruSip API
    /// </summary>
    /// <param name="serviceProvider"></param>
    public void Execute(IServiceProvider serviceProvider)
    {

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        if (context == null)
        {
            throw new ArgumentNullException("localContext");
        }

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

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

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            //initaialize Entity
            Entity phoneCallEntity = (Entity)context.InputParameters["Target"];

            if (phoneCallEntity.LogicalName != Contact.EntityLogicalName)
                return;

            //ensure that the Plugin fires on a create operaton
            if (context.MessageName == "Update")
            {
                try
                {
                    if (phoneCallEntity.Attributes.Contains("new_dialnumber"))
                        phoneCallEntity["new_dialnumber"] = true;

                    string NumberToCall;                // = phoneCallEntity.Contains("telephone1") ? phoneCallEntity["telephone1"].ToString() : null;

                    if (phoneCallEntity.Contains("telephone1"))
                    {
                        NumberToCall = phoneCallEntity.Attributes["telephone1"].ToString();
                    }

                    else
                    {
                        NumberToCall = phoneCallEntity.Attributes["mobilephone"].ToString();
                    }

                    string ReceiveCallOn = phoneCallEntity.Contains("new_receivecallon") ? phoneCallEntity["new_receivecallon"].ToString() : null;
                    string apiKey = phoneCallEntity.Attributes.Contains("new_apikey") ? phoneCallEntity.Attributes["new_apikey"].ToString() : null;
                    int fId = phoneCallEntity.Attributes.Contains("new_fid") ? (int)phoneCallEntity.Attributes["new_fid"] : 0;

                    //service.Update(phoneCallEntity);

                    //Create a new instance of the WebCallService and call the webcall method
                    WebCallService webCallService = new WebCallService();

                    webCallService.WebCall(NumberToCall, ReceiveCallOn, apiKey, fId);
                }
2
Just to clearify the control that I want to fire the plugin on update is phoneCallEntity["new_dialnumber"] = false; in the code above. – Papi
What do you mean by it fails to fire? The plugin isn't getting called at all? Or no actions are being taken? – Daryl

2 Answers

0
votes

In case telephone1 or mobilephone fields were not updated - target you are referring would not contain mentioned fields. My suggestion to get that data from pre-Image. You can check this article - https://deepakexploring.wordpress.com/2011/02/04/preentityimages-and-postentityimages-in-crm-5-0-2011/

0
votes

If you registered the plugin on update of the Phone Call entity and you are comparing its logical name to "Contact" your plugin will never fire.

Review this line from your code:

if (phoneCallEntity.LogicalName != Contact.EntityLogicalName)
                return;