在CRM 2011中,我想将联系人附加到报价中,对此没有任何问题。

当我保存报价时,对于每个联系人,我想发送电子邮件以提醒目的。 (使用插件)如何标记它,并赋予CRM用户使用复选框从报价表中解开此功能的能力。

最终目的是赋予CRM用户将新的电子邮件提醒发送给报价中附带的一个或多个联系人的能力。

你能帮助我吗 ?

有帮助吗?

解决方案

您将需要一个功能区按钮,该按钮将在其中一个Web-Resources中调用JavaScript方法。

在里面 CommandDefinition 您将需要将参数发送到JS方法中,该方法将包含子网格中选定记录的所有ID。

<CommandDefinitions>
<CommandDefinition Id="xyz.Button.SendEmail.command">
<EnableRules>
</EnableRules>
<DisplayRules>
</DisplayRules>
<Actions>
    <JavaScriptFunction Library="$webresource:Test.Js" FunctionName="SendEmail">
        <CrmParameter Value="SelectedControlAllItemIds" />
    </JavaScriptFunction>
</Actions>
</CommandDefinition>

然后,JS方法将是下面的,您需要解析所有ID,然后处理您的逻辑

function SendEmail(selectedIds) {
if (selectedIds != null && selectedIds != “”) {
    var strIds = selectedIds.toString();
    var arrIds = strIds.split(“, ”);
    for (var indxIds = 0; indxIds < arrIds.length; indxIds++) {
        //The logic that you want to process on each record will come here.
    }
} else {
    alert(“No records selected !! !”);
}
}

其他提示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace SendEmail
{
    public class Email : IPlugin
    {
        public void Execute(IServiceProvider serviceprovider)
        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));
            if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
                return;

            //entity
            Entity ent = (Entity)context.InputParameters["Target"];
            if (ent.LogicalName != "entityName")//EntityName
                throw new InvalidPluginExecutionException("Not a Service Request record! ");

            //service
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
             string Email="";

            if (ent.Contains("emailidfiled"))
               Email = (string)ent["emailidfiled"];



            #region email template
            QueryExpression query = new QueryExpression()
            {
                EntityName = "template",
                Criteria = new FilterExpression(LogicalOperator.And),
                ColumnSet = new ColumnSet(true)
            };
            query.Criteria.AddCondition("title", ConditionOperator.Equal, "templateName");

            EntityCollection _coll = _service.RetrieveMultiple(query);
            if (_coll.Entities.Count == 0)
                throw new InvalidPluginExecutionException("Unable to find the template!");
            if (_coll.Entities.Count > 1)
                throw new InvalidPluginExecutionException("More than one template found!");

            var subjectTemplate = "";
            if (_coll[0].Contains("subject"))
            {
                subjectTemplate = GetDataFromXml(_coll[0]["subject"].ToString(), "match");
            }
            var bodyTemplate = "";
            if (_coll[0].Contains("body"))
            {
                bodyTemplate = GetDataFromXml(_coll[0]["body"].ToString(), "match");
            }
            #endregion



            #region email prep
            Entity email = new Entity("email");
            Entity entTo = new Entity("activityparty");
            entTo["addressused"] =Email;
            Entity entFrom = new Entity("activityparty");
            entFrom["partyid"] = "admin@admin.com";
            email["to"] = new Entity[] { entTo };
            email["from"] = new Entity[] { entFrom };
            email["regardingobjectid"] = new EntityReference(ent.LogicalName, ent.Id);
            email["subject"] = subjectTemplate;
            email["description"] = bodyTemplate;
            #endregion

            #region email creation & sending
            try
            {
                var emailid = _service.Create(email);
                SendEmailRequest req = new SendEmailRequest();
                req.EmailId = emailid;
                req.IssueSend = true;
                GetTrackingTokenEmailRequest wod_GetTrackingTokenEmailRequest = new GetTrackingTokenEmailRequest();
                GetTrackingTokenEmailResponse wod_GetTrackingTokenEmailResponse = (GetTrackingTokenEmailResponse)
                                                                 _service.Execute(wod_GetTrackingTokenEmailRequest);
                req.TrackingToken = wod_GetTrackingTokenEmailResponse.TrackingToken;
                _service.Execute(req);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("Email can't be saved / sent." + Environment.NewLine + "Details: " + ex.Message);
            }
            #endregion



        }


        private static string GetDataFromXml(string value, string attributeName)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            XDocument document = XDocument.Parse(value);
            // get the Element with the attribute name specified
            XElement element = document.Descendants().Where(ele => ele.Attributes().Any(attr => attr.Name == attributeName)).FirstOrDefault();
            return element == null ? string.Empty : element.Value;
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top