+ Start a Discussion
Michal SkuhraMichal Skuhra 

Trigger to retrieve record from another object

Hi all,

i´ll try describe my problem. I have 2 objects, Standart (Opportunity) and Custom (Point_of_delivery__c). Both have same fields (EIC_code__c, Number_code__c). but value are only in custom object (Point_of_delivery__c).

In Opportunity IF i set  EIC_code__c, then I need retrieve Number_code__c  
FROM custom object Point_od_delivery__c which have right EIC_code__c. 

My code is right. 

trigger T22 on Opportunity (before insert, before update) {
    Opportunity[] opportunity = Trigger.new;

Point_of_delivery__c Number_code;

Number_code = 
[SELECT Number_code__c
FROM  Point_of_delivery__c
WHERE Point_of_delivery__c.EIC_code__c = :opportunity[0].EIC_code__c
LIMIT 1];

opportunity[0].Number_code__c = Number_code.Number_code__c;

}

BUT:
If I set  wrong EIC_code__c which doesnt exist, I have error. I need set default value if Number_code is null. 

Best Answer chosen by Michal Skuhra
Denis VakulishinDenis Vakulishin
Try this and tell me if it's OK
trigger T22 on Opportunity (before insert, before update) {
    Opportunity[] opportunity = Trigger.new;

    Set<Double> eicCodes = new Set<Double>();
    for(Opportunity opp : Trigger.new)
    {
      if(!iecCodes.contains(opp.EIC_code__c))
        eicCodes.add(opp.EIC_code__c);
    }
    Map<Double, Point_of_delivery__c> eicToPodMap = new Map<Double, Point_of_delivery__c>();

    List<Point_of_delivery__c> pods = 
    [SELECT Number_code__c, EIC_code__c
      FROM  Point_of_delivery__c
      WHERE EIC_code__c IN :eicCodes];
    for(Point_of_delivery__c pod : pods)
    {
      eicToPodMap.put(pod.EIC_code__c, pod);
    }
    for(Opportunity opp : Trigger.new)
    {
      if(eicToPodMap.containsKey(opp.EIC_code__c))
        opp.Number_code__c = eicToPodMap.get(opp.EIC_code__c).Number_code__c;
      else 
        opp.Number_code__c = 0;
    }
}


All Answers

Denis VakulishinDenis Vakulishin
Could you tell me what you're expecting if you set wrong EIC_coode__c (when it cannot find appropriate Point_of_delivery__c object) ?

Also there's core error in your trigger - if trigger will process more than 1 Opportunity, only first will be processed correctly.

Could you tell me what type has EIC_code__c field ?

Michal SkuhraMichal Skuhra
if I set wrong EIC  i´m expecting Number_code value 0, 

EIC code is number type 
Denis VakulishinDenis Vakulishin
Try this and tell me if it's OK
trigger T22 on Opportunity (before insert, before update) {
    Opportunity[] opportunity = Trigger.new;

    Set<Double> eicCodes = new Set<Double>();
    for(Opportunity opp : Trigger.new)
    {
      if(!iecCodes.contains(opp.EIC_code__c))
        eicCodes.add(opp.EIC_code__c);
    }
    Map<Double, Point_of_delivery__c> eicToPodMap = new Map<Double, Point_of_delivery__c>();

    List<Point_of_delivery__c> pods = 
    [SELECT Number_code__c, EIC_code__c
      FROM  Point_of_delivery__c
      WHERE EIC_code__c IN :eicCodes];
    for(Point_of_delivery__c pod : pods)
    {
      eicToPodMap.put(pod.EIC_code__c, pod);
    }
    for(Opportunity opp : Trigger.new)
    {
      if(eicToPodMap.containsKey(opp.EIC_code__c))
        opp.Number_code__c = eicToPodMap.get(opp.EIC_code__c).Number_code__c;
      else 
        opp.Number_code__c = 0;
    }
}


This was selected as the best answer
Michal SkuhraMichal Skuhra
Thx :) what I have to change if EIC (or something else) was text field type ? 
Denis VakulishinDenis Vakulishin
//Double
1)Set<Double> eicCodes = new Set<Double>();
2)Map<Double, Point_of_delivery__c> eicToPodMap = new Map<Double, Point_of_delivery__c>();

//String
1)Set<String> eicCodes = new Set<String>();
2)Map<String, Point_of_delivery__c> eicToPodMap = new Map<String, Point_of_delivery__c>();
These 2 lines

Denis VakulishinDenis Vakulishin
But if your fields will be something like Currency, in code it will remain as Double

Picklist is String, etc