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
steve456steve456 

Help needed with a trigger

I have three custom objects Product , Order ,  Order Entry

 

 

Order Entry has master detail with Order  and a  lookup to Product

 

 

I have a field called Revenue on Order Entry which needs to be  calculated based on  the  Category of Order 

 

 

I mean to say on Order I have a field called  Category  and based on this field I need to calculate the revenue  on Order Entry 

 

************

Trigger should execute upon Order Entry creation and whenever Order__r.Category__c is changed according to the following logic:

 

 

2.       IF(Order__r.Category__c <> "Event", Product__r.Invoice_Rate_Blend__c * Quantity__c, Product__r.Invoice_Rate_Event__c * Quantity__c)

 

 

 

I am struck as how do I do it do it  on an insert as well as an  update  as the Order field  is not changed  on the Order Entry but a particular field on Order is changed .

 

Best Answer chosen by Admin (Salesforce Developers) 
zettahertzzettahertz

oh, Can you check the master detail field (Order) on the Order Entry object.

 

There's a "Child Relationship Name" in that field that the query supposed to use.  The document/wiki seems wrong.

 

Should be something like

 Child Relationship Name:Order_Entries 
 

If it is, you just append __r to it in the SOQL query, making it "Order_Entries__r"

All Answers

Juan SpagnoliJuan Spagnoli

I think you need two Triggers, one for Order and other one for Order Entry. If both triggers have to do the same thing, you can create an Apex Class with all the business logic inside and both triggers uses it.

 

Best Regards

steve456steve456

Insert part I am ok with it.Only issue is with the update

zettahertzzettahertz

Not sure of the exact use case for your scenario, but can you just use a formula field? Unless you want to allow the user to override the value in the field.

 

Not sure what the existing insert trigger is like but you also need an update trigger on Order__c.  

 

Something along the lines of:


trigger updateOrderEntriesByCategory on Order__c (after update){

     List<OrderEntry__c> listOfOrderEntriesToUpdate = new List<OrderEntry__c>();

     List<Id> listOfUpdatedOrderIds = new List<Id>();

     for (Order__c order : Trigger.new){

          if (order.Category__c != Trigger.oldMap.get(order.id).Category__c){ // perform update only if category is changed

               listOfUpdatedOrders.add(order.id);

          }

     }

 

     for (Order__c updatedOrder : [select id, (select id, Product__r.Invoice_Rate_Blend__c, Quantity__c, Revenue__c from orderEntries__r) from Order__c where id in :listOfUpdatedOrders]){

          for (OrderEntry__c entry : updatedOrder.orderEntries__r){

                if (updatedOrder.Category__c != 'Event' ){

                     entry.Revenue__c = entry.Product__r.Invoice_Rate_Blend__c * entry.Quantity__c;

                } else {

                     entry.Revenue__c = entry.Product__r.Invoice_Rate_Event__c * entry.Quantity__c;

                } 

                listOfOrderEntriesToUpdate.add(entry);

          }

     }

 

     update listOfOrderEntriesToUpdate;

}

 

This code isn't compiled / tested but should give you an idea of how your trigger can be.

steve456steve456

   for (Order__c updatedOrder : [select id, (select id, Product__r.Invoice_Rate_Blend__c, Quantity__c, Revenue__c from orderEntries__r) from Order__c where id in :listOfUpdatedOrders]){

          for (OrderEntry__c entry : updatedOrder.orderEntries__r){

 

 

 

I could understand the SOQL query and I get errors with this 

zettahertzzettahertz

Try changing "from orderEntries__r" to "Order_Entries__r" (I'm guessing you're using the default naming convention w/ the underscores instead of CameCase)

 

And the for statement needs to be changed accordingly to:
for (Order_Entry__c entry : updatedOrder.Order_Entries__r)

 

Take a look at this doc in the 

"Understanding Relationship Names, Custom Objects, and Custom Fields" section

The inner query needs the name of the object (Order Entry) in plural.

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

steve456steve456

the custom object api names are Order_Entry_c and Order__c.I donot have any object with  Order_Entries__r

steve456steve456

Error: Compile Error: Didn't understand relationship 'Order_Entries__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 12 column 40

zettahertzzettahertz

Yeah, it's referring to the Order_Entry__c object in plural form.  Sorry, actually, it should've been OrderEntries__r as originally mentioned assuming that the plural label is setup correctly

To verify what the plural is set to, do this:
1) Go to Setup > Create > Objects

2) Click on Order Entry

3) Check the "Plural Label" of the object

 

http://wiki.developerforce.com/page/Programming_Techniques

in "Using Nested SOQL Statements" section

 

steve456steve456

 

Still i am getting an error 

 

 

Error: Compile Error: Didn't understand relationship 'OrderEntries__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 12 column 40

 

 

I have used Order_Entries__r and also OrderEntries__r .None of them are working

 

Singular Label Order Entry Description Detail (product level) sales order data. Plural Label Order Entries

zettahertzzettahertz

oh, Can you check the master detail field (Order) on the Order Entry object.

 

There's a "Child Relationship Name" in that field that the query supposed to use.  The document/wiki seems wrong.

 

Should be something like

 Child Relationship Name:Order_Entries 
 

If it is, you just append __r to it in the SOQL query, making it "Order_Entries__r"

This was selected as the best answer