+ Start a Discussion
Thomas Bille RasmussenThomas Bille Rasmussen 

Help with IdeaComment Trigger

I'm attempting to add a Trigger on the IdeaComment object to update a custom text area field on the parent idea object. However I keep getting the following error: "Error: Compile Error: Expression cannot be assigned at line -1 column -1"

Please help. Here's the copy of trigger:

trigger test on IdeaComment (before insert) {

for (IdeaComment obj: trigger.new){
    Idea.Last_Comment__c = IdeaComment.CommentBody;
  }
}
Best Answer chosen by Thomas Bille Rasmussen
bob_buzzardbob_buzzard
There's a few issues here:

(1) You are referring to the Idea sobject rather than an instance of idea when updating the Last_Comment__c field.  The same applies to the IdeaComment you are assigning from, presumably this should be obj.

(2) You can't simply update a related record in a trigger (in this case the parent idea), you have to query those back from the database.

(3) You need to save the related records once they are changed, as they aren't part of the records being updated by the trigger.

I think the following should be pretty close :

trigger test on IdeaComment (before insert) {
  // find all the idea ids
  Set<Id> ideaIds=new Set<Id>();
  for (IdeaComment obj: trigger.new){
   ideaIds.add(obj.ideaId);
  }

  // query back the ideas
  Map<Id, Idea> ideasByid=new Map<Id, Idea>([select id, Last_Comment__c from Idea where id in :ideaIds]);
 
  // now iterate the idea comments, applying the comment to the last_comment__c field
  for (IdeaComment obj: trigger.new){
    Idea cand=ideasById.get(obj.ideaId);
    if (null!=cand) {
      cand.Last_Comment__c = obj.CommentBody;
    }
  }
 
  // finally update the ideas in the database
  update ideasById.values();
}

All Answers

bob_buzzardbob_buzzard
There's a few issues here:

(1) You are referring to the Idea sobject rather than an instance of idea when updating the Last_Comment__c field.  The same applies to the IdeaComment you are assigning from, presumably this should be obj.

(2) You can't simply update a related record in a trigger (in this case the parent idea), you have to query those back from the database.

(3) You need to save the related records once they are changed, as they aren't part of the records being updated by the trigger.

I think the following should be pretty close :

trigger test on IdeaComment (before insert) {
  // find all the idea ids
  Set<Id> ideaIds=new Set<Id>();
  for (IdeaComment obj: trigger.new){
   ideaIds.add(obj.ideaId);
  }

  // query back the ideas
  Map<Id, Idea> ideasByid=new Map<Id, Idea>([select id, Last_Comment__c from Idea where id in :ideaIds]);
 
  // now iterate the idea comments, applying the comment to the last_comment__c field
  for (IdeaComment obj: trigger.new){
    Idea cand=ideasById.get(obj.ideaId);
    if (null!=cand) {
      cand.Last_Comment__c = obj.CommentBody;
    }
  }
 
  // finally update the ideas in the database
  update ideasById.values();
}
This was selected as the best answer
Thomas Bille RasmussenThomas Bille Rasmussen
Thank you Bob. I really appreciate your help and you taking the time to explain in detail how you approched this problem.