CRM 2011: Transfer Related Entities between Accounts


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. // Transfer Opportunities
  2. private void TransferOpportunities(OrganizationServiceContext orgContext, Account sourceAccount, Account destinationAccount)
  3. {
  4. var opportunities = orgContext.OpportunitySet.Where(o=>o.CustomerId != null && o.CustomerId.Id == sourceAccount.Id);
  5. if (opportunities != null)
  6. {
  7. foreach (var opportunity in opportunities)
  8. {
  9. var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
  10. // or var er = destinationAccount.ToEntityReference();
  11. opportunity.CustomerId = er;
  12. orgContext.UpdateObject(opportunity);
  13. }
  14. }
  15. }
  16.  
  17. // Transfer Annotations
  18. private void TransferNotes(OrganizationServiceContext orgContext, Account sourceAccount, Account destinationAccount)
  19. {
  20. var notes = orgContext.AnnotationSet.Where(a => a.ObjectId.Id == sourceAccount.Id);
  21. if (notes != null)
  22. {
  23. foreach (var note in notes)
  24. {
  25. var er = new EntityReference(Account.EntityLogicalName, destinationAccount.Id);
  26. note.ObjectId = er;
  27. note.ObjectTypeCode = Account.EntityLogicalName;
  28. orgContext.UpdateObject(note);
  29. }
  30. }
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.