locked
CRM 2011 + custom workflow send mail to multiple recipients. RRS feed

  • Question

  • Hi All,

    The attached workflow working fine when mail send to one recipient. But when sending to multiple recipients it give me an error that “propertobjectcode” is cannot set to null.

    I have commented the below two lines where I have tried for multiple recipients. Need help on this.

     Entity Fromparty = new Entity("activityparty");
                            Entity Toparty = new Entity("activityparty");

                            Fromparty["partyid"] = new EntityReference("systemuser", CreatedBy);

                            //EntityReferenceCollection sendTo = new EntityReferenceCollection();

                            bool bFlag = false;
                            foreach (var item in fetchResults.Entities)
                            {
                                bFlag = true;
                                if (item.Attributes.Contains("systemuserid"))
                                {
                                    //sendTo.Add(new EntityReference("systemuser", new Guid(item.Attributes["systemuserid"].ToString())));
                                    Toparty["partyid"] = new EntityReference("systemuser", new Guid(item.Attributes["systemuserid"].ToString()));
                                    //Toparty.Attributes.Add("partyid", sendTo);
                                    break;
                                }
                            }
                            if (bFlag)
                            {
                                //Toparty.Attributes.Add("partyid", sendTo);
                                Entity email = new Entity("email");
                                email["from"] = new Entity[] { Fromparty };
                                email["to"] = new Entity[] { Toparty };
                                email["directioncode"] = true;

                                Entity template = null;
                                if (Create.Get<bool>(executionContext))
                                {
                                    template = GetTemplateByName("New Account Creation Mail to Data Admin", service);
                                }
                                if (Resubmit.Get<bool>(executionContext))
                                {
                                    template = GetTemplateByName("Account Resubmit Mail", service);
                                }

                                if (template != null && template.Id != null)
                                {
                                    var emailUsingTemplateReq = new SendEmailFromTemplateRequest
                                    {
                                        Target = email.ToEntity<Entity>(),
                                        TemplateId = template.Id,
                                        RegardingId = accountId,
                                        RegardingType = "account"
                                    };
                                    var emailUsingTemplateResp = (SendEmailFromTemplateResponse)service.Execute(emailUsingTemplateReq);
                                }
                            }


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Yogesh Vijay Mulay

    Saturday, March 22, 2014 11:34 AM

All replies

  • Hi Yogesh,

    Try something like this, hope this helps.

                Entity Fromparty = new Entity("activityparty");
                //Entity Toparty = new Entity("activityparty");
    
                Fromparty["partyid"] = new EntityReference("systemuser", CreatedBy);
    
                //EntityReferenceCollection sendTo = new EntityReferenceCollection();
    
                bool bFlag = false;
                Entity[] Toparty = new Entity[fetchResults.Entities).Count()];
                int i = 0;
                foreach (var item in fetchResults.Entities)
                {
                    bFlag = true;
                    if (item.Attributes.Contains("systemuserid"))
                    {
                        Toparty[i] = new Entity("activityparty");
    		    Toparty[i]["partyid"] = new EntityReference("systemuser", new Guid(item.Attributes["systemuserid"].ToString()));
    		    i++;
                    }
                }
    
                if (bFlag)
                {
                    //Toparty.Attributes.Add("partyid", sendTo);
                    Entity email = new Entity("email");
                    email["from"] = new Entity[] { Fromparty };
                    email.Attributes.Add("to", Toparty);
                    //email["to"] = new Entity[] { Toparty };
                    email["directioncode"] = true;
    
                    Entity template = null;
                    if (Create.Get<bool>(executionContext))
                    {
                        template = GetTemplateByName("New Account Creation Mail to Data Admin", service);
                    }
                    if (Resubmit.Get<bool>(executionContext))
                    {
                        template = GetTemplateByName("Account Resubmit Mail", service);
                    }
    
                    if (template != null && template.Id != null)
                    {
                        var emailUsingTemplateReq = new SendEmailFromTemplateRequest
                        {
                            Target = email.ToEntity<Entity>(),
                            TemplateId = template.Id,
                            RegardingId = accountId,
                            RegardingType = "account"
                        };
                        var emailUsingTemplateResp = (SendEmailFromTemplateResponse)service.Execute(emailUsingTemplateReq);
                    }
                }


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Sunday, March 23, 2014 2:56 AM