Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin working fine while debugging, but only partially without

Posted on by 120

Hello People,

I am still strying to develop a Plugin, which executes a CRM Report as PDF, creates an Email activity and then attaches the PDF to it.

When I debug my Plugin everything works fine, but when I execute it without profiling/debugging via Plugin Registration Tool it only creates the Email but without the regarding object and without the attachment.

Also after creating the Email, I write the GUID into a Field on the opportunity, which works fine on normal execution. But I also write a "Logfile-String" inside my Plugin which I then want to put in a multiline-textfield because Tracing won't work for some reason and I want to know if there are some errors/null values etc. but this String will only be put in the according field when I debug the Plugin although the line in the code comes right after inserting the Email-GUID.

This whole thing seems pretty strange to me and I was hoping anybody had similar issues and know what could cause this weird behaviour.

The code is this:

        protected void ExecuteExecuteTriggerQuotationCreation(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }
            else
            {
                IPluginExecutionContext context = (IPluginExecutionContext)localContext.PluginExecutionContext;
                IOrganizationService service = (IOrganizationService)localContext.OrganizationService;
                ITracingService trace = (ITracingService)localContext.TracingService;

                string log = "";
                //Create an XML document to get the configuration-string
                XmlDocument configDoc = new XmlDocument();
                configDoc.LoadXml(UnsecureString);
                var bla = context.InputParameters["ReportSvrUrl_Output"];

                var reportServerURL = "";
                var reportName = "";

                //Get XML node which holds the name of the Report Server and write it in the variable
                XmlNodeList reportServerURLNode = configDoc.GetElementsByTagName("ReportServerURL");
                for (int i = 0; i < reportServerURLNode.Count; i++)
                {
                    reportServerURL = reportServerURLNode[i].InnerText;
                    //Show import-entity name in tracelog
                    trace.Trace("Report Server URL:" + reportServerURL);
                    log = log + "Report Server URL:" + reportServerURL + "\n";


                }

                XmlNodeList reportNameNode = configDoc.GetElementsByTagName("ReportName");
                for (int i = 0; i < reportNameNode.Count; i++)
                {
                    reportName = reportNameNode[i].InnerText;
                    //Show import-entity name in tracelog
                    trace.Trace("Report Name:" + reportName);
                    log = log + "Report Name:" + reportName + "\n";
                }

                string fullURL = reportServerURL + "/ReportExecution2005.asmx";
                string reportURL = reportServerURL + "/" + reportName;
                //string userName = context.UserId.ToString();
                string userName = context.InitiatingUserId.ToString();
                string passWord = context.OrganizationId.ToString();
                string reportNameLong = "/play1_MSCRM/" + reportName;

                

                try
                {
                    Guid ownerId = Guid.Empty;
                    Guid contactId = Guid.Empty;
                    Guid currentRecordId = Guid.Empty;

                    EntityReference opportunity = null;
                    opportunity = (EntityReference)context.InputParameters["Target"];
                    currentRecordId = opportunity.Id;

                    QueryExpression opportunityQuery = new QueryExpression();
                    opportunityQuery.EntityName = "opportunity";
                    ColumnSet opportunitySet = new ColumnSet("ownerid", "parentcontactid","name", "new_emailguid");
                    opportunityQuery.Criteria.AddCondition("opportunityid", ConditionOperator.Equal, currentRecordId);
                    opportunityQuery.ColumnSet = opportunitySet;
                    Entity currentOpportunity = service.Retrieve("opportunity", currentRecordId, opportunitySet);


                    foreach (var item in currentOpportunity.Attributes)
                    {
                        if (item.Key.ToString() == "ownerid")
                        {
                            EntityReference ownerReference = (EntityReference)item.Value;
                            ownerId = ownerReference.Id;
                            
                        }
                        else if(item.Key.ToString()== "parentcontactid")
                        {
                            EntityReference parentcontactReference = (EntityReference)item.Value;
                            contactId = parentcontactReference.Id;


                        }
                    }


                    #region new try                    
                    
                    Guid accountId = context.PrimaryEntityId;
                    byte[] result = null;
                    ReportExecutionService rs = new ReportExecutionService();
                    trace.Trace("--ReportingService initialized--");
                    
                    log = log + "--ReportingService initialized--" + "\n";

                    rs.Credentials = CredentialCache.DefaultNetworkCredentials;
                    trace.Trace("--ReportingService Credentials = DefaultCredentials--");
                    log = log + "--ReportingService Credentials = DefaultCredentials--" + "\n";

                    // Setting the URL of the Reporting Server
                    rs.Url = fullURL;
                    trace.Trace("-- Reporting Server Full URL: {0} --", fullURL);
                    log = log + "-- Reporting Server Full URL: "+ fullURL + "  --\n";
                   

                    string reportPath = reportNameLong;
                    trace.Trace("-- Report Name Long: {0} --", reportPath);
                    log = log + "-- Report Name Long: " + reportPath + "  --\n";

                    string format = "PDF";
                    string historyID = null;
                    string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                    string encoding;
                    string mimeType;
                    string extension;
                    ReportExecution2005.Warning[] warnings = null;
                    string[] streamIDs = null;

                    //var dsc = new ReportExecution2005.DataSourceCredentials
                    //{
                    //    DataSourceName = "CRM",
                    //    UserName = userName,
                    //    Password = passWord
                    //};
                    ////dsc = null;

                    //var credentials = new ReportExecution2005.DataSourceCredentials[] { dsc };
                    //rs.SetExecutionCredentials(credentials);

                    ExecutionInfo execInfo = new ExecutionInfo();
                    ExecutionHeader execHeader = new ExecutionHeader();
                    trace.Trace("-- ExecInfo & Header initialized --");
                    log = log + "-- ExecInfo & Header initialized--\n";

                    rs.ExecutionHeaderValue = execHeader;

                    trace.Trace("-- Load Report --");
                    log = log + "-- Load Report --" + "\n";
                    execInfo = rs.LoadReport(reportPath, historyID);
                    trace.Trace("-- Report Loaded --");
                    log = log + "-- Report Loaded --" + "\n";

                    //String SessionId = rs.ExecutionHeaderValue.ExecutionID;
                    trace.Trace("-- Render started --");
                    log = log + "-- Render started --" + "\n";
                    result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
                    trace.Trace("-- Render finished --");
                    log = log + "-- Render finished --" + "\n";
                    trace.Trace("-- Result != null? {0} --", result.Count());
                    log = log + "-- Result != null? = "+ result.Count() + " --" + "\n";

                    //var fu = "";
                    #endregion

                    #region trigger email creation

                    //ownerId = new Guid(ownerID);
                    //contactId = new Guid(contactID);

                    trace.Trace("-- Create Email Activity --");
                    log = log + "-- Create Email Activity --" + "\n";
                    CreateEmailActivity emailActivity = new CreateEmailActivity();
                    Guid emailGuid = emailActivity.createEmailActivity(service,trace, ownerId, contactId, result, currentRecordId);

                    trace.Trace("-- Email created --");
                    log = log + "-- Email created --" + "\n";


                    //trace.Trace("-- While result == null --");
                    //log = log + "-- While Result = null --" + "\n";

                    //while (result == null)
                    //{
                    //    trace.Trace("-- Wait 2 seconds --");
                    //    log = log + "-- Wait 2 Seconds --" + "\n";
                    //    System.Threading.Thread.Sleep(2000);
                    //}
                    


                    if (result!=null)
                    {
                        trace.Trace("-- Add attachment --");
                        log = log + "-- Add attachment --" + "\n";
                        AddEmailAttachment addAttachment = new AddEmailAttachment();
                        addAttachment.addEmailAttachment(service, trace, emailGuid, result);
                        trace.Trace("-- Attachment added --");
                        log = log + "-- Attachment added --" + "\n";
                    }


                    trace.Trace("-- Set Email Guid --");
                    log = log + "-- Set Email Guid --" + "\n";
                    currentOpportunity["new_emailguid"] = emailGuid.ToString();

                    //trace.Trace("-- Update opportunity --");
                    //log = log + "-- Update opportunity --" + "\n";
                    
                    currentOpportunity["new_opportunitylog"] = log;
                    service.Update(currentOpportunity);
                    
                    #endregion


                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
    }


Greeting from Germany!

*This post is locked for comments

  • Iswarya Profile Picture
    Iswarya 1,345 on at
    RE: Plugin working fine while debugging, but only partially without

    hi Seiffert,

    my requirement also same please mention what are the plugin assembly and dll have to use for this , because i struggle to achieve this one 

    Thanks& Regards,

    Iswarya.

  • Verified answer
    R.Seiffert Profile Picture
    R.Seiffert 120 on at
    RE: Plugin working fine while debugging, but only partially without

    Okay...the problem was that I was using the new CRM 365 Developer Toolkit and for some reason the deploying from Visual Studio didn't renew my Plugin. When I updated the dll via Plugin Registration Tool everything worked.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans