-
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 DupeCatcher Salesforce
Duplicate records can be a headache for any Salesforce admin or user. Not only do they clutter up the database and make it difficult to…
Lookup Relationship in Salesforce - Here's All You Need to Know
Introduction Lookup Relationship is an object type relationship in Salesforce. It is one to one or one to many relationships between two objects. Some Features…
MFA (Multi-Factor Authentication) in Salesforce
Introduction MFA is a special security feature provided by Salesforce that provides an extra layer of protection against suspicious or unauthorised logins. After enabling the…
Popular Salesforce Videos
How to Compare Change Sets from Another Org ? #Salesforce Change Sets Helper | Salesforce Tutorials
Change set with sort, view all, and additional information, such as last modified date. Allows comparison with other orgs. A chrome extension to improve the…
How To Use RingCentral App In Salesforce | Salesforce Service Cloud
Learn how to use RingCentral App in Salesforce. This video will cover the following points - 1. Advantages Take a look at the advantages of…
Salesforce Admin Beginners Tutorial - 3.1 - Salesforce Platform Basics
Salesforce is an innovative tool that can be very powerful when used properly. That’s why it’s important to find effective and trustworthy ways to learn…
Popular Salesforce Infographics
Salesforce Marketing Automation: Sales or Marketing Tool? [infographic]
Marketing automation has a hidden talent. Despite being marketed for years as a tool for, well, marketers, automation is changing the shape of the modern…
Salesforce Operations/Demographics
Salesforce, the world’s No. 1 CRM Company, has significantly changed the way how business software is delivered and used. Salesforce has found the perfect way…
What Can Salesforce’ Non Profit Success Pack Provide You With?
The Nonprofit Success Pack NPSP (formerly known as "Nonprofit Starter Pack) is a set of managed packages developed and maintained by Salesforce.org and made available…