-
Why do I'm getting an error on inserting and updating the address in contact mailing address?
trigger ContactMailAddr on Contact (after insert, after update) { // first you create a list to store the Account ids List<Id> accountToSearch = new List<Id>(); // get them from the contacts for (Contact contact : Trigger.new) { accountToSearch.add(contact.AccountId); } // query in the db List<Account> accounts = [SELECT Id, BillingStreet, BillingCity, BillingState, BillingCountry, BillingPostalCode FROM Account WHERE Id IN :accountToSearch]; list<contact> contacts =new list<contact>([select id,AccountId,MailingCity,MailingState,MailingStreet,MailingCountry from contact where accountid in :accountToSearch]); // iterate over the accounts and contacts for (Account account : accounts) { for (Contact contact : Trigger.new) { // if a contact is set to the account then if (contact.AccountId == account.Id && contact.AccountId != null) { // copy the contact's address to the account's fields contact.MailingStreet = account.BillingStreet; contact.MailingCountry = account.BillingCountry; contact.MailingState = account.BillingState; contact.MailingCity = account.BillingCity; contact.MailingPostalCode = account.BillingPostalCode; } } } // finally update the contacts records update contacts; }
I'm getting an error on inserting and updating the address?
Log In to reply.
Popular Salesforce Blogs
![](https://images.forcetalks.com/wp-content/uploads/2016/04/25085305/Custom-Visualfore-Lookup.jpg)
Custom Visualforce Lookup
If you are creating a Visualforce page with a Standard controller, you can easily create Lookup field. But when using a custom controller and you…
![](https://images.forcetalks.com/wp-content/uploads/2023/12/05080250/sa_1701763120Illustration%20%20Salesforce%20Flow%20vs.%20Apex%20Complexity%20Comparison%201920%20x%201080%20px-1024x576.png)
Exploring Salesforce Automation: A Comparative Analysis of Flow and Apex
You will inevitably be required to choose once you face the need to automate tasks in Salesforce. Flow or Apex? What sets them apart? Besides…
![](https://www.forcetalks.com/wp-content/uploads/2024/03/sa_1709290773Illustration__MCAE_.png)
What is Pardot in Salesforce and Does Your Business Need it in 2024?
Many businesses consider marketing automation to be a must-have. According to the latest Salesforce State of Marketing Report, the average enterprise has 900 (!) different applications…
Popular Salesforce Videos
Features of Salesforce Industries (Vlocity)
There are many features of Salesforce Industries that make it a one-stop solution for businesses. Salesforce Industries or Vlocity offers Omnichannel processes with lightning Web,…
Introduction to Salesforce CPQ | EP1
Session 1: Introduction to Salesforce CPQ: The video provides an introduction to the general quoting challenge many Customers face, and how Salesforce CPQ with its…
5 Salesforce Hacks to Boost Your Business Sales
Good sales help you to get qualified leads and customers who bring value to you as well. Hence it becomes imperative to have astute resources…
Popular Salesforce Infographics
![](https://images.forcetalks.com/wp-content/uploads/2019/11/26113716/sa_1574768116salesforcecertinfographic.png)
Your Guide to Salesforce Certifications
In the age of customer experience, businesses now rely on their CRM systems more than ever. As the world leader in CRM software, Salesforce knows…
![](https://images.forcetalks.com/wp-content/uploads/2021/10/05081130/sa_1633421417SalesforceBuyVsSalesforceBuild.png)
Salesforce Buy Vs Salesforce Build - An Infographic
Uncover the Process that every business should understand at the time of Salesforce implementation and integration. Salesforce Solution Buying Process Search for Required Solution on…
![Salesforce CPQ](https://images.forcetalks.com/wp-content/uploads/2020/03/27155945/Salesforce-CPQ-1-2.jpg)
What is Salesforce CPQ or Configure, Price, Quote – [Infographic]
Enterprises are using various CPQ tools in order to enhance their business efficiency & revenue by engaging their sales reps in doing actual sales activities…