You need to sign in to do that
Don't have an account?
Rvsk
Trigger Scenario : Creates the number of contacts which are equal to the number which we will enter in the Number of Locations field on the Account Object.
I have created a Custom field called “Number of Locations” on the Account Object (Data Type=Number)
Object : Account
Trigger: After Insert
This is my code:
trigger ContactsCreation on Account (after insert) {
list<contact> listContact = new list<contact>();
map<id,decimal> mapAcc = new map<id,decimal>();
for(Account acc:trigger.new){
mapAcc.put(acc.id,acc.NumberofLocations__c);
}
if(mapAcc.size()>0 && mapAcc!=null){
for(Id accId:mapAcc.keyset()){
for(integer i=0;i<mapAcc.get(accId);i++){
contact newContact=new contact();
newContact.accountid=accId;
newContact.lastname='contact'+i;
listContact.add(newContact);
}
}
}
if(listContact.size()>0 && listContact!=null)
insert listContact;
Can someone please explain the logic behind the no of creation of contact records based on location field.
Sample example: I have given account name,no of locations=2 in account object, then after two contacts record got created with lastname as contact0,contact1.
Why multiple contacts are creating as per the above logic?
Object : Account
Trigger: After Insert
This is my code:
trigger ContactsCreation on Account (after insert) {
list<contact> listContact = new list<contact>();
map<id,decimal> mapAcc = new map<id,decimal>();
for(Account acc:trigger.new){
mapAcc.put(acc.id,acc.NumberofLocations__c);
}
if(mapAcc.size()>0 && mapAcc!=null){
for(Id accId:mapAcc.keyset()){
for(integer i=0;i<mapAcc.get(accId);i++){
contact newContact=new contact();
newContact.accountid=accId;
newContact.lastname='contact'+i;
listContact.add(newContact);
}
}
}
if(listContact.size()>0 && listContact!=null)
insert listContact;
Can someone please explain the logic behind the no of creation of contact records based on location field.
Sample example: I have given account name,no of locations=2 in account object, then after two contacts record got created with lastname as contact0,contact1.
Why multiple contacts are creating as per the above logic?
multiple contacts are creating because of you used in the condition for(integer i=0;i<mapAcc.get(accId);i++)
for example-you input 2 in the no.of location so it fullfil this condition if(mapAcc.size()>0 && mapAcc!=null) and ,
from the above condition for (integer i=0)the contact will start with the index/serial no.0[contact0]
and,for(i<mapAcc.get(accid);i++) the contacts which gonna print will be less than/not equal to 2 the number,
you input in no.of location field ,
after that
if(listContact.size()>0 && listContact!=null)
insert listContact; that means you put the size of contact(2) in the list .
so the contact0 and contact1 created.
if you get your answer,mark this best answer or like.
It is working Try once,
ApexHandler
public void accrelatedContact(list<account> lstNewAcc)
{
list<contact> lstContact = new list<contact>();
For(Account ac : lstNewAcc)
{
if(ac.NumberofLocations__c >0)
{
for(Integer i=1 ; i<=ac.NumberofLocations__c;i++){
Contact con = new Contact();
con.AccountId = ac.id;
con.LastName = ac.Name + i;
lstContact.add(con);
}
}
}
if(lstContact.size()>0)
{
insert lstContact;
}
}
This is Trigger
trigger AccountTrigger on Account (After insert) {
AccountHandler achndlr = new AccountHandler();
if(trigger.isAfter && Trigger.isinsert)
{
achndlr.accrelatedContact(Trigger.new);
}
if you get your answer,mark this best answer or like.
trigger NoOfLoctnCon on Account (after insert) {
list<Contact> conlist = new list<Contact>();
for(Account a:trigger.new){
if(a.NumberofLocations__c != null){
Decimal i = a.NumberofLocations__c ;
for(integer j=1; j<=i; j++){
Contact c = new Contact();
c.LastName = a.Name+i;
c.AccountId = a.Id;
conlist.add(c);
}
}
insert conlist;
}
}
In account there is No_of_contact__c custom field, when we are giving No 5 in no.Of.contact field automatically create a 5 contact records, when we are change or increasing the number 5 to 7 then automatically 2 contact records has automatically created that means before 5+ 2= Total 7 records. Similarly if we change or decrease the value from 7 to 4 then automatically 4 contact records should be delete and remain 3 records that means 7-4 = 3.
Please anyone help for logic on update and delete operations
trigger accountTrigger on Account (after insert) {
List<contact> cons=new List<contact>();
for(account a : trigger.new){
for(integer i=0; i<a.Number_of_contacts__c; i++){
contact cc = new contact();
cc.accountid=a.id;
cc.lastname=a.name;
cc.phone=a.phone;
cons.add(cc);
}
insert cons;
}
}