We need to track any change in billing Address ( address,city,state,zip) of Account in a new Object : ÄLTERNATE CONTACT (Address, City,State, Zip, UpdatedDateTime, Account) . This object should only store unique address information. If the same address already exists in Alternate contact, just update “UpdatedDateTime” field without creating a new record.Only required on after update trigger
-
Any help would be much appreciated!!– John CarterOct 13, 2017 at 8:38
-
what problem are you facing?– Mr.FrodoOct 13, 2017 at 8:51
-
it seems that you know what to do, a trigger on account after update, so I'll ask the same as Mr.Frodo : what problem are you facing ? What did you try so far ?– Emmanuel BRUNOOct 13, 2017 at 9:47
1 Answer
I'd put something like this in the trigger :
List<Id> ids = new List<Id>(); //ids of accounts with watched fields modified
for (Integer i=0; i<trigger.new.size(); i++) //foreach account
{
if ( //if one of your fields is modified
(trigger.old[i].address != trigger.new[i].address) ||
(trigger.old[i].Zip != trigger.new[i].Zip) // || etc. with all field u want
)
{
ids.add(trigger.new[i].Id); //keep track of its id
}
}
if (ids.size()>0) //if u got accounts modified
{
AlternateContact__c[] altCts = [Select Id, UpdatedDateTime__c from AlternateContact__c where AccountId__c in :ids]; //select alt contacts linked to these accounts
for (AlternateContact__c ac : altCts) { //for each of them
ac.UpdatedDateTime__c = DateTime.now(); //update the date
}
update altCts; //save your changes
}
Please have a look at https://salesforce.stackexchange.com/help before you post your next question. This one lacked details.
Edit after your comment : If I understood you need to
- Query all fields on your AlternateContact__c object related to account
- Check if all the fields (except id of course) match your new address
- if it does update date
- if it doesn't create new AlternateContact__c
I can see nothing hard in it, you just have to adapt the code I wrote up there.
-
Actually, I want to write a trigger on account object so that when account's address is updated with a new address then its old address record is created in alternate contact object. But when the account address is updated with any of the previously created alternate contact address records then the UpdatedDateTime__c field of that alternate contact record will be updated with the current time without creating any new alternate contact record. Thanks in advance. Oct 13, 2017 at 11:33
-
First I would'nt call "contact" an object supposed to represent an address since contact is already a totally different object in Salesforce. Oct 13, 2017 at 12:30
-
alternate contact(alternate_contact__c) is a custom child object of account. Oct 13, 2017 at 13:26
-
yes I got it, as you can see if you look at the code (and understand it) I wrote. If my answers are not enough I'm afraid you should start with learning developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… or trailhead.salesforce.com : I can't do more or I'd have to bill you ;) Oct 13, 2017 at 14:14