+ Start a Discussion

update a Lead field with Product data

I am writing my first apex trigger and need your help please.

Trying to set a flag on lead record by checking if Lead.SP_Model_Number__c belongs to certain family by looking into the product2 table. 

Picked up an example from cookbook and started working on it. But there is no direct relation ship between Lead and the Product2 table except SP_Model_Number__c is  a look-up field on Lead looking up Product2.Model_Number__c.

Kind of stuck and am unable to go anywhere from here. Would appreciate any kind of help. Please find my code below

trigger validateProductFamily on Lead (before update, before insert){

 Set<String> Names = new Set<String>();

    for (Lead lead : Trigger.new) 

         Map<String, Lead> stmodel = new Map<String, Lead>(
   [SELECT SP_Model_Number__c FROM Lead WHERE SP_Model_Number__c in :Names]);

     for (Lead lead : Trigger.new)
    lead.textest__c = stmodel.get(lead.SP_Model_Number__c).product2.Product_Family;    


Message Edited by yespee on 09-13-2008 10:12 AM

Start with this post, http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=342#M342

Keep in mind not to process queries in for loops, and in your trigger you don't need nested for loops to get you code to work.