Opportunity is the master and CustomObject is detail with multiple CustomObjects linked to one Opportunity. I want to update OpportunityField to equal CustomObjectField from the most recent CustomObject. I think I have the SOQL correct, but I'm not sure what to do in front of or after it to make the field update.

trigger somethingrandom on Opportunity (before insert) 
    OpportunityField var = [
        SELECT CustomObjectField 
        FROM CustomObject 
        ORDER BY CreatedDate
        DESC LIMIT 1 //last two lines to pick the most recent CustomObject            

This just gives me an error "Invalid Type: OpportunityField". [Both of them are numbers, although one is Number(6,0) and the other is Number (18,0)] I also feel like I need a WHERE so that I'm only getting CustomObjects that are linked to the Opportunity, but I'm not sure.

  • Is this even bulk friendly? – denvergreene Jun 25 '14 at 0:07
  • 2
    If CustomObject has a master-detail relationship to Opportunity as the master then there won't be any CustomObject records in the database until after the Opportunity has inserted. I.e. You can't use a before insert trigger on the Opportunity, as the CustomObject records won't exist yet. – Daniel Ballinger Jun 25 '14 at 0:46
  • Do you want this trigger to fire based on the Opportunity being updated, or a CustomObject being created/updated? – Nick Cook Jun 25 '14 at 0:48
  • Opportunity is the Master, Custom Object is the detail. – denvergreene Jun 25 '14 at 14:47
  • I think best practice would be the CustomObject being created/updated – denvergreene Jun 25 '14 at 14:48

You can try something like this -- note bulk-aware as suggested by previous commenters; note after trigger as you need the ids of the Opportunities to do updates.

Looking at your initial question, I might suggest reviewing the Apex Developers Guide - especially the examples on how to write trigger correctly.

Note also I skipped try-catch blocks or partial success processing for clarity.

Note also this will only execute when the Oppo changes value - not when a customObject__c is CRUD'd - if you are trying to sync the Oppo to the most recent custom object, you need to give the Oppo trigger a reason to execute such as:

  • A trigger (after insert, after update, after delete, after undelete) on CustomObject__c sets a custom field value on parent Oppo - 'is_enabled_for_sync__c'
  • A workflow on CustomObject_c does a field update on parent Oppo (although won't work for deletes and undeletes)
  • There is a RSF field on Oppo that sums/counts childObject__c causing the Oppo trigger to fire

    trigger myOppoUpdateTrigger (after insert, after update) {
     // Step 1 - build map of all Oppos and the most recent child obj
     Map<ID,Opportunity> oIdToOppoWCustomObjsMap = new Map<ID,Opportunity> (
      [select id, oppoCustomField__c, (select id, customObjFld__c 
                    from CustomObjects__r
                    order by createdDate desc limit 1)
        from Opportunity where id IN :Trigger.newMap.keySet()]);
     // Step 2 -- go through map and set value in Oppo from child; note that some oppos may have no children       
     for (Opportunity o : oIdToOppoWCustomObjsMap.values())
       o.oppoCustomField__c = o.customObjects__r.size() > 0 
                              ? o.customObjects__r[0].customObjFld__c
                              : null;
     // Step 3 - do DML to update all oppos
     update oIdToOppoWCustomObjsMap.values();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.