function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
priya bhawna shettypriya 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;

}
}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

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:

trigger mysecondtrigger on Account (before insert,after insert){
    List<Contact> lstcon = new List<Contact>();
    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;
            lstcon.add(c); 
        }
    }
    if(lstcon.size() > 0){
        insert lstcon;
    }
}

 

 

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

hitesh90hitesh90

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:

trigger mysecondtrigger on Account (before insert,after insert){
    List<Contact> lstcon = new List<Contact>();
    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;
            lstcon.add(c); 
        }
    }
    if(lstcon.size() > 0){
        insert lstcon;
    }
}

 

 

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/

This was selected as the best answer
priya bhawna shettypriya bhawna shetty

hey thanks a lot...

it worked ...

is there any method i can do without using list here...if so how...

Thanks

hitesh90hitesh90

Yes, you can use direct insert c; instead of using List.

priya bhawna shettypriya bhawna shetty

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

hitesh90hitesh90

In this case you have to put below code also in your else part.


if(lstcon.size()>0)
{
insert lstcon;
}