-
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
Salesforce for Nonprofits: A Complete Guide
Nonprofit organizations need to manage donors, volunteers, fundraising campaigns, programs, and communications while making the most of limited resources. A powerful CRM can help bring…
Difference between a Salesforce Admin & Developer
In the fast paced world of customer relationship management (CRM) Salesforce stands as a robust platform. Its robust platform empowers organizations to manage relationships &…
Guide to Install an App from AppExchange
Thank you for your interest in knowing how to install apps from AppExchange for Salesforce. Use my blog as a guide, it will help you…
Popular Salesforce Videos
Simplify Salesforce Development using CLI and VSCode Tasks
The Salesforce CLI is a powerful command-line interface that simplifies development and builds automation when working with your Salesforce org. Typing commands can be monotonous…
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,…
Popular Salesforce Infographics
Sales Productivity: Tips to Overcome Productivity Challenges
Growing sales is the cornerstone of business success. Whether you're a startup or an established enterprise, the objective remains the same: Increase Revenue, Expand your…
Salesforce vs. HubSpot: Which CRM is Best For Your Business?
Salesforce has highly customizable lead management and scoring, while HubSpot can get small businesses up and running quickly with lead generation via content marketing. HubSpot…
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…