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
Corie DahlkeCorie Dahlke 

Need help creating a simple trigger on the Opportunity object.

I have two custom fields on the Opportunity object, a text field and a lookup field.  The text field gets populated from an outside source and I want to populate the contents of text field  and have it display in the lookup field.

I need help to create a simple trigger for this on the Opportunity object.

**Opportunity object**
text field - Discount_Code__c 
lookup field - Discount_code_LU__c 

Basic logic would be "Discount_Code_LU__c = Discount_Code__c"
Best Answer chosen by Corie Dahlke
Arshadulla.ShariffArshadulla.Shariff
Hello Corie,

Try replacing your trigger with following code.
trigger OpportunityTrigger on Opportunity (before insert, before update) {
    
  List<product2> productlsit2Insert  = new List<product2>();
  for (Opportunity opp : Trigger.new) {
    product2 p = new product2();
    p.Name        =  opp.Discount_Code__c;
    productlsit2Insert.add(p); 
      
  }
      insert productlsit2Insert;
    
    List <product2> productlsit = [SELECT ID, Name from product2 ] ;
        
    map <String,id> product_Map_To_Id  = New map <String,id>  ();
    For (product2 p: productlsit) {
			product_Map_To_Id.put(p.Name, p.id);
    }   
    
    For (Opportunity opp : Trigger.New) {
       opp.Discount_code_LU__c  =  product_Map_To_Id.get(opp.Discount_code__c) ;

    }
    
}

I hope this helps.
Thanks
Arshad

All Answers

EldonEldon
Hi Corie, To which object is your lookup field created? Do you want to select a particular record whose name is equal to value in Discount_Code__c ?

 
Arshadulla.ShariffArshadulla.Shariff
Hello Corie,

Is the Discount_Code__c  field is marked External ID or unique,
Let me know so that I can share the code snippet, built accordingly.

Thanks 
Arshad
Corie DahlkeCorie Dahlke
@Eldon both of these fields are within the Opportunity object.  @Arshadulla.Shariff both are unique.  Thank you for your help!  
Saurabh BSaurabh B
Hi Corle,
Please use below code, it should work.
 
trigger DiscountLooupUpdate on Opportunity (before insert, before update) {
    
  List<Discount_Code__c> Discode  = new List<Discount_Code__c>();
  for (Opportunity o : Trigger.new) {
    Discount_Code__c D = new Discount_Code__c();
    D.Name        =  o.Discount_Code__c;
    Discode.add(D); 
      
  }
      insert Discode;
    
    List <Discount_Code__c> Dlsit = [SELECT ID, Name from Discount_Code__c ] ;
        
    map <String,id> discMap  = New map <String,id>  ();
    For (Discount_Code__c d : Dlsit) {
			discMap.put(d.Name, d.id);
    }   
    
    For (Opportunity o : Trigger.New) {
       o.Discount_code_LU__c  =  discMap.get(o.Discount_code__c) ;

    }
    
}
Please mark this as BEST ANSWER if it helps you!
 
Corie DahlkeCorie Dahlke
Thank you @Saurabh B this is the error i recieved on line 12.  I'm not sure what this means

sObject type 'Discount_Code__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.
Arshadulla.ShariffArshadulla.Shariff

Hello Corie,

Which sObject, is the field  Discount_code_LU__c on opportunity is related To.

Thanks 
Arshad

Saurabh BSaurabh B
Hi Corle, "Discount_Code__c" is the API name of lookup custom object on Opportunity. It seems name of your Discount_code lookup object is different than "Discount_Code__c". What is the API name of your Discount_Code_LU__c lookup object?
EldonEldon
Corie what i asked is the name of the sObject you want to assign to Discount_code_LU__c field.Also do you want to assign an already existing record to that Discount_code_LU__c or create a new record with the name as Discount_Code__c field.?
 
Corie DahlkeCorie Dahlke
On the Opportunity record I have two separate custom fields.  The first is Discount_Code__c that is a text data type.  The second is Discount_code_LU__c which is a data type of a lookup field to the product.
Corie DahlkeCorie Dahlke
@Saurabh B
This is what our API WSDL shows.  Let me know if you need any other info.
<element name="Discount_code_LU__c" nillable="true" minOccurs="0" type="tns:ID"/>
<element name="Discount_code_LU__r" nillable="true" minOccurs="0" type="ens:Product2"/>
Arshadulla.ShariffArshadulla.Shariff
Hello Corie,

Try replacing your trigger with following code.
trigger OpportunityTrigger on Opportunity (before insert, before update) {
    
  List<product2> productlsit2Insert  = new List<product2>();
  for (Opportunity opp : Trigger.new) {
    product2 p = new product2();
    p.Name        =  opp.Discount_Code__c;
    productlsit2Insert.add(p); 
      
  }
      insert productlsit2Insert;
    
    List <product2> productlsit = [SELECT ID, Name from product2 ] ;
        
    map <String,id> product_Map_To_Id  = New map <String,id>  ();
    For (product2 p: productlsit) {
			product_Map_To_Id.put(p.Name, p.id);
    }   
    
    For (Opportunity opp : Trigger.New) {
       opp.Discount_code_LU__c  =  product_Map_To_Id.get(opp.Discount_code__c) ;

    }
    
}

I hope this helps.
Thanks
Arshad
This was selected as the best answer
Corie DahlkeCorie Dahlke
Thank you Arshad!