Показать сообщение отдельно
Старый 18.11.2010, 10:05   #7  
CCNP|Leonid is offline
CCNP|Leonid
sys*#**#**#**#**#*
 
9 / 10 (1) +
Регистрация: 15.11.2010
Адрес: Екатеринбург
Thumbs down
Добрый день, уже 2 день не получается.

Вот что получилось. Ткните носом в ошибки плз.

X++:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;

namespace LeadConversionPlugin
{
    public class ContactCreationHandler : IPlugin
    {
        #region IPlugin Members

        public void Execute(IPluginExecutionContext context)
        {
            if (context.MessageName == MessageName.Create &&
                context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is DynamicEntity)
            {
                DynamicEntity entity = (DynamicEntity)context.InputParameters["Target"];

                //Проверяем является ли значение контактом
                if (entity.Name != EntityName.contact.ToString())
                    return;
                // Проверяем есть ли у контакта связь с интересом
                if (!entity.Properties.Contains("originatingleadid"))
                    return;

                //Получаем Guid интереса
                Guid leadid = ((Lookup)entity["originatingleadid"]).Value;

                //получаем Guid контакта
                Guid contactid = (Guid)context.OutputParameters["Id"];

                ICrmService crmservice = context.CreateCrmService(true);

                //Just build the query which will be used to retrieve
                //all child annotations (notes and attachements)
                //QueryByAttribute query = new QueryByAttribute();
                //query.ColumnSet = new AllColumns();
                //query.EntityName = EntityName.annotation.ToString();

                //query.Attributes = new string[] { "objectid" };
                //query.Values = new object[] { leadid };

                QueryExpression relationshipCheckQuery = new QueryExpression();
                relationshipCheckQuery.EntityName = "new_project";
                relationshipCheckQuery.ColumnSet = new ColumnSet(new string[] { "new_projectid" });

                //
                //
                // Code Create Moniker for first Entity: Contact

                Moniker Moniker1 = new Moniker();
                Moniker1.Id = contactid;
                Moniker1.Name = EntityName.contact.ToString();

                // Code Create Moniker for second Entity: New_CustomEntity
                Moniker Moniker2 = new Moniker();
                Moniker2.Id = leadid;
                Moniker2.Name = EntityName.new_project.ToString();

                string strManyToManyRelationshipName = "new_new_project_contactId"; 

                RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                request.Query = query;
                request.ReturnDynamicEntities = false;

                RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmservice.Execute(request);}}


  public bool AssociateManyToManyEntityRecords(Moniker Moniker1, Moniker Moniker2, string strEntityRelationshipName){
    try
    {
        // Create an AssociateEntities request.
        AssociateEntitiesRequest request = new AssociateEntitiesRequest();


        // Set the ID of Moniker1 to the ID of the lead.
        request.Moniker1 = Moniker1;  


        // Set the ID of Moniker2 to the ID of the contact.
        request.Moniker2 = Moniker2; 


        // Set the relationship name to associate on.
        request.RelationshipName = strEntityRelationshipName;


        // Execute the request.
        service.Execute(request); 


        return true;
    }  


    catch (SoapException ex)
    {
        return false;
    }

}
    }
        #endregion IPlugin Members
}
ps. Программист из меня фиговый, работаю системным администратором, нашего прогера, к моему большому горю и сожалению сократили, теперь его адцкую работу приходится делать мне, сейчас активно курю маны по шарпу, в дальнейшем научусь я думаю, просто этот функционал надо срочно, а я тока 3 дня назад приступил к обязанностям программиста.