0

So I have two custom objects: The Workload_Unit_Score__c object is a mapping table, with a reference field (WLU_Combination_Value__c) and a value (Score__c) for each row reference. We receive requests each day to process contractual agreements. A new record is created on the Agreement_Title__c object for each request. Each record is assigned a Workload_Unit_Score__c, based on its WLU_Combination_Value__c.

I basically want to do something similar to an excel vlookup - each time we receive a request and a new Agreement_Title__c record is created, I want a trigger to take the WLU_Combination_Value__c, retreive a Score__c from the Workload_Unit_Score__c object, and populate that value in the Workload_Unit_Score__c field. The two custom objects are not related. Below is a summary of the fields.

-Workload_Unit_Score__c object(sort out like a "definition" or "reference" table)

  • Name
  • MiscField1
  • MiscField2
  • MiscField3
  • WLU_Combination_Value__c (a formula field that concatenates MiscField1 + MiscField2 + MiscField3)
  • Score__c (a score designated for each unique WLU_Combination_Value__c)

-Agreement_Title__c object(contractual agreements)

  • Name
  • MiscField1
  • MiscField2
  • MiscField3
  • WLU_Combination_Value__c (a formula field that concatenates MiscField1 + MiscField2 + MiscField3)
  • Workload_Unit_Score__c (a score given to each unique WLU_Combination_Value__c)

I have run the code below but I get "Compile Error: expecting right curly bracket, found '' at line 22 column 0" But I think there may be other issues with the code that wont work.

Can someone assist? Is there an easier way to do this? Sorry I'm an Apex noob...

trigger updateWLUvalue on Agreement_Title__c (before insert) {

    Map<String,Agreement_Title__c[]> relatedScores = new Map<String, Agreement_Title__c[]>();

    for (Agreement_Title__c agmtt : trigger.new) {
        if(!relatedScores.containsKey(agmtt.WLU_Combination_Value__c)){

            relatedScores.put(agmtt.WLU_Combination_Value__c, new Agreement_Title__c[]{});
    }

    relatedScores.get(agmtt.WLU_Combination_Value__C).add(agmtt);

    for(Workload_Unit_Score__c wus : [Select Id, Score__c, WLU_Combination_Value__c 
                                          FROM Workload_Unit_Score__c 
                                          WHERE WLU_Combination_Value__c 
                                          IN : relatedScores.keySet()]){
        for(Agreement_Title__c agmtt2 : relatedScores.get(wus.WLU_Combination_Value__c)){
            agmtt.Workload_Unit_Score__c = wus.Score__c;
            }
        }
}
1
  • There are some missing brackets in the code and the value added in the map is in outside for loop. (line no 11). You need to correct those to make your code workable.
    – Maha
    Commented May 5, 2016 at 5:02

1 Answer 1

0

Change your code like below and try.

    trigger updateWLUvalue on Agreement_Title__c (before insert) {

        Map<String,Agreement_Title__c[]> relatedScores = new Map<String, Agreement_Title__c[]>();

        for (Agreement_Title__c agmtt : trigger.new) {

            if(!relatedScores.containsKey(agmtt.WLU_Combination_Value__c)){

                relatedScores.put(agmtt.WLU_Combination_Value__c, new Agreement_Title__c[]{});
            }

            relatedScores.get(agmtt.WLU_Combination_Value__C).add(agmtt);
        }



        for(Workload_Unit_Score__c wus : [Select Id, Score__c, WLU_Combination_Value__c 
                                              FROM Workload_Unit_Score__c 
                                              WHERE WLU_Combination_Value__c 
                                              IN : relatedScores.keySet()]){

            for(Agreement_Title__c agmtt2 : relatedScores.get(wus.WLU_Combination_Value__c)){

                agmtt2.Workload_Unit_Score__c = wus.Score__c;
            }
        }
    }
2
  • Thanks for your help. I made the changes you mentioned and now I get the following error: "Compile Error: Incorrect SObject type: Agreement_Title__c should be Workload_Unit_Score__c at line 1 column 1" ..I'm not sure what that means
    – Clink
    Commented May 5, 2016 at 5:17
  • you need to go to setup->Create->Objects->Agreement_Title__c->Triggers and click new button from that. I suspect you are creating code from Workload_Unit_Score__c object page. You need to create this trigger from Agreement_Title__c object page. Hope that solves the issue.
    – Maha
    Commented May 5, 2016 at 6:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .