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.

 

Sunday, May 24, 2020

Trigger to prevent creating duplicate accounts in salesforce

This post explains how to prevent creating duplicate accounts in Salesforce.


Trigger :


trigger Prevent_duplicacy on Account (before insert) {

  Set<string> str=new Set<string>();
    for(Account acc:trigger.new)
    {
        str.add(acc.name);
    }
    
    List<Account> acc= new List<Account>([select name from Account where name in:str]);
    
    for(Account a:trigger.new)
    {
        if(acc.size()>0)
        {
            a.adderror('error');

        }
    }
}




As I already have an account named Microsoft in my org.. When I am trying to create a new account with same the name it shows an error.


Thanks,

 Lovesalesforceyes



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...