0

I'm trying to update fields on the opportunity object once the fields are updated on a custom case object. I'm getting the trigger to save, but nothing is happening. Can someone help me with the issue?

trigger AsessmentUpdateOpp on PlatformCase__c (after insert) {
    if(Trigger.isAfter) {
        if(Trigger.isInsert){
            for(PlatformCase__c c : Trigger.new) {
                // We need to update associated Opportunity Fields with Fields from Case.
               Opportunity opp = [SELECT Id, Name, Amount, Building_Sq_Footage__c 
                   FROM Opportunity WHERE Id = :c.Opportunity__c ];

               opp.Building_Sq_Footage__c += c.Building_Sq_Footage__c;

               update opp;
            }
        }
    }
}
  • Have you tried inserting a new PlatformCase__c object? I'm asking because in your question you're always mentioning 'update' but your trigger only looks at inserts. – FrankZ Nov 17 '14 at 20:06
  • You're a genius! Thanks so much. I did an insert and it worked so I updated the trigger as the following. – Andrew Sims Nov 17 '14 at 20:40
  • trigger AsessmentUpdateOpp on PlatformCase__c (after update) { if(Trigger.isAfter) { if(Trigger.isUpdate){ for(PlatformCase__c c : Trigger.new) { // We need to update associated Opportunity Fields with Fields from Case. Opportunity opp = [SELECT Id, Name, Amount, Building_Sq_Footage__c FROM Opportunity WHERE Id = :c.Opportunity__c ]; opp.Building_Sq_Footage__c = c.Building_Sq_Footage__c; update opp; } } } } – Andrew Sims Nov 17 '14 at 20:41
3

I am happy that my comment above has helped you to solve your most burning problem. Let me please mention another problem with your code. As the trigger could potentially be called for up to 200 records at once, you need to move the SOQL query out of the loop. Otherwise you might be getting a too many SOQL queries exception - and possibly some performance issues.

Assuming that you only want the trigger to run after update, I suggest you try this code (not tested, please feed back if it works or not):

trigger AsessmentUpdateOpp on PlatformCase__c (after update) {

    Set<Id> oportunityIds = new Set<Id>();
    for (PlatformCase__c c : Trigger.new) {
        oportunityIds.add(c.Opportunity__c);
    }

    List<Opportunity> opps : [SELECT Id, Building_Sq_Footage__c 
            FROM Opportunity 
            WHERE Id in :oportunityIds ]);

    for (Opportunity opp: opps) {
        opp.Building_Sq_Footage__c += c.Building_Sq_Footage__c; 
    }

    update opps;

}

You might also want to think about update and delete triggers, I could imagine they'd be important to keep the data correct. Also, you might want to read up on Roll-Up Summary fields

  • This worked perfect! I don't believe we'll be doing mass updating but it's good to build that way to start with. Thank you Sir. – Andrew Sims Nov 18 '14 at 22:35
0

You can do a system.debug to check what is the value going to 'opp' and another system.debug after the update, to check the updated value for 'opp'.

  • Thank you. I was able to figure it out. The original trigger was on insert not update! Thanks so much for the help. – Andrew Sims Nov 17 '14 at 20:42

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.