• jasonrtaylor
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies

I am trying to update the parent opportunity record with a count of all child records that meet a specified criteria when a new child custom object is created or modified. I have checked the SOQL queries and they are returning the results I expected. The opportunity relationship field is present and required.

 

trigger UpdatePendingPOMRequests on DM__c (after insert, after update) {

    //Find ID for 'POM' Record Type
    RecordType rt = [Select r.Id from RecordType r WHERE r.name = 'POM' AND r.SobjectType = 'DM__c'];

for(DM__c dm : trigger.new){
       if(dm.RecordTypeId == rt.id){

            integer dmct = [Select Count()
                            FROM DM__c d
                            WHERE dm__c.dm_opportunity_id__c = :dm.dm_opportunity_id__c
                            AND dm__c.RecordTypeId = :rt.id
                            AND dm__c.POM__c = 'Pending'
                            ];           
            Opportunity oid = [Select Num_Pending_NSP__c from Opportunity WHERE id =:dm.dm_opportunity_id__c ];
            oid.Num_Pending_NSP__c = dmct;

        Database.upsert(oid);
       }
    }
}

 

Thanks, in advance for your response.