CRM 2015 ExecuteTransactionRquest 与 AssociateRequest 示例?

CRM 2015 ExecuteTransactionRquest with AssociateRequest example?

是否有人有 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);
}

你不需要第二个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);