• David Roth 17
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Im very new to SF Dev, and I've written my first trigger Befire Insert for newly created Opp Products.   At the opp level, there is a custom field "Corp Ad Class"  Picklist.  When the Opp is created, this field must be assigned a pickist value. 

The trigger is for each new Opp product created on that opp, I want it to enter the Corp Ad Class from the Opportunity as the default value.   I was unsuccessful accessing the Opportunity field to assign the data.  I had to create a custom formula field to capture the Picklist value first and then I could do the assignment using that field.  I'd like not to have these extra fields if I can aviod them and I assume I can access the parent related record to collect the data as need.

This is what I've come up with :

//Replicates the Opportunity level Corp Ad Class to each product line
trigger StampCorpAdClassNewRec on OpportunityLineItem (Before Insert)
{
for(OpportunityLineItem OpportunityLineItem:Trigger.new)
    {
    OpportunityLineItem.Corp_Ad_Class__c = OpportunityLineItem.OppCorpAdClass__c ;  
    }
}

Opportunity.Corp_Ad_Class__c is what I first placed to the right of the = but it errored.  I was attempting to reference the Master Record field value, Corp Ad Class field on the Opportunity Object.

OpportunityLineItem.OppCorpAdClass__c is a formula field "text(Opportunity.Corp_Ad_Class__c)"; referencing this field, the trigger works, I can make the default value assignment using that field.  Its likely the most efficient way to do this.  I've created an extra field I likely do not need.

Reading other posts on the topic, It appears I may possibly do a SOQL query for the field value then assign it to oppproduct field.   Im not quite sure.    Any feedback for a better would be greatly appreciated.

thank you, David

 
Im very new to SF Dev, and I've written my first trigger Befire Insert for newly created Opp Products.   At the opp level, there is a custom field "Corp Ad Class"  Picklist.  When the Opp is created, this field must be assigned a pickist value. 

The trigger is for each new Opp product created on that opp, I want it to enter the Corp Ad Class from the Opportunity as the default value.   I was unsuccessful accessing the Opportunity field to assign the data.  I had to create a custom formula field to capture the Picklist value first and then I could do the assignment using that field.  I'd like not to have these extra fields if I can aviod them and I assume I can access the parent related record to collect the data as need.

This is what I've come up with :

//Replicates the Opportunity level Corp Ad Class to each product line
trigger StampCorpAdClassNewRec on OpportunityLineItem (Before Insert)
{
for(OpportunityLineItem OpportunityLineItem:Trigger.new)
    {
    OpportunityLineItem.Corp_Ad_Class__c = OpportunityLineItem.OppCorpAdClass__c ;  
    }
}

Opportunity.Corp_Ad_Class__c is what I first placed to the right of the = but it errored.  I was attempting to reference the Master Record field value, Corp Ad Class field on the Opportunity Object.

OpportunityLineItem.OppCorpAdClass__c is a formula field "text(Opportunity.Corp_Ad_Class__c)"; referencing this field, the trigger works, I can make the default value assignment using that field.  Its likely the most efficient way to do this.  I've created an extra field I likely do not need.

Reading other posts on the topic, It appears I may possibly do a SOQL query for the field value then assign it to oppproduct field.   Im not quite sure.    Any feedback for a better would be greatly appreciated.

thank you, David