I am trying to use the below code in my trigger to copy the Notes from my custom object to a newly created record on the Demand object:

List<Note> insertingNotes = new List<Note>();
List<Note> lstNotes = [SELECT Id, Title, ParentId, LastModifiedDate, createdbyid, body FROM Note where ParentId =:rq.Id];

for (Note eachNote : lstNotes){

    Note newDemandNote= new Note();
    newDemandNote.Body = eachNote.Body;                            

    newDemandNote.Id = eachNote.Id;
    newDemandNote.createdBy = string.valueOf(eachNote.CreatedById);
    newDemandNote.LastModifiedDate = eachNote.LastModifiedDate;           
    newDemandNote.ParentId = newD.Id;


if (insertingNotes.size() >0){
    insert insertingNotes;

But I am getting a compile error Illegal assignment from String to User on the line

newDemandNote.createdBy = string.valueOf(eachNote.CreatedById);

I also tried:

newDemandNote.createdBy = eachNote.OwnerId;

but no luck!

so what is the correct way to do this?

2 Answers 2

newDemandNote.createdBy = string.valueOf(eachNote.CreatedById);

change to

newDemandNote.createdById = eachNote.CreatedById;

I believe we can't assign createdById field. This field is not writtable.

You may need to give user to set audit fields permission in user's profile.

I suggest just remove all audit fields lines .. rest code look good to me


The correct API Name is CreatedById:

newDemandNote.CreatedById = eachNote.CreatedById;

Please note that this field is typically not writeable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .