0
votes

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;
}
1
To increase your chances of getting the attention of users able to answer this question, you should edit it and add the applicable language tag. Also, have you tried to solve this problem yourself? Can you show us some code? Presumably this is in a database - can you show us your DB schema, and expected input and output?Bernhard Barker

1 Answers

1
votes
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 ;
}**