You need to sign in to do that
Don't have an account?
Bryan Cerrati
Contact and Account Map
Upon update of a date field on an Account, i would like all contacts that have the checkbox "Admin Contact" selected to update the same date field.
i cannot see what i am doing wrong. i have looked up every single post and help forum i can find. documentation does not help. please dont post links, im sure ive seen it.
tell me how i can get this to actually fire?
Class:
i cannot see what i am doing wrong. i have looked up every single post and help forum i can find. documentation does not help. please dont post links, im sure ive seen it.
tell me how i can get this to actually fire?
Class:
public with sharing class AdminUsersAccount { public void leadAlerts(List<Account> lstAcct) { sendAlertsPrivate(lstAcct); } private void sendAlertsPrivate(List<Account> lstAcct) { List<Contact> lstUpdateCon = new List<Contact>(); map<Id, Contact> conMap = new map<Id, Contact>(); for(Account a : lstAcct) conMap.put(a.Id, null); conMap.remove(null); conMap.putAll([SELECT Id, Admin_User__c, Account_Expiration__c FROM Contact WHERE Admin_User__c = true AND AccountId In : conMap.keySet()]); for(Account acc : lstAcct) { if(conMap.containsKey(acc.Id)) { if(acc.Approved_Date__c != null) { Date tdy = date.today(); Date expire = acc.Approved_Date__c.addDays(365); boolean approved = expire >= tdy; lstUpdateCon.add(new Contact(Account_Expiration__c = approved?conMap.get(acc.Id).Account_Expiration__c: null)); } else { lstUpdateCon.add(new Contact(Account_Expiration__c = null)); } } } if(!lstUpdateCon.isEmpty()) update lstUpdateCon; } }
Please Try out the below code , it will help you
public with sharing class AdminUsersAccount
{
public void leadAlerts(List<Account> lstAcct)
{
sendAlertsPrivate(lstAcct);
}
private void sendAlertsPrivate(List<Account> lstAcct)
{
set<id> accIds = new set<id>();
Map<id,account> AccMap = new Map<id,account> ();
Map<id,List<Contact>> ConAccMap = new Map<id,List<Contact>>();
List<contact> updateContact = new List<contact>();
for(Account Acc:lstAcct )
{
accIds.add(Acc.id);
AccMap.put(Acc.id,Acc);
}
List<contact> contactsList= [Select id ,Admin_User__c,AccountId, Account_Expiration__c FROM Contact where Admin_User__c = true AND AccountId In :accIds];
// creating map with account id as key and contact as list
for(Contact contacts:contactsList)
{
if(ConAccMap.containsKey(contacts.AccountId))
{
ConAccMap.get(contacts.AccountId).add(contacts);
}
else
{
ConAccMap.put(contacts.AccountId,new list<contact>{contacts});
}
}
for(id accountID:ConAccMap.keyset())
{
if(AccMap.get(accountID).Approved_Date__c != null)
{
if(ConAccMap.get(accountID)!=null)
{ //quering all contact related to Account id
List<contact> contList = ConAccMap.get(accountID);
for(contact Contacts:contList)
{
if(Contacts.Admin_User__c== true)
{
Contacts. Account_Expiration__c=AccMap.get(accountID).Approved_Date__c;
updateContact .add(Contacts);
}
}
}
// if there is no Contact // you can write you logic here
else
{
}
}
}
update updateContact;
}
}
All Answers
I found few errors in your code like
@14 conMap.remove(null); this method doesn't exist
// conMap.remove(key) where key in your case should be valid account id in the map
@15
conMap.putAll(new Map<id,Contact>[SELECT Id, Admin_User__c, Account_Expiration__c FROM Contact WHERE Admin_User__c = true AND AccountId In : conMap.keySet()]);
Also the last statement
@33,34 if this method is called in before trigger ,it mayn't work
Please let me know if you face any further issues
Thanks and Regards,
Shiva RV
I think there is a problem in your code at line number 15
conMap.putAll([SELECT Id, Admin_User__c, Account_Expiration__c FROM Contact WHEREAdmin_User__c = true AND AccountId In : conMap.keySet()]);
This will replace your map <account.id,contact> to Map<contact.id,contact>. If you want to create Map<account,contact> then tou have to query list of contact and then you have to use for loop for contact list and create map.
Or you can use Inner query so it will give you list of account with realted Contacts.
Hope this will help you.
Thanks
Shweta
Please Try out the below code , it will help you
public with sharing class AdminUsersAccount
{
public void leadAlerts(List<Account> lstAcct)
{
sendAlertsPrivate(lstAcct);
}
private void sendAlertsPrivate(List<Account> lstAcct)
{
set<id> accIds = new set<id>();
Map<id,account> AccMap = new Map<id,account> ();
Map<id,List<Contact>> ConAccMap = new Map<id,List<Contact>>();
List<contact> updateContact = new List<contact>();
for(Account Acc:lstAcct )
{
accIds.add(Acc.id);
AccMap.put(Acc.id,Acc);
}
List<contact> contactsList= [Select id ,Admin_User__c,AccountId, Account_Expiration__c FROM Contact where Admin_User__c = true AND AccountId In :accIds];
// creating map with account id as key and contact as list
for(Contact contacts:contactsList)
{
if(ConAccMap.containsKey(contacts.AccountId))
{
ConAccMap.get(contacts.AccountId).add(contacts);
}
else
{
ConAccMap.put(contacts.AccountId,new list<contact>{contacts});
}
}
for(id accountID:ConAccMap.keyset())
{
if(AccMap.get(accountID).Approved_Date__c != null)
{
if(ConAccMap.get(accountID)!=null)
{ //quering all contact related to Account id
List<contact> contList = ConAccMap.get(accountID);
for(contact Contacts:contList)
{
if(Contacts.Admin_User__c== true)
{
Contacts. Account_Expiration__c=AccMap.get(accountID).Approved_Date__c;
updateContact .add(Contacts);
}
}
}
// if there is no Contact // you can write you logic here
else
{
}
}
}
update updateContact;
}
}