You need to sign in to do that
Don't have an account?
Abhishek Sharma 527
fetch contact and save in list
Hello There, I'm writing program to fetch newly created contact and save that into a list, it's partially done I'm able to fetch and print it but how to insert it into list. plz guide.
here's my code -
trigger ContactTrigger_query on Contact (before insert, before update) {
List<Contact> aclist = new List<Contact>();
Contact[] cont = [Select name, Account.Name from contact Where id IN : Trigger.new];
System.debug(cont[0].name +' '+ cont[0].Account.Name);
}
here's my code -
trigger ContactTrigger_query on Contact (before insert, before update) {
List<Contact> aclist = new List<Contact>();
Contact[] cont = [Select name, Account.Name from contact Where id IN : Trigger.new];
System.debug(cont[0].name +' '+ cont[0].Account.Name);
}
You can directly query your result in list like below :-
Kindly let me know what is your end Goal, I can help you with the code.
Kindly mark it as the best answer if it helps.
Thanks & Regards,
Priya Ranjan
All Answers
You can directly query your result in list like below :-
Kindly let me know what is your end Goal, I can help you with the code.
Kindly mark it as the best answer if it helps.
Thanks & Regards,
Priya Ranjan
can you tell how can i access this list entries.
Abhishek
Thanks
1) System.debug(aclist.size());
it's giving 1 every time and not getting incremented.
2) when i'm writing this -
if(conList.size()>0){
for(Contact c : conList){
system.debug("Contact value "+ c);
}
}
it's giving account id instead of account name but we inserted account name in the list.
any suggestion !!
For Your First Question, Only one Account can be associated with one contact record. So, that's why its shows Size as 1.
For Your Second Question, First of all, you are not inserting anything in your code which means you are just collecting the contact records in a list of contacts.
trigger ContactTrigger_query on Contact (before insert, before update)
{
List<Contact> conList = new List<Contact>();
conList = [Select Name, Account.Name from contact Where id IN : Trigger.new];
if(conList.size()>0)
{
for(Contact c : conList)
{
system.debug("Contact value "+ c); // gives list of contacts
system.debug("Contact value "+ c.Name); // gives list contact names
system.debug("Contact value "+ c.Account.Name); // gives list Account names which is associated with contact.
}
}
}
Kindly let me know if l answered your Questions.
Kindly mark it as the best answer if it helps.
Regards,
Prabhakaran