-
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

All You Need to Know About Service Console in Salesforce
Salesforce is a popular cloud-based Customer Relationship Management (CRM) platform that provides businesses with a variety of tools for managing their customer interactions. The Service…

New Conversation Channels in Salesforce Essentials: What Do They Mean To Customers?
Businesses are increasingly leveraging mobile and Social media platforms to reach billions of customers instantly and deliver personalized and connected customer experiences across channels at…
Popular Salesforce Videos
Security in Salesforce | Learn About the Security Levels in Salesforce
This "𝐒𝐞𝐜𝐮𝐫𝐢𝐭𝐲 𝐢𝐧 𝐒𝐚𝐥𝐞𝐬𝐟𝐨𝐫𝐜𝐞" video by Edureka will help you understand the different levels of security in Salesforce. It will also talk about some of…
Translate/Rename Custom Field Labels in Salesforce
Watch the short video instruction of how you can translate or rename custom field labels, fieldsets, buttons, etc. that come with installed managed packages in…
Run UI Tests with UTAM | Salesforce Developer Tutorial
Testing apps is critical to ensure quality and there are different types of tests such as unit tests, integrations tests or end-to-end tests just to…
Popular Salesforce Infographics

Top 5 Affordable Salesforce Integration Apps for SMBs
Salesforce is a CRM platform that allows small businesses to manage all of their sales, marketing and customer service communications in one place. Launched back…

10 Common Salesforce Mistakes—and How to Avoid Them
Salesforce integration is challenging if you don’t know exactly what you’re getting into. It’s an enormously valuable project, but diving into it without proper understanding…

How Important Is CRM Today? | An Infographic
CRM or Customer Relationship Management has become unavoidable and imperative in the world of business to reach the desired level of success. It has reached…