user3303348
user3303348

Reputation: 111

trigger to Update account filed, when a corresponding contact field is updated in salesforce

How do I create a trigger, to update a field on Account, when the contact, related to this account changes the field.

Example--- If a contact has a mailingcity, the respective Account Mailing city field should updated with the same value

My Trigger is as follows

But its not working Illegal assignment from Schema.SObjectField to String

The code what i have written is also from google. I am not able to solve this. Please help

 trigger ContactToAccountAddress on Contact (after insert,after update) {



List<ID> AccID = New List<ID>();


for(Contact con : Trigger.new){
if(con.MailingCity!=null&& con.AccountId != null){
  AccID.add(con.AccountId);
}
}

List<Account> accList = [SELECT Name, BillingStreet FROM Account WHERE id in :AccID];
for(integer i = 0 ; i < accList.size(); i++){
accList[i].BillingStreet =Contact.MailingCity;
}
update accList;
}

Upvotes: 0

Views: 10472

Answers (1)

user3310794
user3310794

Reputation: 11

List<Contact> conlist = [ select mailingCity from Contact where accountId in :AccID];
List<Account> accList = [SELECT Name, BillingStreet FROM Account WHERE id in :AccID];
for(integer i = 0 ; i < accList.size(); i++){
   **String address = ''+ conlist[0].get('mailingcity');
accList[i].BillingStreet = address ;
}**

Upvotes: 1

Related Questions