1

I have 2 custom objects (Workload_Unit_Score__c, Agreement_Title__c). The WLU_combination_Value__C and Score__c fields are on both objects.

I want a trigger on the Agreement_Title__c object that, once a new record is created, looks for a matching WLU_Combination_Value__c on the Workload_Unit_Score__c object, and returns a Score__c to be populated on the same field on the Agreement_Title__c object.

Whenever I run the code I get the following error:

"Error: Compile Error: Initial term of field expression must be a concrete SObject: Map at line 16 column 39"

Can someone help me?

trigger updateWLUvalue2 on Agreement_Title__c (before insert){

   Map<String,Workload_Unit_Score__c> wus= new Map<String,Workload_Unit_Score__c>();

   for (Agreement_Title__c agmtt : Trigger.new){
      wus.put(agmtt.WLU_Combination_Value__c,null);
   }

   wusByWLU = New Map<String,Workload_Unit_Score__c>();

   for(Workload_Unit_Score__c wus : [
      Select WLU_Combination_Value__c,Score__c 
      FROM Workload_Unit_Score__c
      WHERE WLU_Combination_Value__c
      IN : wus.keySet()
    ]){
      wusByWLU.put(wus.WLU_Combination_Value__c,wus);  
    }

 for(Agreement_Title__c agmtt: Trigger.new){

  if(wusByWLU.containsKey(agmtt.WLU_Combination_Value__c)){
    Workload_Unit_Score__c wluScore = wusByWLU.get(agmtt.WLU_Combination_Value__c);
    agmtt.Workload_Unit_Score__c = wluScore.Score__c;
  }
}
1

When you do these

wus = New Map<String,Workload_Unit_Score__c>([
  Select WLU_Combination_Value__c,Score__c 
  FROM Workload_Unit_Score__c
  WHERE WLU_Combination_Value__c
  IN : wus.keySet()
]);

The key of the map is the id of the record, not the WLU_Combination_Value__c.

You need change these part to:

wusByWLU = New Map<String,Workload_Unit_Score__c>();

for(Workload_Unit_Score__c wus : [
  Select WLU_Combination_Value__c,Score__c 
  FROM Workload_Unit_Score__c
  WHERE WLU_Combination_Value__c
  IN : wus.keySet()
]){
  wusByWLU.put(wus.WLU_Combination_Value__c,wus);  
}

And when you are relating the workload_uit_Score__c with the agreement you need verify that you have a workload_uit_Score__c for that WLU.

for(Agreement_Title__c agmtt: Trigger.new){

  if(wusByWLU.containsKey(agmtt.WLU_Combination_Value__c)){
    Workload_Unit_Score__c wluScore = wusByWLU.get(agmtt.WLU_Combination_Value__c);
    agmtt.Workload_Unit_Score__c = wluScore.Score__c;
  }
}

EDIT 2 TRIGGER COMPLETE:

    trigger updateWLUvalue2 on Agreement_Title__c (before insert){

  Set<String> wluValues = new Set<String>();
  Map<String,Workload_Unit_Score__c> wLU= new Map<String,Workload_Unit_Score__c>();
  for (Agreement_Title__c agmtt : Trigger.new){
    wluValues.add(agmtt.WLU_Combination_Value__c);
  }

  Map<String,Workload_Unit_Score__c> wusByWLU = New Map<String,Workload_Unit_Score__c>();

  for(Workload_Unit_Score__c wus : [
    Select WLU_Combination_Value__c,Score__c 
    FROM Workload_Unit_Score__c
    WHERE WLU_Combination_Value__c
    IN :wluValues
  ]){
    wusByWLU.put(wus.WLU_Combination_Value__c,wus);  
  }

 for(Agreement_Title__c agmtt: Trigger.new){

    if(wusByWLU.containsKey(agmtt.WLU_Combination_Value__c)){
      Workload_Unit_Score__c wluScore = wusByWLU.get(agmtt.WLU_Combination_Value__c);
      agmtt.Workload_Unit_Score__c = wluScore.Score__c;
    }
  }
}
  • Thanks for you help! I made these updates but am getting one (probably minor) error: "Compile error: Duplicate variable: wus at line 11 column 31" Apologies, Apex (triggers especially) are very new to me still... – Clink May 5 '16 at 9:47
  • Here is your code read edit 2 – Torsen May 5 '16 at 9:56
  • 1
    -@Torsen @Ratan I was able to get the code to work with both of your help - thanks so much! – Clink May 5 '16 at 17:12
1

issue in last line ie. agmtt.Workload_Unit_Score__c = wus.Score__c;

this should be

agmtt.Workload_Unit_Score__c = wluScore.Score__c;

or

agmtt.Workload_Unit_Score__c = wus.get(agmtt.WLU_Combination_Value__c).Score__c;

Updates

Add conatainsKey check Id is contains in map or not.

//your rest code

for(Agreement_Title__c agmtt: Trigger.new)
{
    //Add this check so we will get only those records which are present in map 
    if(wus.containsKey(agmtt.WLU_Combination_Value__c))
    {
        Workload_Unit_Score__c wluScore = wus.get(agmtt.WLU_Combination_Value__c);
        agmtt.Workload_Unit_Score__c = wluScore.Score__c;
    }
}
  • Thanks for your help. I tried both and they allowed me to save the trigger, which is progress. Now I am getting the following error message with both, when I tried testing it out by creating a new record on the Agreement_Title__c object: Error: Invalid Data. Review all error messages below to correct your data. Apex trigger updateWLUvalue caused an unexpected exception, contact your administrator: updateWLUvalue: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateWLUvalue: line 16, column 1 – Clink May 5 '16 at 8:17
  • @Clink can you update your question with your latest code and let me review your code.. – Ratan Paul May 5 '16 at 8:32
  • Sure, I have updated it now. Only difference is the bottom line. Thanks again for your help! – Clink May 5 '16 at 8:39
  • @Clink you are welcome.. Please check my updates above. – Ratan Paul May 5 '16 at 8:43
  • Thanks again for you help. I updated the code above and am getting one final error: "Compile error: Duplicate variable: wus at line 11 column 31" Im hoping this is a minor error. Apologies, Apex (triggers especially) are very new to me still... – Clink May 5 '16 at 9: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.