0

I don't know why my object's field from map doesn't change. Can you help me please?

public void changeCommentAfterUpdate(List<OppDesc__c> newOppDesc){
    Map<Id, Tool__c> mapToConnect = new Map<Id, Tool__c>();
    List<Tool__c> toUpdate = [SELECT Id, Comment__c, Mopp__r.Opportunity__c FROM Tool__c WHERE EndDate__c >=: Date.Today()];

    for (Tool__c t : toUpdate){
        mapToConnect.put(t.Mopp__r.Opportunity__c, t);
    }        
    for(OppDesc__c od : newOppDesc){
        if(od.Comment__c != null && mapToConnect.ContainsKey(od.Opportunity__c)){
            mapToConnect.get(od.Opportunity__c).Comment__c = od.Comment__c;
        }
    }
    update mapToConnect.values(); //After this the tool's comment does't change
}

Maybe is something wrong with my trigger?

trigger ApartmentsCommentTrigger on OppDesc__c (after update) {
TriggerHelper helperClass = new TriggerHelper();

if(Trigger.isAfter && Trigger.isUpdate) {
    helperClass.changeCommentAfterUpdate(Trigger.new);
}
}

P.S. Comment__ is a Long Text Area.

  • Code looks ok. Perhaps there's nothing to update, the list returned by mapToConnect.values() should show what's going on. – identigral Oct 30 '19 at 16:55
  • No. I play a scenario in which there is a change. Perhaps the problem is that there is not just one Tool__c, but a List<Tool__c>. How to do this right with Map<Id, List<Tool__c>>? – Apelsinova Oct 30 '19 at 17:01
  • The compiler would not let you save the class if it was a type mismatch. – identigral Oct 30 '19 at 17:03
  • No, I'm talking about many Cleanings for each Opportunity. So we even don't know what exactly it updates. – Apelsinova Oct 30 '19 at 17:16
3

Agreeing with David, you need to add WHERE clause to your query and also the names of your variables should be meaningful.

Like you mentioned, I think you are missing multiple tool records for the same opportunity.

Map<Id, List<Tool__c>> mapToConnect = new Map<Id, List<Tool__c>>();
List<Tool__c> tools = new List<Tool__c>();

List<Tool__c> toUpdate = [SELECT Id, Comment__c, Mopp__r.Opportunity__c FROM Tool__c];

for (Tool__c t : toUpdate){
    if (t.Mopp__r.Opportunity__c != null) {
        if (!mapToConnect.containsKey(t.Mopp__r.Opportunity__c)) {
            mapToConnect.put(t.Mopp__r.Opportunity__c, new List<Tool__c>());
        }
        mapToConnect.get(t.Mopp__r.Opportunity__c).add(t);
    }
}    

for(OppDesc__c od : newOppDesc){
    if(string.isNotBlank(od.Comment__c) && mapToConnect.ContainsKey(od.Opportunity__c)){
        for (Tool__c tool : mapToConnect.get(od.Opportunity__c)) {
            tool.Comment__c = od.Comment__c;
            tools.add(tool)
        }
    }
}

if(!tools.isEmpty()) {
    update tools;
}
  • Thank you for help, but it still doesn't work. I updated my code and add the trigger's code. Maybe is something wrong with my trigger? – Apelsinova Oct 30 '19 at 22:31
2
List<Tool__c> toUpdate = [SELECT Id, Comment__c, Mopp__r.Opportunity__c FROM Tool__c];

Running an unbounded query like this (no WHERE clause) is risky with regard to several limits, as well as general performance. You should update only those records whose state needs to change based on the trigger event.

Your logic here is likely to be the culprit:

for (Tool__c t : toUpdate){
    mapToConnect.put(t.Mopp__r.Opportunity__c, t);
}        
for(OppDesc__c od : newOppDesc){
    if(od.Comment__c != null && mapToConnect.ContainsKey(od.Opportunity__c)){

Because you don't say that you've gotten a NullPointerException or somesuch, I suspect that mapToConnect.ContainsKey() is always returning false, because either the Mopp__c or Opportunity__c lookup on your Tool__c records is null.

The basic pattern using update someMap.values() is fine.

  • In the loop System.debug(mapToConnect.get(od.Opportunity__c).Comment__c) it shows me the right value. So, mapToConnect.ContainsKey() returns true. And I really don't know why it doesn't work. Maybe because of List of Cleanings for each Opportunity. Yes, I know about WHERE. It's a test version with a few records. – Apelsinova Oct 30 '19 at 18:45
  • I updated my code and add the trigger's code. Maybe is something wrong with my trigger? – Apelsinova Oct 30 '19 at 22:30
  • @Apelsinova I think you need to do some debugging and review logs to understand how records are moving through your code. It's hard to square your description of the behavior with what I'm seeing. – David Reed Oct 30 '19 at 22:32
1

You should check in the second for loop that if Comment__c of custom object OppDesc__c is actually null or blank or have value . If Comment__c is null then actually there is nothing to update.

Add a debug log in second for loop :

for(OppDesc__c od : newOppDesc){

    //checking Comment__c is actually null or blank or have value
    System.debug('comment: @startComment-' + od.Comment__c + '-@endComment');

    if(od.Comment__c != null && mapToConnect.ContainsKey(od.Opportunity__c)){
        mapToConnect.get(od.Opportunity__c).Comment__c = od.Comment__c;
    }
}

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.