0

so I am trying to map a note that a user has entered to one's id. The errors I am getting

Illegal assignment from Map<Id,String> to Map<Id,MTX_Matrix_Note__c> DML requires SObject or SObject list type: Map<Id,MTX_Matrix_Note__c>

Illegal conversion from Map<Id,MTX_Matrix_Note__c> to MTX_Matrix_Note__c

The note is not being added.

 public static MTX_Matrix_Note__c createNewNote(Id candidateId, String enteredNotes ){

        Map<Id, MTX_Matrix_Note__c> usernotes = new Map<Id, String>();

        for (MTX_Matrix_Note__c nt : [SELECT Id, CreatedById, CreatedBy.Name, CreatedDate, Note__c, Name 
                      FROM MTX_Matrix_Note__c WHERE Candidate__c =:candidateId])
        {
            usernotes.put(candidateId, new MTX_Matrix_Note__c( Candidate__c = candidateId,
            Note__c = enteredNotes) );
        }

        insert usernotes;
        return usernotes;
    }

Any changes to my code are welcome, I just want the correct note for each user.

Editing a note is so that a user can edit his or her notes

public static MTX_Matrix_Note__c editNewNote(Id candidateId, String editedentry )
    {

        Map<Id, MTX_Matrix_Note__c> originalNotes = new Map<Id, MTX_Matrix_Note__c>();

        for (MTX_Matrix_Note__c a : [Select Id, Note__c FROM MTX_Matrix_Note__c WHERE Candidate__c =:candidateId])
        {            
            if (a.Note__c != editedentry){
                originalNotes.put(candidateId, 
                                  new MTX_Matrix_Note__c( Candidate__c = candidateId,
                                                                      Note__c = editedentry));
                }
            }
        insert originalNotes.values();

        return originalNotes.values();

    }
  • 1
    didnt you ask this same question earlier today? – glls Apr 10 at 19:04
  • yes I modified my code @glls – Deja Bond Apr 10 at 19:10
  • 1
    @glls I had changed my code a lot and got new errors, so i wasnt sure if i needed to make a new question – Deja Bond Apr 10 at 19:21
3

You have a typo. Instead of

Map<Id, MTX_Matrix_Note__c> usernotes = new Map<Id, String>();

write

Map<Id, MTX_Matrix_Note__c> usernotes = new Map<Id, MTX_Matrix_Note__c>();

You can perform DML operation only over SObject or SObject list So instead of

insert usernotes;

use

insert usernotes.values();

If your method is going to return a map you should change its signature from

public static MTX_Matrix_Note__c createNewNote(Id candidateId, String enteredNotes)

to

public static Map<Id, MTX_Matrix_Note__c> createNewNote(Id candidateId, String enteredNotes)
  • Thank you, I had just changed my code and overlooked the Map<Id, MTX_Matrix_Note__c> usernotes = new Map<Id, MTX_Matrix_Note__c>(); and ty the insert usernotes.values(); worked, I never heard of a Sobject, I'll read up on it. – Deja Bond Apr 10 at 19:19
  • You're welcome) – Oleh Berehovskyi Apr 10 at 19:27
  • One more question, does the second function make sense editNewNote, I constructed it based off of the first function. The goal is that if a user edits a note that was previously created, the edited note will replace the old one? Does my if statement suffice ? – Deja Bond Apr 10 at 20:22
  • No, if you return originalNotes.values() you should change the method signature from MTX_Matrix_Note__c to List<MTX_Matrix_Note__c> because originalNotes.values() method returns a List. – Oleh Berehovskyi Apr 10 at 21:01
  • Does List<MTX_Matrix_Note__c> need to be change everywhere MTX_Matrix_Note__c is ? I understand why you said change the method signature to MTX_Matrix_Note__c bc originalNotes.values() method returns a List., but I don't understand where else it should be changed if editedentry is just a string. – Deja Bond Apr 10 at 21:13

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.