locked
Clone a record in C# in 2011 RRS feed

  • Question

  • I have a Service Activity record that i want to clone. I have tried this in the constructor:

    this.ActivityId = sa.ActivityId;
    this.CreatedBy.LogicalName = sa.CreatedBy.LogicalName; this.CreatedBy.Id = sa.CreatedBy.Id; this.CreatedBy.Name = sa.CreatedBy.Name; this.StateCode = sa.StateCode; this.StatusCode = sa.StatusCode;

    but when it tries to set the CreatedBy attribute it says that object is null, and it shows a compilation error at the line when it tries to set the StateCode.

    I googled for ways to clone records in 2011, but I can't find anything that helps.

    Thanks

     

     

     

    Tuesday, September 6, 2011 10:42 PM

Answers

  • Created By is usually used by CRM system to mark user who created this record. Why are you using created by? it will be marked automatically. Moreover you should sync owner attribute.

     

    Also it seems your sa entity is missing created by attribute. can you debug and confirm it is there in sa entity?


    Ayaz Ahmad CRM Solutions Architect | http://ayazahmad.wordpress.com
    Tuesday, September 6, 2011 11:27 PM
    Moderator
  • But CreatedBy is not valid for create right ???, then how can we set it, it will give your error.

     


    Mahain : MS CRM Freelancer
    Wednesday, September 7, 2011 5:30 AM
    Moderator

All replies

  • Created By is usually used by CRM system to mark user who created this record. Why are you using created by? it will be marked automatically. Moreover you should sync owner attribute.

     

    Also it seems your sa entity is missing created by attribute. can you debug and confirm it is there in sa entity?


    Ayaz Ahmad CRM Solutions Architect | http://ayazahmad.wordpress.com
    Tuesday, September 6, 2011 11:27 PM
    Moderator
  • Yes, I tried to debug and it shows that "sa" has a value. I need to make an exact copy of the record. I am switching the code from CRM 4 to CRM 2011 and this code seemed to work in CRM 4.
    Tuesday, September 6, 2011 11:29 PM
  • Have you tried :

     

    this.CreatedBy= new SystemUser();


    and then try to assign properties.


    Ayaz Ahmad CRM Solutions Architect | http://ayazahmad.wordpress.com
    Wednesday, September 7, 2011 3:33 AM
    Moderator
  • Ayaz has pointed us to the right direction. The following code might be the one that you are looking for. 

     

                this.CreatedBy = new EntityReference
                {
                    Id = sa.CreatedBy.Id,
                    LogicalName = sa.CreatedBy.LogicalName,
                    Name = sa.CreatedBy.Name
                }; 
                this.StateCode = sa.StateCode;
                this.StatusCode = sa.StatusCode;
    
    

     

    I have removed the first line of code that you had, which I believe will cause problems for you. 

    Hope this helps. 


    Daniel Cai | http://danielcai.blogspot.com
    Wednesday, September 7, 2011 4:03 AM
  • But CreatedBy is not valid for create right ???, then how can we set it, it will give your error.

     


    Mahain : MS CRM Freelancer
    Wednesday, September 7, 2011 5:30 AM
    Moderator
  • Thanks for the replies guys. It doesn't matter what I assign to this.CreatedBy, it always gives an error because CreatedBy is read only.
    • Edited by KatRiketa Wednesday, September 7, 2011 3:13 PM
    Wednesday, September 7, 2011 3:12 PM
  • Thanks for the replies guys. It doesn't matter what I assign to this.CreatedBy, it always gives an error because CreatedBy is read only.


    I had an impression that CRM 2011 allows us to override CreatedBy through providing explicit user id, but I didn't get a chance to test it.

    Based on the exception that you are getting, you will have to use imperonate in order to properly set the CreatedBy field.


    Daniel Cai | http://danielcai.blogspot.com
    Wednesday, September 7, 2011 3:14 PM