CRM 2015 ExecuteTransactionRquest与AssociateRequest示例?

时间:2016-03-23 11:42:22

标签: transactions microsoft-dynamics dynamics-crm-2015

是否有人在CRM ExecuteTransactionRequest集中有CreateRequest的工作代码片段和后续的AssociateRequest?我正在寻找并且没有运气。

感谢您的帮助: - )

编辑:代码段

var entPetition = new msd_petition();
Petition.MapEntityFromModel(entPetition, mdlPetition);
var reqPetition = new CreateRequest() { Target = entPetition };
_transactionRequest.Requests.Add(reqPetition);

// Add PetitionEvents to transaction
foreach (var mdlEvent in mdlPetition.Events)
{
    var entEvent = new msd_petitionevent();
    PetitionEvent.MapEntityFromModel(entEvent, mdlEvent);
    var reqEvent = new CreateRequest() { Target = entEvent };
    _transactionRequest.Requests.Add(reqPetition);
    // Add Petition-PetitionEvent association
    var assocPetition2PetitionEvent = new AssociateRequest()
    {
        Target = new EntityReference(msd_petition.EntityLogicalName, entPetition.msd_petitionId.Value),
        RelatedEntities = new EntityReferenceCollection
        {
            new EntityReference(sevp_petitionevent.EntityLogicalName, entEvent.msd_petitioneventId.Value)
        },
        Relationship = new Relationship("msd_petition_petitionevent")
    };
    _transactionRequest.Requests.Add(assocPetition2PetitionEvent);
}

1 个答案:

答案 0 :(得分:1)

您不需要第二个AssociateRequest。您只需使用CreateRequestEntity.RelatedEntites属性即可实现相同功能。它将作为单个事务执行。

var entPetition = new msd_petition();
Petition.MapEntityFromModel(entPetition, mdlPetition);

var petitionPetitionEventRelationship = new Relationship("msd_petition_petitionevent");
var petitionRelatedEvents = new EntityCollection();

// Add PetitionEvents to transaction
foreach (var mdlEvent in mdlPetition.Events)
{
    var entEvent = new msd_petitionevent();
    PetitionEvent.MapEntityFromModel(entEvent, mdlEvent);
    petitionRelatedEvents.Entities.Add(entEvent);
}

entPetition.RelatedEntities.Add(petitionPetitionEventRelationship, petitionRelatedEvents);

service.Create(entPetition);