You need to sign in to do that
Don't have an account?
Teju Meshram
Whenever a user updates the account record with rating as Warm then check for its related contact, if there is any related contact then update the account description field with the contact's firstname.
Can you please provide me code for this
Here is my code, but I am getting an error
trigger AccountTrigger on Account (before update) {
if(Trigger.isBefore && Trigger.isUpdate){
AccountTriggerHandler.updateAcct(Trigger.new, Trigger.oldMap);
}
}
public class AccountTriggerHandler {
public static void updateAcct(List<Account> acctList, Map<Id, Account> actMap){
Set<Id> actId = new Set<Id>();
for(Account a : acctList){
if((a.rating != actMap.get(a.id).rating && a.rating == 'Warm')&& (a.Type != actMap.get(a.id).Type && a.Type == 'Prospect')){
actId.add(a.Id);
}
}
Map<Id, Contact> conMap = new Map<Id, Contact>();
for(Contact con :[SELECT id, firstName from Contact where accountId IN : actId]){
conMap.put(con.id, con);
}
for(Account act : [SELECT id, description from Account where Id IN : actId]){
act.Description = conMap.get(act.id).firstname;
}
}
}
Here is my code, but I am getting an error
trigger AccountTrigger on Account (before update) {
if(Trigger.isBefore && Trigger.isUpdate){
AccountTriggerHandler.updateAcct(Trigger.new, Trigger.oldMap);
}
}
public class AccountTriggerHandler {
public static void updateAcct(List<Account> acctList, Map<Id, Account> actMap){
Set<Id> actId = new Set<Id>();
for(Account a : acctList){
if((a.rating != actMap.get(a.id).rating && a.rating == 'Warm')&& (a.Type != actMap.get(a.id).Type && a.Type == 'Prospect')){
actId.add(a.Id);
}
}
Map<Id, Contact> conMap = new Map<Id, Contact>();
for(Contact con :[SELECT id, firstName from Contact where accountId IN : actId]){
conMap.put(con.id, con);
}
for(Account act : [SELECT id, description from Account where Id IN : actId]){
act.Description = conMap.get(act.id).firstname;
}
}
}
What is the error you are seeing? Thanks
But this works only when there is a single related contact and it fails when there is more than one contact
public class PracticeActTrigger {
public static void updtAcct(List<Account> acctList, Map<Id, Account> actMap){
Set<Id> setOfAccctId = new Set<Id>();
for(Account a : acctList){
if((a.rating != actMap.get(a.id).rating && a.rating == 'Warm')&& (a.Type != actMap.get(a.id).Type && a.Type == 'Prospect')){
setOfAccctId.add(a.Id);
}
}
Map<Id,Contact> mapToUpdate = new Map<Id, Contact>();
if(!setOfAccctId.isEmpty()){
for(Account acc : [Select id, description, (SELECT id, firstname, lastName from Contacts) from Account where id IN : setOfAccctId]){
mapToUpdate.put(acc.Id, acc.contacts);
}
}
if(!mapToUpdate.isEmpty())
for(Account a : acctList){
if(mapToUpdate.containsKey(a.id)){
a.Description = mapToUpdate.get(a.id).firstName;
}
}
}
}
Hi Teju,
Try below code to update the Account's description field with the related contact's firstName
public class PracticeActTrigger {
public static void updtAcct(List<Account> acctList, Map<Id, Account> actMap) {
Set<Id> setOfAccctId = new Set<Id>();
for (Account a : acctList) {
if ((a.Rating != actMap.get(a.Id).Rating && a.Rating == 'Warm') && (a.Type != actMap.get(a.Id).Type && a.Type == 'Prospect')) {
setOfAccctId.add(a.Id);
}
}
Map<Id, String> accountToContactNamesMap = new Map<Id, String>();
if (!setOfAccctId.isEmpty()) {
for (Contact c : [SELECT Id, FirstName, AccountId FROM Contact WHERE AccountId IN :setOfAccctId]) {
if (!accountToContactNamesMap.containsKey(c.AccountId)) {
accountToContactNamesMap.put(c.AccountId, c.FirstName);
} else {
String contactNames = accountToContactNamesMap.get(c.AccountId);
contactNames += ', ' + c.FirstName;
accountToContactNamesMap.put(c.AccountId, contactNames);
}
}
}
for (Account a : acctList) {
if (accountToContactNamesMap.containsKey(a.Id)) {
a.Description = accountToContactNamesMap.get(a.Id);
}
}
}
}
Mark this as best answer if this helps.
Thanks