function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CaitlinGMCaitlinGM 

Trigger to increment/decrement field on related object

I received some helpful feedback here to write a trigger on opportunity to update a field on a related contact, linked through a lookup field. I now understand how to do a simple field update on a picklist field on a related object.

 

I now need to write a revised version to actually increment or decrement a counter field (Active_Count__c) on the related contact. If Opp Stage = Active, Active_Count__c needs to be the previous value + 1. If Opp Stage is not Active, Active_Count__c needs to be the previous value -1. I'm not sure how to express the previous value or the addition/subtraction. I have question marks at those places in the code. 

 

Here is what I have so far: 

 

trigger OpportunityRelatedContact2 on Opportunity (after insert, after update) { 
{
Map<Id,Id> instructorsToOppsMap = new Map<Id,Id>();
for(Opportunity A : trigger.new)
           instructorsToOppsMap.put(A.InstructorName__c,A.Id);
 
List<Contact> contactsToUpdate = new List<Contact>{};
      
    for (Contact Instructor: [SELECT Id,Active_Count__c FROM Contact WHERE Id IN:  instructorsToOppsMap.keySet()])
    {
        Id oppId = instructorsToOppsMap.get(Instructor.Id);
        Opportunity opp = trigger.newMap.get(oppId);
            if (opp.StageName=='Active'){
            Instructor.Active_Count__c= ???Active Count value + 1 ;
       }else {
       Instructor.Active_Count__c= ???Active Count value - 1;
             contactsToUpdate.add(instructor);
         }
}
if(contactsToUpdate != null && !contactsToUpdate.isEmpty())
        Database.update(contactsToUpdate);
}
}

 

Thanks for the input!

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

I copy-pasted that as is ! Changed now

 

 

 if (opp.StageName=='Active' && transition){
            Instructor.Active_Count__c= Instructor.Active_Count__c + 1 ;
            contactsToUpdate.add(instructor);
       }else if (opp.StageName != 'Active' && transition ) {
       Instructor.Active_Count__c= Instructor.Active_Count__c - 1;
             contactsToUpdate.add(instructor);
         }

 

All Answers

Ritesh AswaneyRitesh Aswaney
Hey again Caitlin,
Here you go :
trigger OpportunityRelatedContact2 on Opportunity (after insert, after update) { 
{
Map<Id,Id> instructorsToOppsMap = new Map<Id,Id>();
for(Opportunity A : trigger.new)
           instructorsToOppsMap.put(A.InstructorName__c,A.Id);
 
List<Contact> contactsToUpdate = new List<Contact>{};
      
    for (Contact Instructor: [SELECT Id,Active_Count__c FROM Contact WHERE Id IN:  instructorsToOppsMap.keySet()])
    {
        Id oppId = instructorsToOppsMap.get(Instructor.Id);
        Opportunity opp = trigger.newMap.get(oppId);
        //we need to check that this is either an insert, or if an update, then the status has changed to subtract or add
        boolean transition = trigger.IsInsert || (trigger.isUpdate && trigger.oldMap.get(opp.Id).StageName != opp.StageName);
            if (opp.StageName=='Active' && transition){
            Instructor.Active_Count__c= Instructor.Active Count value + 1 ;
            contactsToUpdate.add(instructor);
       }else if (opp.StageName != 'Active' && transition ) {
       Instructor.Active_Count__c= Instructor.Active Count value - 1;
             contactsToUpdate.add(instructor);
         }
}
if(contactsToUpdate != null && !contactsToUpdate.isEmpty())
        Database.update(contactsToUpdate);
}
}
CaitlinGMCaitlinGM

That's helpful; thank you. 

 

I'm getting:

 


Error: Compile Error: expecting a semi-colon, found 'value' at line 15 column 66

 

Is there some other way to write "Instructor.Active Count value + 1 ;"?

Ritesh AswaneyRitesh Aswaney

I copy-pasted that as is ! Changed now

 

 

 if (opp.StageName=='Active' && transition){
            Instructor.Active_Count__c= Instructor.Active_Count__c + 1 ;
            contactsToUpdate.add(instructor);
       }else if (opp.StageName != 'Active' && transition ) {
       Instructor.Active_Count__c= Instructor.Active_Count__c - 1;
             contactsToUpdate.add(instructor);
         }

 

This was selected as the best answer
CaitlinGMCaitlinGM

OK, thanks. I think this works, provided I have a default value of 0 for the Active Count field.