You need to sign in to do that
Don't have an account?
Ram Shiva Kumar
Before Update Trigger on Contact object
Hi i have written the trigger in such away that when i update the contact record , another Contact record hasto be inserted .But when i try to update the existing record it throwing the error like " execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.wed: line 39, column 1"
and my code is as below,
trigger wed on Contact(Before insert, Before Update)
{
Public Account a;
Public List<Contact> ab;
if(Trigger.isinsert==TRUE)
{
For(Contact c:Trigger.New)
{
ab=new List<Contact>();
a= [select Phone from Account where ID=:c.AccountID];
c.Phone=a.Phone;
ab.add(c);
}
}
if (Trigger.isUpdate==TRUE)
{
For(Contact ct:Trigger.New)
{
String stv=ct.FirstName;
Contact cts=new Contact();
cts.FirstName='ram';
cts.LastName=ct.LastName;
ab.add(cts);
}
insert ab;
}
}
So please suggest me any one, where iam doing the error.
and my code is as below,
trigger wed on Contact(Before insert, Before Update)
{
Public Account a;
Public List<Contact> ab;
if(Trigger.isinsert==TRUE)
{
For(Contact c:Trigger.New)
{
ab=new List<Contact>();
a= [select Phone from Account where ID=:c.AccountID];
c.Phone=a.Phone;
ab.add(c);
}
}
if (Trigger.isUpdate==TRUE)
{
For(Contact ct:Trigger.New)
{
String stv=ct.FirstName;
Contact cts=new Contact();
cts.FirstName='ram';
cts.LastName=ct.LastName;
ab.add(cts);
}
insert ab;
}
}
So please suggest me any one, where iam doing the error.
Do this,
Please mark this as the best answer if this helps
All Answers
I can see that "Public List<Contact> ab" is not initiated before assigning the values to it.
Do reply if that solved your problem.
Do this,
Please mark this as the best answer if this helps
Never write SOQL query inside for loop as it will cause SQOL 101 error if you insert bulk data. and As you have written ab=new List<Contact>(); inside for loop it is going to create new contact list everytime.
Thanks,
Sailee
@vasani parth:Your code worked for me with samll correction of creating instance for the List<Contact>. Thanks alot.
@lalitarora: yoursuggest is exact and it worked for me. Thanks.
@Sailee hande: thanks for the suggestin. Couldyou please tell , how can i use the SOQL queryin the abocve code to avoid the soql 101 error.