You need to sign in to do that
Don't have an account?

I have two objects like Account and Contact if Account Name and Contact Last Name is Same but Account Id is Null
trigger updatepho on Contact (before update){
List<Contact> con = new List<Contact>();
List<Account> acc = [select Id from Account where Name =: con[0].LastName];
if(con[0].AccountId == null){
for(Contact c:con){
c.AccountId = acc[0].Id;
con.add(c);
}
}
update con;
}
My error Description:Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updatepho caused an unexpected exception, contact your administrator: updatepho: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.updatepho: line 3, column 1
List<Contact> con = new List<Contact>();
List<Account> acc = [select Id from Account where Name =: con[0].LastName];
if(con[0].AccountId == null){
for(Contact c:con){
c.AccountId = acc[0].Id;
con.add(c);
}
}
update con;
}
My error Description:Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updatepho caused an unexpected exception, contact your administrator: updatepho: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.updatepho: line 3, column 1
What is your requirement Can you tell us in little more depth?
So far this is what I am understanding.
Create a trigger which joints contact to the account whenever it has something to do with Last Name..
Let's explore the requirement because it's looks like it is definitely possible with trigger.
Thank You!
Kindly don't forget to Mark as Best Answer if it solves your question.