Saturday, May 30, 2020

Trigger to create new contact when new account is inserted

This post explains how to create a new related contact when account is inserted in salesforce.

⇒ " This trigger creates a new contact(With same account name) whenever new account is inserted ..Number of new contact created is depend on NumberofLocations__c(Account field) and all contact name will be Team_AAA"



trigger Contact_Create on Account (after insert) {
list<Contact> New_Contact = new list<Contact>();
map <ID,Decimal> acc = new map<ID,Decimal>();
for(Account a : trigger.new)
{
  acc.put(a.id, a.NumberofLocations__c);
}
   for(Id x : acc.keyset())
   {
      for(Decimal z=0; z<acc.get(x); z++)
      {
       New_Contact.add (new Contact(AccountId=x, LastName='Team_AAA'));
      }
  }
      insert New_Contact;
}

                   Here I've created one new Account with name Microsoft with NumberOfLocation equals 4



After insert this account trigger will fire and create 4 new contact with name Team_AAA with account name Microsoft





    Thanks,
   Lovesalesforceyes.

 

No comments:

Post a Comment

Get selected radio button value in LWC

This post explains how to get selected radio button in LWC. Below is demo of output : To use the radio button in LWC you have to use the  li...