+ Start a Discussion
Joe HayesJoe Hayes 

Trigger to get value from another object based on picklist value

Hi,

I am having a bit of trouble and cannot find an answer that fits what I need.

I have 2 custom objects totally unrelated. Assessment__c, and AsstCodes__c.

AsstCodes__c has 2 fields Name and Description. Both are text fields.

What I want to achieve is this:
When I save a new Assessment__c record I want a trigger to update the Assessment__c.Description with the AsstCodes__c.Description.
The common factor is that Assessment__c.Code__c (picklist) will be the same value as a record in AsstCodes__c.Name (text value)

So far I have minimal code for this.

I know I need to do the following:
AsstCodeDescription = [SELECT description__c FROM AsstCodes__c WHERE Assessment__c.Name = AsstCode.Name LIMIT 1];
trigger AsstDesc on Assessment__c (after insert, after update)
{
    map<string, Assessment__c> TheMap = new map<string, Assessment__c>();
        
    for (Assessment__c Desc:trigger.new)
    {
        if (Desc.Name !='')
        Desc.Description__c = AsstCodeDescription;
        update desc;
    }
    
}

I am just not sure how to put it all together.​ Hope this all makes sense.

Thanks for your help
Joe
 
Best Answer chosen by Joe Hayes
Paul S.Paul S.
Assuming that Code__c on the Assessment__c object is unique, you could initially build a map of AsstCodes and then get the value of the Description based on the picklist value (which would correspond to your AsstCode name). 

Also, this may work better in a before context so that you don't have to worry about recursion.
Map<String, String> codeMap = new Map<String, String>();

for(AsstCodes__c ac : [SELECT Name, Description__c FROM AsstCodes__c]{
    codeMap.put(ac.Name, ac.Description__c);
}

for(Assessment__c Desc : trigger.new){
    if(codeMap != NULL){
        Desc.Description__c = codeMap.get(Desc.Code__c);
    }
}

 

All Answers

Paul S.Paul S.
Assuming that Code__c on the Assessment__c object is unique, you could initially build a map of AsstCodes and then get the value of the Description based on the picklist value (which would correspond to your AsstCode name). 

Also, this may work better in a before context so that you don't have to worry about recursion.
Map<String, String> codeMap = new Map<String, String>();

for(AsstCodes__c ac : [SELECT Name, Description__c FROM AsstCodes__c]{
    codeMap.put(ac.Name, ac.Description__c);
}

for(Assessment__c Desc : trigger.new){
    if(codeMap != NULL){
        Desc.Description__c = codeMap.get(Desc.Code__c);
    }
}

 
This was selected as the best answer
Joe HayesJoe Hayes
Thanks Paul that makes sense yeah.
At the moment I'm getting an error on line 10 (Compile Error: Unexpected token 'Desc'. at line 10 column 23)
trigger AsstDesc on Assessment__c (before insert, before update)
{

    Map<String, String> codeMap = new Map<String, String>();

    for(AsstCodes__c ac : [SELECT Name, Description__c FROM AsstCodes__c]){
        codeMap.put(ac.Name, ac.Description__c);
    }

    for(Assessment__c Desc : trigger.new){
        if(codeMap != NULL){
            Desc.Description__c = codeMap.get(Desc.Code__c);
        }
    }
}

Not sure where I'm going wrong.
Thanks
Joe
Paul S.Paul S.
Oh - "desc" is a reserved keyword.  Change desc to something else, like "assess"
Joe HayesJoe Hayes
Ahhhh thanks Paul! It's compiled now.

Thank you