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
BP MurthyBP Murthy 

Redundant List

Hi I'm new to Salesforce
In the follwing code, how can I avoid using the con List.
Is there a way in which you can use SOQL query on directly instead of bringing in another List ??
trigger ContactTest on Contact (before insert, before update) {

    List<Contact> con = [Select Id, Account.Phone from Contact where Id in :];
    for(Contact c:

         for(Integer i=0; i< ; i++)
        c.OtherPhone = con[i].Account.Phone;


Also, can the above update be done with WOrkflow Rules ??

Michael DsozaMichael Dsoza
Yes....You can achieve above requirement using workflow rules wiht using field update on contact.

Mark resolved. If it resolves your query
madhan bondalapatimadhan bondalapati
We can do it through workflow,but it will fire when u created and edited the record
Mahesh DMahesh D

You can achieve this using the formula field on the Contact. But the field will be readonly if you make it as a formula filed.


Using the below trigger:
trigger ContactTest on Contact (before insert, before update) {

	Set<Id> accIdSet = new Set<Id>();
	for(Contact con: {
		if(con.AccountId != null)
	if(!accIdSet.isEmpty()) {
		Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Name, Phone from Account where Id IN: accIdSet]);
		for(Contact con: {
			if(con.AccountId != null) {
				con.OtherPhone = accMap.get(con.AccountId).Phone;

If it is only one field then you can proceed with formula field, if incase if you want to go with trigger then the  advantage of using it is, if you want to populate more fields in the future, it will be very easy.

Please let me know if this helps for you.