• Andréa de Faria
  • NEWBIE
  • 20 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 2
    Likes Given
  • 4
    Questions
  • 3
    Replies
I added the Apex Class and Apex trigger below to update the field Number_of_Contacts at the Account level when a Contact is added or removed to a certain Account:

Class:
public without sharing class  ContactTriggerHandler {
    
   private Set<Id> getAccountIds(List<Contact> contacts) {
       
       Set<Id> accountIds = new Set<Id>();
       
        for(Contact c : contacts) {
            if(c.AccountId != null) {
                accountIds.add(c.AccountId);
            }
        }
        
        return accountIds;
   }
    
   private Map<Id, Account> getAccountMap(Set<Id> accountIds) {
       
       return new Map<Id, Account>([SELECT Id, Number_of_Contacts__c FROM Account WHERE Id in :accountIds]);
   }
   public void process(List<Contact> contacts, System.TriggerOperation operation) {
       
       Set<Id> accountIds = getAccountIds(contacts);
        
        if(accountIds.size() > 0) {
            Map<Id, Account> accountMap = getAccountMap(accountIds);
            
            for(Contact c : contacts) {
                if(accountMap.containsKey(c.AccountId)) {
                    switch on operation{
                        when AFTER_INSERT {
                            accountMap.get(c.AccountId).Number_of_Contacts__c += 1;
                        }
                        when AFTER_DELETE {
                            accountMap.get(c.AccountId).Number_of_Contacts__c -= 1;
                        }
                        when AFTER_UNDELETE {
                            accountMap.get(c.AccountId).Number_of_Contacts__c += 1;
                        }
                        
                        when AFTER_U {
                            accountMap.get(c.AccountId).Number_of_Contacts__c += 1;
                        }
                        
                    }
                }
            }
            
            update accountMap.values();
        }
       
   }


}
Trigger:
trigger ContactTrigger on Contact (after insert, after delete, after undelete) {
    
    ContactTriggerHandler handler = new ContactTriggerHandler();
    switch on Trigger.operationType {
        when AFTER_INSERT {
            handler.process(Trigger.new, Trigger.operationType);
        }
        when AFTER_DELETE {
            handler.process(Trigger.old, Trigger.operationType);
        }
        when AFTER_UNDELETE {
            handler.process(Trigger.new, Trigger.operationType);
        }
    }
    
}
However, how can I include a line of code that updates the Number_of_Contacs__c field when a Contact moves to a different Account?

Thank you,
 
Hello,

In our company, the Task object have a field called "Meeting Notes", which is the standard "Description" Text field of the Task object, but with the label "Meeting Notes".

Please, how can I copy the value from this field in the most recent Taks created and paste it in a custom Event field called "Pre Meetin Notes".

This Event will also be the latest one created.

The Task and the Event will have the same Contact in common.

I tried with Process Builder and formula fields, but none of these worked.

Appreciate your help.
I built a Flow (Record-Triggered Flow) in Salesforce Flow Builder that updates the fields Contract Start DateContract End Date, Renewal Date and Contract Length (Months) in the Account page.

To update those fields, the flow must take the values from the fields Contract Start DateContract End Date, Next Renewal Date and Contract Term (months) in the most recent Activated Contract related to the Account.

It works well when a new contract is created, however if the Account already has a Contract, the fields in the Account page don't update (i.e. the fields don't update with historical data).

Please, could you help know why does it happen?

I already tried with a AutoLaunch Flow and Process Builder, but they also didn't work.
Thank you,
User-added image
Hello, How are you?

Please, I would like to know if you could help me with the following: how can I prevent the Opportunity Owner from moving an Opportunity from Stage A to Stage B if the field C in the Event related to the Opportunity is different from D.

Being the the field C is a picklist and D is a string?

Thank you a lot!
I added the Apex Class and Apex trigger below to update the field Number_of_Contacts at the Account level when a Contact is added or removed to a certain Account:

Class:
public without sharing class  ContactTriggerHandler {
    
   private Set<Id> getAccountIds(List<Contact> contacts) {
       
       Set<Id> accountIds = new Set<Id>();
       
        for(Contact c : contacts) {
            if(c.AccountId != null) {
                accountIds.add(c.AccountId);
            }
        }
        
        return accountIds;
   }
    
   private Map<Id, Account> getAccountMap(Set<Id> accountIds) {
       
       return new Map<Id, Account>([SELECT Id, Number_of_Contacts__c FROM Account WHERE Id in :accountIds]);
   }
   public void process(List<Contact> contacts, System.TriggerOperation operation) {
       
       Set<Id> accountIds = getAccountIds(contacts);
        
        if(accountIds.size() > 0) {
            Map<Id, Account> accountMap = getAccountMap(accountIds);
            
            for(Contact c : contacts) {
                if(accountMap.containsKey(c.AccountId)) {
                    switch on operation{
                        when AFTER_INSERT {
                            accountMap.get(c.AccountId).Number_of_Contacts__c += 1;
                        }
                        when AFTER_DELETE {
                            accountMap.get(c.AccountId).Number_of_Contacts__c -= 1;
                        }
                        when AFTER_UNDELETE {
                            accountMap.get(c.AccountId).Number_of_Contacts__c += 1;
                        }
                        
                        when AFTER_U {
                            accountMap.get(c.AccountId).Number_of_Contacts__c += 1;
                        }
                        
                    }
                }
            }
            
            update accountMap.values();
        }
       
   }


}
Trigger:
trigger ContactTrigger on Contact (after insert, after delete, after undelete) {
    
    ContactTriggerHandler handler = new ContactTriggerHandler();
    switch on Trigger.operationType {
        when AFTER_INSERT {
            handler.process(Trigger.new, Trigger.operationType);
        }
        when AFTER_DELETE {
            handler.process(Trigger.old, Trigger.operationType);
        }
        when AFTER_UNDELETE {
            handler.process(Trigger.new, Trigger.operationType);
        }
    }
    
}
However, how can I include a line of code that updates the Number_of_Contacs__c field when a Contact moves to a different Account?

Thank you,
 
Hello,

In our company, the Task object have a field called "Meeting Notes", which is the standard "Description" Text field of the Task object, but with the label "Meeting Notes".

Please, how can I copy the value from this field in the most recent Taks created and paste it in a custom Event field called "Pre Meetin Notes".

This Event will also be the latest one created.

The Task and the Event will have the same Contact in common.

I tried with Process Builder and formula fields, but none of these worked.

Appreciate your help.
Hello, How are you?

Please, I would like to know if you could help me with the following: how can I prevent the Opportunity Owner from moving an Opportunity from Stage A to Stage B if the field C in the Event related to the Opportunity is different from D.

Being the the field C is a picklist and D is a string?

Thank you a lot!
Hi folks, I'm a complete newbie here, so please go easy on me. I found some answers that were along the lines of what I was looking for, but didn't really understand how to apply them to my situation. 

I have a multi-select picklist for 'Product' in my Account records, and I need to copy that information in my Contact records. I do not need the information editable in Contacts, and don't even plan on displaying it in the Layout (as I only need it for sync'ing with MailChimp). I do need the field to stay up to date if changes are made in the Account record. I thought a Custom Field with a Formula type was going to be the solution, but it seems that Text format won't play nicely with multi-select fields. 

Look forward to your patience and assistance with this.

Chris
What does org with respect to salesforce.com mean?
How can we create an org here? please mention the steps to do the same.