You need to sign in to do that
Don't have an account?

Associate contact with account dynamically
Hi All Veterans
We have a requirement wherein we have to write a trigger on contact after update. There will be a field called IsDeleted in contacts, when that field will be selected the contact will automatically get associated with a private account. Now the challenge is we have to create the private account dynamically. Before associating the contact we have to check if that pfivate account contains 10000 contacts if yes then create a new private account and associate that contact with the newly created private account else with the previous account, this process will continue.
Thanks in advance...:)
We have a requirement wherein we have to write a trigger on contact after update. There will be a field called IsDeleted in contacts, when that field will be selected the contact will automatically get associated with a private account. Now the challenge is we have to create the private account dynamically. Before associating the contact we have to check if that pfivate account contains 10000 contacts if yes then create a new private account and associate that contact with the newly created private account else with the previous account, this process will continue.
Thanks in advance...:)
All Answers
I am trying something like this:
trigger CreatePrivateAccount on Contact (Before update)
{
List<Account>PrivateAccountList=new List<Account>();
List<Contact>RelatedContactList=new List<Contact>();
List<Account> LatestPrivateAccount=new List<Account>();
//List<Contact>ContactToScan=[Select Id, IsDeleted__c From Contact Where IsDeleted__c=True];
for (Contact con : Trigger.new)
{
LatestPrivateAccount=[Select Id, Name, CreatedDate from Account Where Id IN:PrivateAccountList ORDER BY CreatedDate DESC Limit 1];
RelatedContactList=[Select Id, LastName from Contact Where AccountID IN: LatestPrivateAccount];
if(con.IsDeleted__c == True)
{
for(integer i=0;i<300;i++)
{
Account acc=new Account();
acc.name= 'Private Account'+i;
if(RelatedContactList.size() <= 10000) PrivateAccountList.add(acc);
}
}
}
insert PrivateAccountList;
for(Account acc: LatestPrivateAccount) con.AccountId= acc.Id;
Update con;
}
the challenge is that in for loop while creating account record we have to check if any Private account is there or not (an identifier will be there in account lets say a checkbox indicating if its a private account), if there is no private account then we have to create one and add the contact to it, if there is private account then we have to check if has its max contact limit of 10000 or not, if max limit is not achieved then we have to attach contact to that account else create new account and attach to it. Account name should be created dynamically with unique name everytime.
thanks for your time and effort, but its not working, not changing the contact account id.
I have made some changes as below and its working fine now. Thanks a lot for your help. :)
trigger ContactTrigger on Contact(before update){
List<Account> accList = [SELECT Id,(SELECT Id FROM Contacts) FROM Account WHERE IsPrivate__c = true Order By CreatedDate DESC Limit 1];
// list of private accounts with less than 10k contact
List<Account> eligibleAccounts = new List<Account>();
// Map of account id vs count of contacts
Map<Id, Integer> accToContactCount = new Map<Id, Integer>();
for(Account acc: accList){
if(acc.Contacts.size() < 10000){
eligibleAccounts.add(acc);
accToContactCount.put(acc.Id, acc.Contacts.size());
}
}
boolean eligibleAccountExists = false;
Account eligibleAccount;
if(eligibleAccounts.size() > 0){
eligibleAccountExists = true;
eligibleAccount = eligibleAccounts[0];
}Else{
Account acc = new Account(Name = 'Private Account', IsPrivate__c = true);
insert acc;
eligibleAccountExists = true;
eligibleAccount = acc;
}
for(Contact con: Trigger.new){
// check if value of IsDeleted is changed from false to true
if(con.IsDeleted__c == True) {
if(eligibleAccountExists == True && accToContactCount.get(eligibleAccount.Id) < 10000){
con.AccountId = eligibleAccount.Id;
//Update con;
Integer existingContactCount = accToContactCount.get(eligibleAccount.Id);
if(existingContactCount == null){
existingContactCount = 0;
}
accToContactCount.put(eligibleAccount.Id, existingContactCount + 1);
}
}
}
Can you advice if below code will generate unique account name everytime its created
You can find the documentation at this link: Math.random (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_math.htm)