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
sreeesreee 

when ever contact record is created.......................

when ever contact record is created/updated then address fields data in contact should be updated into account address fields.can we do it with workflow....?

 

thanks in  Advance

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Try the below code

 

trigger PopulateAccountData on contact(after insert,after update){
List<Id> idList = new List<Id>();
List<Account> accListToBeUpdated = new List<Account>();
for(Contact con : Trigger.new){
if(con.AccountId != NULL){
idList.add(con.AccountId);
}
}
Map<ID,Account> accMap = new Map<ID,Account>([SELECT id,name FROM Account WHERE id in :idList]);

for(Contact con : Trigger.new){
if(con.AccountId != NULL){
Account acc = new Account();
acc = accMap.get(con.AccountId);
acc.MailingStreet = con.MailingStreet;
acc.MailingCity = con.MailingCity;
acc.MailingState = con.MailingState;
acc.MailingCountry = con.MailingCountry;
accListToBeUpdated.add(acc);
}
}
if(accListToBeUpdated.size()>0){
upsert accListToBeUpdated;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

All Answers

Bhawani SharmaBhawani Sharma
Yes, you can.
sreeesreee
Hi Sharma,
Can u Explain in Detail
souvik9086souvik9086

Hi,

 

You want to be able to perform field updates on parent records. For standard objects, workflow rules can only perform field updates on the object related to the rule. The exceptions are that both Case Comments and Email Messages can perform cross-object field updates on Cases. For all custom objects, however, you can create workflow actions where a change to a detail record updates a field on the related master record. Cross-object field updates only work for custom-to-custom master-detail relationships.

 

 You can see the above section in the following link
 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 
 
 
sambasamba

I don't think so. So you can't found account address fields in the Field Update for cotact object. I think you have to do it with tirgger. Thanks.

Bhawani SharmaBhawani Sharma
Sorry, my bad. I thought you want to update Contact's field with Account data.
for your scenario, I would say only trigger will work.
sambasamba

Never mind.

 

 

Thanks,

Samba

souvik9086souvik9086

Not possible through workflow. So if you want it in trigger. I'm giving it to you.

 

trigger PopulateAccountData on contact(after insert,after update){

List<Account> accListToBeUpdated = new List<Account>();
for(Contact con : Trigger.new){
Account acc = new Account(Id = con.AccountId);
acc.MailingStreet = con.MailingStreet;
acc.MailingCity = con.MailingCity;
acc.MailingState = con.MailingState;
acc.MailingCountry = con.MailingCountry;
accListToBeUpdated.add(acc);
}
if(accListToBeUpdated.size()>0){
update accListToBeUpdated;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

sreeesreee

Thax souvik

sreeesreee

Hi souvik

Iam getting following error with ur code

MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.PopulateAccountData&colon; line 

 

trigger PopulateAccountData on contact(after insert,after update){

List<Account> accListToBeUpdated = new List<Account>();
for(Contact con : Trigger.new){
Account acc = new Account(Id = con.AccountId);
acc.BillingStreet = con.MailingStreet;
acc.BillingCity = con.MailingCity;
acc.BillingState = con.MailingState;
acc.BillingCountry = con.MailingCountry;
accListToBeUpdated.add(acc);
}
if(accListToBeUpdated.size()>0){
update accListToBeUpdated;
}
}

 

souvik9086souvik9086

Try the below code

 

trigger PopulateAccountData on contact(after insert,after update){
List<Id> idList = new List<Id>();
List<Account> accListToBeUpdated = new List<Account>();
for(Contact con : Trigger.new){
if(con.AccountId != NULL){
idList.add(con.AccountId);
}
}
Map<ID,Account> accMap = new Map<ID,Account>([SELECT id,name FROM Account WHERE id in :idList]);

for(Contact con : Trigger.new){
if(con.AccountId != NULL){
Account acc = new Account();
acc = accMap.get(con.AccountId);
acc.MailingStreet = con.MailingStreet;
acc.MailingCity = con.MailingCity;
acc.MailingState = con.MailingState;
acc.MailingCountry = con.MailingCountry;
accListToBeUpdated.add(acc);
}
}
if(accListToBeUpdated.size()>0){
upsert accListToBeUpdated;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

This was selected as the best answer
sambasamba

Apex:

trigger PopulateAccountData on Contact (after insert, after update)
{
Map<String, Contact> accountMap = new Map<String, Contact>();
List<Account> accounts = new List<Account>();

for(Contact con : Trigger.new)
{
accountMap.put(con.AccountId, con);
}

for(Account acc : [select Id, BillingStreet, BillingCity, BillingState, BillingCountry, BillingPostalCode from Account where Id in :accountMap.keySet()])
{
Contact con = accountMap.get(acc.Id);
acc.BillingStreet = con.MailingStreet;
acc.BillingCity = con.MailingCity;
acc.BillingState = con.MailingState;
acc.BillingCountry = con.MailingCountry;
acc.BillingPostalCode = con.MailingPostalCode;
accounts.add(acc);
}

if(accounts.size() > 0)
{
update accounts;
}
}

I hope I can help you.

 

Thanks,

Samba

sreeesreee

thnq souvik and samba for ur reply....both are wrkg fine.