+ Start a Discussion
ManjusrinuManjusrinu 

Auto-populating custom field(Type) on contact object

Hi,
I have a custom field called Type of picklist (picklist values are same as off type values on account object).
I need to get all the contacts related to account and auto update the Type field on contact object with a value same as off to that account..

I tried the following code ,it is not showing any errors but my field on contact is not updating.

Can anyone hlepme to solve the issue ?
Apex Class:
public class UpdatingtypefieldonContact {
    public Map<list<Account>,list<Contact>> maplist{get;set;}// getting list of id(accounts) and values(contacts) //
    list<Contact> updatedlist{get;set;}
    public void updatetypefield()
    {
      maplist=new Map<list<Account>,list<Contact>>();
       list<Account> accountlist=new list<Account>();
       list<Contact> contactlist=new list<Contact>();
       list<Contact> updatedlist=new list<Contact>(); // list after updating type field//
       accountlist=[select id,name,Type from Account];
         contactlist=[select id,name,Accountid,Type__c from Contact where Accountid in:accountlist]; 
        maplist.put(accountlist,contactlist);
       // system.debug(maplist);//
           for(Account aa:accountlist)
           {
          for(Contact cc:contactlist)
          {
            if(cc.Type__c=='null')
             {
                  cc.Type__c=aa.Type;
                 updatedlist.add(cc);
              }
          }
        }
        update updatedlist;
        maplist.put(accountlist,updatedlist);
        system.debug(maplist);
     }
}

 
Saurabh Rawat 14Saurabh Rawat 14
public class UpdatingtypefieldonContact {
    public void updatetypefield()
    {

try
{
List<Account> Accountlist=new List<Account>();
List<Contact> Contactlist=new List<Contact>();
List<Contact> updateContactlist=new List<Contact>();
Map<Id,List<Contact>> AccountidVsContacts=new Map<Id,List<Contact>>();
Accountlist=[Select Id, Name, Type from Account LIMIT 20000];
Set<Id> AccountidSet=new Set<Id>();
for(Account each : Accountlist)
{
AccountidSet.add(each.Id);
}
Contactlist=[Select Id, Name, Type__c from Contact Where AccountId IN :AccountidSet];
for(Contact  each : Contactlist)
{
if(AccountidVsContacts.containsKey(each.AccountId))
{
AccountidVsContacts.get(each.AccountId).add(each);
}
else
{
List<Contact> templist=new List<Contact>();
templist.add(each);
AccountidVsContacts.put(each.AccountId , each);
}
}

//assigning each status

for(Account eachaccount : Accountlist)
{
  if(AccountidVsContacts.containsKey(eachaccount.Id))
  {
      for(Contact eachcon:AccountidVsContacts.get(eachaccount.Id))
        {

           if(eachcon.Type__c=='null')
             {
                 eachcon.Type__c =eachaccount .Type;
                  updateContactlist.add(eachcon);
              }//end if 2nd
        }//end for 2

  }// end if 1
}//end for 1

if(updateContactlist!=null && updateContactlist.size() > 0)
{
update updateContactlist;
}
}catch(Exception e){System.debug('Error at: '+e.getMessage());}

      
     }
}

You have to think and implement the structure before performing any logical operation in the apex.
Hope this code will help you.
Code may have some syntax error but logic will work absolutely fine. Feel free to contact me for further query.
Try to use "TRY and CATCH" block in the code. It will give you more specific details about errors and prevent exceptions.

Enjoy your day.
RituSharmaRituSharma
Meka,

You are querying all the accounts and that could cause issues in production(now or later). Query will fail if there are more than 50000 records in the system. 

From where(Trigger/VF/Lightning) and how are you calling the updatetypefield method? Based on that, I can help in restructuring the code.

Keep learning!