0

I have a requirement in which I have a custom object. I have a text field in that object. When that text field is filled with a value it show create a note in the related list in which i need to map notes & attachments object field "body" with that text area. Can any body help me to achieve it?

Code that I have tried so far:

trigger UpdateNotes on Opportunity (after insert) {
  //Map<Id, Opportunity> opp = new  Map<Id, Opportunity> ();
  if (Trigger.isInsert) {
    for(Opportunity o : Trigger.newMap.keySet()) {
      if(Trigger.oldMap.get(o).TM_test__Next_Update__c != Trigger.newMap.get(o).TM_test__Next_Update__c ) {
        Note n = new Note(
          Title = 'Next Step',
          Body = Trigger.newMap.get(o).TM_test__Next_Update__c, 
          ParentId = Trigger.newMap.get(o).Id
        );
        insert n;    
      }
    }
  }
}

And I'm now getting the error:

Error: Compile Error: Incompatible key type Opportunity for Map at line 13 column 65

1
  • The only map you're using in what you've shown here is Map<Id, Opportunity> opp = new Map<Id, Opportunity> (); which you've commented out. Does it work when it's commented? Where are you using opp you've created? Is that line on line 13? – Dan Jones Dec 16 '15 at 15:09
2

You are trying to use an Opportunity as your key. You need to use the Opportunities Id as your key instead.

trigger UpdateNotes on Opportunity (after insert, after update) {
  if (Trigger.isAfter) {
    //Collection so you can insert your notes in bulk
    List<Note> notes = new List<Note>();
    for(Opportunity o: Trigger.new) {
      //You likely want this check on insert and update but Trigger.old will be null on insert
      if((Trigger.isInsert && o.TM_test__Next_Update__c != null) || Trigger.oldMap.get(o.Id).TM_test__Next_Update__c != o.TM_test__Next_Update__c) {
        notes.add(new Note(
          Title = 'Next Step',
          Body = o.TM_test__Next_Update__c, 
          ParentId = o.Id));
        );   
      }
      insert notes;
    }
  }
}

You have 2 additional problems in here though besides the one reported (I have fixed them in my answer as well):

1) Your trigger fires only on 'insert'. In this case, oldMap will always be null because there never existed the Opportunity previously. You likely want to do this on 'update' instead of insert. If you are doing it on 'insert' then you simply need to check to make sure the value isn't blank to fit your criteria instead.

2) You want to bulkify your trigger. You should never do an insert inside of a loop. You need to place your notes into a collection then insert them all at once.

List<Note> notes = new List<Note>();
...
notes.add(new Note(
          Title = 'Next Step',
          Body = o.TM_test__Next_Update__c, 
          ParentId = o.Id);
...
insert notes;
2
  • I wasn't actually aware of that method of bulkifying inserts. Good to know! – Dan Jones Dec 16 '15 at 15:32
  • @Poet It's actually one of the most important things to do if you are doing Saleforce development. It's very easy to hit governor limits if you don't do it that way. – dphil Dec 16 '15 at 15:40
1

If I understand your question correctly, What you'll probably need to do is write a trigger for this to check whether or not that particular field has changed. If it has, you'd then want to add a note. You wouldn't, for instance, want to add a note every time the record is saved because that could get very spammy.

trigger MyTrigger on MyObject__c (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
  if (Trigger.isBefore) {
    if (Trigger.isInsert) {
      // Basic DML to insert the note would go here, nothing special!
    }
    if (Trigger.isUpdate) {

    }
  }
}

This is a basic trigger that, currently, does nothing. Here though we'll check whether or not a record is inserted or updated.

Inside there you'll want to loop through records, each time checking if there is a difference between data.

for(MyObject__c o : Trigger.newMap.keySet()) {
  if(Trigger.oldMap.get(o.Id).MyField__c != o.MyField__c) {
    // Do something here if they're different!
  }
}

Basically here by using oldMap() and newMap() we're comparing whether or not the field in that particular iteration is different.

If it is, we can now do some pretty simple DML. You'd want to take a look at the Note object and what fields you need to fill in to make it work.

You'd probably need:

  • Title
  • Body
  • ParentId

Title and body are pretty self-explanitory, ParentId is the record the note should be associated with! Easy!

So the DML would look a little like this:

Note n = new Note(
  Title = 'New note from field!',
  Body = o.MyField__c,
  ParentId = o.Id
);

INSERT n;

This might not be perfect but hopefully it goes some way to helping you with your requirement.

Edit: Not to steal any thunder from dphil and only to give a better answer, putting notes into a collection is a much cleaner way of doing things.

Creating a list as below:

List<Note> notes = new List<Note>();

And adding records to that collection so you can perform DML from a single list rather than through every iteration such as below:

for () {  // The for loop you're iterating on
  notes.add(new Note(
    Title = 'New Note from field!',
    Body = o.MyField__c, 
    ParentId = o.Id
  );
}

INSERT notes;
11
  • Yes, You understood the requirement. The thing is i am completely new to developing & I am unable to start over. Could you help? – guest610 Dec 16 '15 at 14:19
  • @guest610 I've pretty much written it all for you in my answer. The only thing you would need to change is the object name where I've written MyObject__c and the field name where I've written MyField__c. :) – Dan Jones Dec 16 '15 at 14:25
  • 1
    @Poet Sorry, I wanted to get my answer in before he spent too much time following yours. The reason I downvoted is because it doesn't address the error he was reporting, which was using the Opportunity as the key for the Map. In the things were explaining, you were actually making the exact same mistake as well. In addition your 'Trigger.oldMap.get(o).MyField__c != Trigger.newMap.get(o).MyField__c' should be 'Trigger.oldMap.get(o.Id).MyField__c != o.MyField__c' because you already have the object you are checking present in that case and I believe makes it more confusing than it needs to. – dphil Dec 16 '15 at 15:38
  • 1
    @Poet I have no idea how to check the edit history actually... How do I do this? All I really see is the error he is reporting and then your solution has the exact same issue. If his question has evolved that much, IMO it should be a separate question if this current question is about the compile error and your answer was addressing something else, but that part isn't your fault. – dphil Dec 16 '15 at 15:43
  • 1
    @dphil You're probably right in that it should have been 2 separate questions as this seems to have gotten slightly confusing. If you click on "Edited [x amount of] minutes ago", that shows the edit history? Glad to have picked up a few very powerful pointers off you though! – Dan Jones Dec 16 '15 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.