+ Start a Discussion
Simha YadavSimha Yadav 

i have two custom fields in both account and conatct i,e state and city now i want to make auto populate for those fields in contact when i try to create new contact in related list.

Hi Folks,

i have two custom fields in both account and conatct i,e state and city now i want to make auto populate for those fields in contact when i try to create new contact in related list.
Aniket Malvankar 10Aniket Malvankar 10
Can you please more precise on the custom fields.

What is on Account?
What is on Contact?

If you need to do a toggling between state and city than use Dependent field.

Regards
Aniket
 
sandeep reddy 37sandeep reddy 37
use formula field  to write a formula so it will works  
Sergio AlcocerSergio Alcocer

If what you really want is to prepopulate the fields, but allow the user to change them in case is necessary, 
you will need to create a new "New Contact" button and inject the values through the URL.

It is not a best practice, as it might fail as soon as Salesforce decide to change it and it might not work on Salesforce 1, and mobile apps.

To inject the values through the url, for standard fields is easy, as you just use the name of the field, otherwise you need to use the ID (15 characters ID of the field and put it something like &00N41000009eaut=the value you want)

Take into account that you might need to encode it to prevent XSS risks and encoding issues

sailee handesailee hande
simha murthy,

You can do this by formula field or trigger. By formula field : make both field types on Account as text fields and on Contact as formula fields.

formula should be for City: Account.City__c similarly for state.

Regards,
Sailee
 
Simha YadavSimha Yadav
@Sergio Alcocer,sailee hande
i want it by using trigger please suggest anybody how to do it?
sailee handesailee hande
Hi Simha,

Apex Class :

public class AutomaticFieldPopulation 
{
   
    
    
    public static void autoPopulateCityOnContact(List<Contact> contList)
    {
        
       system.debug('**********');
       set<id> ids = new set<id>();
       
       for(Contact c :  contList)
       {
            ids.add(c.AccountID);
       }
       Map<id,Account> mapForAccount = new Map<id,Account>();
       for(Account acct: [Select id,City__c from Account where Id in: ids ])
       {
            mapForAccount.put(acct.id,acct);
       }
        contList= [Select id,CityNew__c from Contact where AccountID in: ids];
        for(Contact c :  contList)
        {
            if(mapForAccount.get(c.AccountID) != null && mapForAccount.get(c.AccountID).City__c != null)
             {
                    system.debug('*******id************'+c.id);
                    system.debug('*******AccountID************'+c.AccountID);
                    c.CityNew__c = mapForAccount.get(c.AccountID).City__c;
                    system.debug('**********'+ mapForAccount.get(c.AccountID).City__c);
                    
             }   
         }
     }
            
            
 }

Trigger :

trigger automaticPopulation on Contact(Before insert, Before Update)
{

        system.debug('*******Trigger************');
        if(trigger.isBefore)
        {
            if(Trigger.isInsert || Trigger.isUpdate)
            {
                AutomaticFieldPopulation.autoPopulateCityOnContact(Trigger.new);
                system.debug('*******Inner trigger************');
            }
        }
}

Add similarly for your remaining custom fields. If this helps you then let me know.

Thanks,
Sailee
sandeep reddy 37sandeep reddy 37
Why this is not work 


trigger populatepickvalue on Contact (before insert) {
    if(trigger.new.isafter&& trigger.new.isinsert){
    for(contact c:trigger.new){
        if(c.accountid!=null&&c.Account.Rating!=null){
            c.srinivasreddy__ratig__c=c.Account.Rating;
        }    
    }
        
}
sandeep reddy 37sandeep reddy 37
use this coad it will work 

trigger populatepickvalue on Contact (before insert) {
    if(trigger.isBefore)
        {
            if(Trigger.isInsert )
            {
                for(contact c:trigger.new){
                    account a=[select id,rating from account where id=:c.accountid];
                    c.srinivasreddy__ratig__c=a.rating;
                }
            }
        }
    }

i hope this is help full for u if your problrm is solved make this as best answer