You need to sign in to do that
Don't have an account?
Tushar Tyagi 4
auto-create Contact record
Hey, team,
I m Facing issue with the below mentioned Bold point.
Please help.
• Create a custom checkbox field on Account and name it ‘Create Contact?’
• Create a number field on Account and name it as ‘No. Of Contacts to create’.
• Create an autonumber field on Account name it as “Account Number”.
• Create a custom number field on Contact and name it as “Contact Count”.
• Each time an account record is saved with checkbox marked as true; with a given number value, that many contact records should be created with following actions
o LastName = Account Number - Contact Count
o FirstName = Account Name
o Phone = Phone
o Billing Address = Billing Address
o Shipping Address = Shipping Address
o Contact Count = which number of contact this is with respect to Account. So for example Account ‘Acme’ already had 5 Contacts and you are creating 6th Contact it should say Contact Count as 6th, 7th etc.
I m Facing issue with the below mentioned Bold point.
Please help.
• Create a custom checkbox field on Account and name it ‘Create Contact?’
• Create a number field on Account and name it as ‘No. Of Contacts to create’.
• Create an autonumber field on Account name it as “Account Number”.
• Create a custom number field on Contact and name it as “Contact Count”.
• Each time an account record is saved with checkbox marked as true; with a given number value, that many contact records should be created with following actions
o LastName = Account Number - Contact Count
o FirstName = Account Name
o Phone = Phone
o Billing Address = Billing Address
o Shipping Address = Shipping Address
o Contact Count = which number of contact this is with respect to Account. So for example Account ‘Acme’ already had 5 Contacts and you are creating 6th Contact it should say Contact Count as 6th, 7th etc.
public class AccountAutomation { public void contactHandler(List<Account> AccList) { try{ List<Contact> ConList = new List<Contact>(); map<id,decimal> AccMap=new map<id,decimal>(); for(Account Acc : AccList) { if(Acc.Create_Contact__c == true) { AccMAp.put(Acc.id, Acc.No_Of_Contacts_to_create__c); if(Accmap.size()>0 && Accmap!=Null) { for(ID AccID : AccMap.keySet()) { for(integer i = 0;i<AccMap.get(Accid);i++) { Contact Con = new Contact(); Con.accountId = Accid; Con.LastName = Acc.Account_Number__c + '-' + con.Contact_Count__c; Con.FirstName = Acc.Name; Con.Phone = Acc.Phone; con.MailingStreet =acc.BillingStreet; con.MailingCity =acc.BillingCity; con.MailingState =acc.BillingState; con.MailingPostalCode =acc.BillingPostalCode; con.MailingCountry =acc.BillingCountry; ConList.add(Con); } } } if(ConList.Size()>0 && ConList!=Null) insert ConList; } } } catch (Exception e) { system.debug('An Exception has occured = '+e); } } }
Con.LastName = acc.Account_Number__c + '-' + sizeCont + numberSuffix;
try to update your code like below
Let us know if this will help you
All Answers
Your code needs to be refactored first and then implement the logic to calculate the St, Nd, Th suffix based on the number.
Find the refactored code below. You just need to add a method (getNumberSuffix(Integer n) ) to calculate the suffix as mentione here (https://salesforce.stackexchange.com/questions/41787/how-can-i-format-a-day-from-a-date-as-2nd-intead-of-2-or-3rd-instead-of-3?answertab=active#tab-top)
I have not tested this code so you may get syntax error.
Let us know if this will help you
Thanks for the solution, But when I am updating the Account with increased value in "No. Of Contacts to create", the count starts again from 0, which makes duplicate contacts.
Like If 4 contacts are there and I update the Account with "No. Of Contacts to create"=2 the, It should make it as 5 and 6 but it is naming them again from 1.
Please check the screen shot
Con.LastName = acc.Account_Number__c + '-' + sizeCont + numberSuffix;
try to update your code like below
Let us know if this will help you
But just want to know.
What if I want to add a manual contact correspond to any account and that contact should follow the same naming convention like we are using above (LastName = Account Number - Contact Count).
so what should I need to do for that?
Do I need to write a separate trigger on account for that or I can achieve that from this trigger too?
Please advice.