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
Abhishek Sharma 527Abhishek 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);
    
}
Best Answer chosen by Abhishek Sharma 527
PriyaPriya (Salesforce Developers) 
Hey Abhishek,

You can directly query your result in list like below :- 
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];
    System.debug(cont[0].name +' '+ cont[0].Account.Name);
    
}

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

PriyaPriya (Salesforce Developers) 
Hey Abhishek,

You can directly query your result in list like below :- 
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];
    System.debug(cont[0].name +' '+ cont[0].Account.Name);
    
}

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

This was selected as the best answer
Abhishek Sharma 527Abhishek Sharma 527
Thanks Priya, actually nothing particular task I'm just exploring these functionalities.
can you tell how can i access this list entries.


Abhishek
 
PriyaPriya (Salesforce Developers) 
Once you get all the values in the list then you can run a for loop to print every value of the list like below :-
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);
      }
   }
    
}

Thanks
Abhishek Sharma 527Abhishek Sharma 527
Thank you Priya, plz two questions on this, when i'm printing size as -
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);
      }
   }
 
User-added image
it's giving  account id instead of account name but we inserted account name in the list.

any suggestion !!

 
PRABHAKARAN V 5PRABHAKARAN V 5
Hi Abhishek Sharma 527,
     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