0

I am new to Apex coding and trying to build a simple trigger that connects online activity to a checkbox on opportunities. We have a custom object (Online_Activity__c) where records are created automatically by an external system. We'd like a certain online activity to cause a checkbox on the opportunity to go from false to true. However, we only want the checkbox on a certain opportunity to be checked. My code is below-

trigger purlOpp on Online_Activity__c (after insert) {
  for (Online_Activity__c updatePurlOpp : Trigger.new) {
    if (updatePurlOpp.Description__c.startswith ('2015 PURL Page Visit')) {
       updatePurlOpp.What__r.Opportunity.PurlLink = true;
      }
   }
}

My question is where can I add the criteria to only update the opportunity checkbox on opportunities where Year__c = 2015 (picklist value) and Value_Prop__c = "External Product Renewal" (string). The what__r is the account object api name on Online Activity.

  • unlike other parts of SFDC, a trigger context does not include the related objects, e.g. the what__r relationship. You'll need to do SOQL to fetch before you can update – cropredy Jan 27 '15 at 0:44
  • You should avoid ever having code in the trigger body except for checking base conditions (before insert, etc.). Move it out into an Apex Class. – Adrian Larson Jan 27 '15 at 0:46
0

Its fairly straightforward to get you there.

The Code

trigger updateRelatedOppty on Online_Activity__c (after insert) {

    Set<String> oIDs = new Set<String>();
    List<Opportunity> oList = new List<Opportunity>();

    //grab the IDs of the Opptys you want to update
    for(List<Online_Activity__c> tmpList : [SELECT Id, Opportunity__c 
                                            FROM Online_Activity__c 
                                            WHERE Id IN :Trigger.New])
    {
        for(Online_Activity__c tmp : tmpList) {
            oIDs.add(tmp.Opportunity__c);
        }
    }

    //populate a list of Opptys to update
    for(List<Opportunity> tmpList : [Select Id, Rad_Checkbox__c, Year__c, Value_Prop__c FROM Opportunity WHERE Id IN :oIDs AND Year__c = '2015' AND Value_Prop__c LIKE 'External Product Renewal']) {
        for(Opportunity tmp : tmpList) {
            tmp.Rad_Checkbox__c = true;
            oList.add(tmp);
        }    
    }

    //update the Opptys
    try {
        update oList;
    } catch (Exception e) {
        System.debug('Exception: ' + e);       
    }    
}

Recommended Next Steps

In no particular order of importance...
1) This is beta code. Optimize it, best-practice it.
2) Definitely pull ALL of this functionality OUT of the Trigger and into a Class. Call that Class (and other classes) from a master trigger, you only need one (1) trigger per object.
3) Make sure you understand things well enough to write a meaningful Apex unit test with truth assertions (e.g. System.AssertEquals)!
4) Order of execution across multiple Triggers (or multiple code blocks referenced in a single Trigger) can be challenging.
5) It is very important to understand Triggers and their Context Variables.

Best of luck, I hope this helps you get started!

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.