[SalesForce] Update Trigger not working

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;
            }
        }
    }
}

Best Answer

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

Related Topic