You need to sign in to do that
Don't have an account?
Manjusrinu
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);
}
}
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);
}
}
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.
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!