Return to Snippet

Revision: 58003
at July 7, 2012 00:06 by toLL


Updated Code
// Transfer Opportunities
private void TransferOpportunities(OrganizationServiceContext orgContext, Account sourceAccount, Account destinationAccount)
{
    var opportunities = orgContext.OpportunitySet.Where(o=>o.CustomerId != null && o.CustomerId.Id == sourceAccount.Id);
    if (opportunities != null)
    {
        foreach (var opportunity in opportunities)
        {
            var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
            // or var er = destinationAccount.ToEntityReference();
            opportunity.CustomerId = er;
            orgContext.UpdateObject(opportunity);
        }
    }
}

// Transfer Annotations
private void TransferNotes(OrganizationServiceContext orgContext, Account sourceAccount, Account destinationAccount)
{
    var notes = orgContext.AnnotationSet.Where(a => a.ObjectId.Id == sourceAccount.Id);
    if (notes != null)
    {
        foreach (var note in notes)
        {
            var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
            note.ObjectId = er;
            note.ObjectTypeCode = Account.EntityLogicalName;
            orgContext.UpdateObject(note);
        }
    }
}

Revision: 58002
at June 20, 2012 18:00 by toLL


Updated Code
// Transfer Opportunities
private void TransferOpportunities(OrganizationServiceContext orgContext, Account sourceAccount, Account destinationAccount)
{
    var opportunities = orgContext.OpportunitySet.Where(o=>o.CustomerId != null && o.CustomerId.Id == sourceAccount.Id);
    if (opportunities != null)
    {
        foreach (var opportunity in opportunities)
        {
            var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
            opportunity.CustomerId = er;
            orgContext.UpdateObject(opportunity);
        }
    }
}

// Transfer Annotations
private void TransferNotes(OrganizationServiceContext orgContext, Account sourceAccount, Account destinationAccount)
{
    var notes = orgContext.AnnotationSet.Where(a => a.ObjectId.Id == sourceAccount.Id);
    if (notes != null)
    {
        foreach (var note in notes)
        {
            var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
            note.ObjectId = er;
            note.ObjectTypeCode = Account.EntityLogicalName;
            orgContext.UpdateObject(note);
        }
    }
}

Revision: 58001
at June 20, 2012 17:59 by toLL


Initial Code
// Transfer Opportunities
private void TransferOpportunities(CrmServiceContext orgContext, Account sourceAccount, Account destinationAccount)
{
    var opportunities = orgContext.OpportunitySet.Where(o=>o.CustomerId != null && o.CustomerId.Id == sourceAccount.Id);
    if (opportunities != null)
    {
        foreach (var opportunity in opportunities)
        {
            var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
            opportunity.CustomerId = er;
            orgContext.UpdateObject(opportunity);
        }
    }
}

// Transfer Annotations
private void TransferNotes(CrmServiceContext orgContext, Account sourceAccount, Account destinationAccount)
{
    var notes = orgContext.AnnotationSet.Where(a => a.ObjectId.Id == sourceAccount.Id);
    if (notes != null)
    {
        foreach (var note in notes)
        {
            var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
            note.ObjectId = er;
            note.ObjectTypeCode = Account.EntityLogicalName;
            orgContext.UpdateObject(note);
        }
    }
}

Initial URL

                                

Initial Description
How to transfer related entities (Annotation, Opportunity) between two accounts

Initial Title
CRM 2011: Transfer Related Entities between Accounts

Initial Tags

                                

Initial Language
C#