You need to sign in to do that
Don't have an account?
sreee
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
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
Can u Explain in Detail
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.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
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.
for your scenario, I would say only trigger will work.
Never mind.
Thanks,
Samba
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
Thax souvik
Hi souvik
Iam getting following error with ur code
MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.PopulateAccountData: 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;
}
}
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
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
thnq souvik and samba for ur reply....both are wrkg fine.