I am having 2 objects one is opportunity and test object.

I Created product lookup, pricebook lookup, quantity, amount fields on "test" custom object and saved the record after filling all the required fields. all the fields should be required except Amount. see the following code snippet to achieve your requirements.

trigger on test Object. here consider 2nd object = test__c x = Opportunity

I want to update amount field in opportunity from test__C object

trigger calculateAmount on Test__c (before insert, before update, after insert, after update) 
    set<Id> productidsSet = new set<Id>();

    set<Id> pricebookidsSet = new set<Id>();

    map<String,Decimal> PriceBookEntryMap = new map<String, Decimal>();

    list<PriceBookEntry> pbe_List = new list<PriceBookEntry> ();



                for(Test__c oli : trigger.new){

                pbe_List = [select id, product2Id, pricebook2id, unitprice from pricebookentry 
                where product2Id IN : productidsSet 
                AND pricebook2id IN : pricebookidsSet];
                System.debug('@@@@pbe_List '+pbe_List );

            for (PricebookEntry pbe : pbe_List) {



            for(Test__c oli : trigger.new){

                String productIdPriceBookIdKey = oli.Product__c + string.valueOf(oli.Price_Book__c);
                oli.quantity__c = oli.quantity__c == null ? 1 : oli.quantity__c;
                oli.Amount__c = PriceBookEntryMap.containsKey(productIdPriceBookIdKey ) ? oli.quantity__c * PriceBookEntryMap.get(productIdPriceBookIdKey) : 0;


        set<Id> oppIds = new set<Id>();

        for(Test__c oli : trigger.new) {




    // retrieves all the parents records with child records 
    list<Opportunity> oppList = [select Amount,(select id, amount__c FROM Test__r)  FROM Opportunity WHERE id IN : oppIds];
    Decimal amt = 0; 
for (Opportunity x : oppList) {
amt = 0;
for(Test__c oli : x.Test__r){

amt += oli.amount__c;
x.amount__c = amt;

// Perform DML On X object to update rollup amount
update oppList;


I had tried using this trigger but getting an error

"Didn't understand relationship 'Test__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 56 column 33"

I mean __r field is showing errors

1 Answer 1


You need to use the correct Child Relationship Name, which is typically plural. You can find this information under Create > Objects > Test__c > Opportunity__c. You didn't explicitly state the name of your Opportunity lookup, so I am making an assumption there.

I would expect the correct name to be Tests__r.

  • Hi it is working but i am getting error while updating amount update oppList;
    – swa
    Feb 1, 2015 at 8:17
  • What is the error?
    – Adrian Larson
    Feb 1, 2015 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .