function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
XIOXIO 

Apex Trigger to update Lookup field from a text field in Opportunity

Hello,

I have a basic trigger that needs to update a lookup field (Discount_code_LU__c). I'm just trying to map the lookup field with the text field (Discount_code__c). The lookup field (Discount_code_LU__c) is related to the Product object. Any assistance will be greatly appreciated!

Thank you!!
trigger UpdateOpportunityProduct on Opportunity (before insert){
    
 for(Opportunity opp:Trigger.new)
 {
     opp.Discount_code_LU__c =  opp.Discount_code__c ;
          
}
}

 
Best Answer chosen by XIO
PRABHAKARAN CHOCKALINGAMPRABHAKARAN CHOCKALINGAM
My understanding is, you are trying to update a lookup field so as to establish a relation wtih the related Object(Product). The Discount_Code__c field in opp has a value which can be used to relate a record in Product object and you wanted to populate the reference to that Product record in the Opportunity so that it will be listed in the related list for that opportunity.
Based on that below is the code. I couldn't compile the code. Please review and change the field names as required.
trigger UpdateOpportunityProduct on Opportunity (before insert, before update ){

    Map<String, Opportunity> discCodeToOppMap = new Map<String, Opportunity>();

    for(Opportunity opp:Trigger.new)
    {
        discCodeToOppMap.put(opp.Discount_Code__c, opp);

    }

    List<Product2> prodLst = [SELECT ID, ProductCode FROM Product2 where ProductCode in :discCodeToOppMap.keySet()];

    for(Product2 prod : prodLst){
        discCodeToOppMap.get(prod.ProductCode).Discount_code_LU__c = prod.ID;
    }
}

All Answers

PRABHAKARAN CHOCKALINGAMPRABHAKARAN CHOCKALINGAM
My understanding is, you are trying to update a lookup field so as to establish a relation wtih the related Object(Product). The Discount_Code__c field in opp has a value which can be used to relate a record in Product object and you wanted to populate the reference to that Product record in the Opportunity so that it will be listed in the related list for that opportunity.
Based on that below is the code. I couldn't compile the code. Please review and change the field names as required.
trigger UpdateOpportunityProduct on Opportunity (before insert, before update ){

    Map<String, Opportunity> discCodeToOppMap = new Map<String, Opportunity>();

    for(Opportunity opp:Trigger.new)
    {
        discCodeToOppMap.put(opp.Discount_Code__c, opp);

    }

    List<Product2> prodLst = [SELECT ID, ProductCode FROM Product2 where ProductCode in :discCodeToOppMap.keySet()];

    for(Product2 prod : prodLst){
        discCodeToOppMap.get(prod.ProductCode).Discount_code_LU__c = prod.ID;
    }
}
This was selected as the best answer
XIOXIO
Beautiful! Thank you and very much appreciated!!!
Donald Jose PatrisDonald Jose Patris

PRABHAKARAN CHOCKALINGAM if Multiple Discount code for multiple object then it will update only the recent record right?
Donald Jose PatrisDonald Jose Patris
  public static void AccountdataLookup(List<Account> myLeads){
        Set<String> accoutOrgNames = new Set<String>();
        for(Account l : myLeads){
            if(l.SAP_Sales_Org__c == null){
                l.SAP_Sales_Org__c = null;
            } else {
                accoutOrgNames.add(l.SAP_Sales_Org__c);
            }
        }
        Map<String,Id> OrgNameToId = new Map<String,Id>();
        for( SAP_Sales_Org__c c : [SELECT ID, Sales_Org_Code__c FROM SAP_Sales_Org__c WHERE Sales_Org_Code__c IN :accoutOrgNames ]){
            OrgNameToId.put(c.Sales_Org_Code__c,c.id);
        }
        for(Account l : myLeads){
            if(l.SAP_Sales_Org__c != null){
                l.SAPSalesOrg__c = OrgNameToId.get(l.SAP_Sales_Org__c);
            }
        }

    }
shivi Tanwarshivi Tanwar
hey but if prodList is empty then how can we create one product and then create lookup field