You need to sign in to do that
Don't have an account?
Nabeel Munir 9
Update a custom field in account after insert
Hello,
I am a newbie in salesforce and practicing apex triggers using the following scenerio.
Pre-Reqs:
Create a field on Account called “Only_Default_Contact”, checkbox, default off
Assignment:
When a new Account is created, create a new Contact that has the following data points:
First Name = “Info”
Last Name = “Default”
Email = “info@websitedomain.tld”
Only_Default_Contact = TRUE
When the Account has more than 1 Contact, update Only_Default_Contact to FALSE.
So far I have only been able to create contacts related to account after insert. But, since I know that after insert, Account becomes read only. I needed a way to update the Only_Default_Contact__c field.
Below is what I have tried but it does not seem to work.
trigger createAccount on Account (after insert,before update)
{
List<Contact> lst = new List<Contact>();
Contact con = new Contact();
if(trigger.isAfter && trigger.isInsert)
{
for(Account acc:trigger.new)
{
con.AccountId = acc.Id;
con.FirstName = 'Info';
con.LastName = 'Default';
con.Email = 'info@websitedomain.tld';
lst.add(con);
}
}
if(!lst.isEmpty())
{
insert lst;
}
if(trigger.isBefore && trigger.isUpdate)
{
List<Account> ls = [Select Only_Default_Contact__c from Account];
for(Account a: trigger.new)
{
if(lst.size()==1)
{
a.Only_Default_Contact__c = TRUE;
update a;
}
else
{
a.Only_Default_Contact__c = FALSE;
update a;
}
}
}
}
I am a newbie in salesforce and practicing apex triggers using the following scenerio.
Pre-Reqs:
Create a field on Account called “Only_Default_Contact”, checkbox, default off
Assignment:
When a new Account is created, create a new Contact that has the following data points:
First Name = “Info”
Last Name = “Default”
Email = “info@websitedomain.tld”
Only_Default_Contact = TRUE
When the Account has more than 1 Contact, update Only_Default_Contact to FALSE.
So far I have only been able to create contacts related to account after insert. But, since I know that after insert, Account becomes read only. I needed a way to update the Only_Default_Contact__c field.
Below is what I have tried but it does not seem to work.
trigger createAccount on Account (after insert,before update)
{
List<Contact> lst = new List<Contact>();
Contact con = new Contact();
if(trigger.isAfter && trigger.isInsert)
{
for(Account acc:trigger.new)
{
con.AccountId = acc.Id;
con.FirstName = 'Info';
con.LastName = 'Default';
con.Email = 'info@websitedomain.tld';
lst.add(con);
}
}
if(!lst.isEmpty())
{
insert lst;
}
if(trigger.isBefore && trigger.isUpdate)
{
List<Account> ls = [Select Only_Default_Contact__c from Account];
for(Account a: trigger.new)
{
if(lst.size()==1)
{
a.Only_Default_Contact__c = TRUE;
update a;
}
else
{
a.Only_Default_Contact__c = FALSE;
update a;
}
}
}
}
Greetings to you!
You need to create 2 triggers, one on Account and second one Contact object. AccountTrigger will create a new contact on account insertion. ContactTrigger will update the checkbox on Account.
Please try below code:
AccountTrigger:
ContactTrigger:
Handler Class:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
All Answers
Greetings to you!
You need to create 2 triggers, one on Account and second one Contact object. AccountTrigger will create a new contact on account insertion. ContactTrigger will update the checkbox on Account.
Please try below code:
AccountTrigger:
ContactTrigger:
Handler Class:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Please follow the given below code with the help of these you can solve your problem, it may be helpful to you.
Apex Trigger :
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha