-
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
FlexDeploy Brings DevOps to Mule Integrations | Salesforce
You can build and test Mule Applications or APIs using Anypoint Studio very easily. But when it comes to automating deployments to your environments, with…
Features Your B2B Customer Portal Will Need in 2024
B2B companies are now conquering the heights of e-commerce, where providing a smooth customer experience is the only way to the top. How do you…
Difference between a Salesforce Administrator and a Salesforce Developer
Managing your business, you will definitely meet a question how to store and analyze data about your customers and their needs. Times when you did…
Popular Salesforce Videos
Rollup Trigger via Salesforce Record Triggered Flow - Trigger without code
Hello Folks, I created a Rollup trigger via Salesforce Record Triggered Flow (without code cool isn't). I use Record trigger Flow for that scenario. There…
What are Fields in Salesforce Admin?
Identity, system, and name fields are standard on every object in Salesforce. Each standard object also comes with a set of prebuilt, standard fields. You can…
How Do You Create Sandboxes in a Professional Edition in Salesforce?
Eric Stafford asks, “How do you create Sandboxes in a Professional Edition?” and gets an expert answer and demo from LeeAnne Rimel, Architect, Admin Evangelism…
Popular Salesforce Infographics
Timing is Everything : Understanding B2B Buyer Behavior
B2B buyer activity and research peaks at different times of the year, week, and day. Software Advice conducted a study gathering data from six million…
How Does Salesforce Use Slack To Collaborate and Connect?
Salesforce and Slack bring together all your teams with the apps and data they need in a single, collaborative workspace. Plus, Slack Connect functionality lets…
Salesforce Service Cloud for Better Customer Service
Service Cloud is classified as a customer support platform or customer relationship management (CRM) platform. It was designed to improve how businesses interact with their…