You need to sign in to do that
Don't have an account?
priya bhawna shetty
trigger relates issue
hello guys
pls help me out with this trigger...
my requirement is simple i.e after creating any account and saving that account i want to display Fax,Industry fields to the value that is specified in my trigger and want to create contact related to that account automatically....
trigger mysecondtrigger on Account (before insert,after insert)
{
if(trigger.isBefore)
{
for(Account acc:trigger.new)
{
acc.Fax='asdf';
acc.Industry='Tcs';
}
}
else{
for(Account acc:trigger.new)
{
contact c=new contact(LastName=' anynamehere');
c.AccountId=acc.Id;
}
}
}
Hi Priya,
You have to use insert DML operation to insert contact related to that account automatically.
see below updated trigger code for your requirement.
Apex Trigger:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
All Answers
Hi Priya,
You have to use insert DML operation to insert contact related to that account automatically.
see below updated trigger code for your requirement.
Apex Trigger:
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/
hey thanks a lot...
it worked ...
is there any method i can do without using list here...if so how...
Thanks
Yes, you can use direct insert c; instead of using List.
hey i got one more question
trigger mysecondtrigger on Account (before insert,after insert)
{
if(trigger.isBefore)
{
for(Account acc:trigger.new)
{
acc.Fax='asdf';
acc.Industry='Tcs';
}
}
else{
list<contact> lstcon=new list<contact>();
for(Account acc:trigger.new)
{
contact c=new contact(LastName='anynamehere');
c.AccountId=acc.Id;
lstcon.add(c);
}
}
if(lstcon.size()>0)
{
insert lstcon;
}
}
In the above code i placed "list<contact> lstcon=new list<contact>();" in else part i.e after insert ....and it is not working and it is showing (variable does not exist "lstcon").
i want a reason.
how come this issue..
In this case you have to put below code also in your else part.
if(lstcon.size()>0)
{
insert lstcon;
}