You need to sign in to do that
Don't have an account?
Neeraj Sharma 103
When I changed the BilllingPostalCode of Account then AccountOwner Changed ContactOwner Changed, and Opportunity of that Account is Changed
Hi Everyone
I am New in Salesforce I am Beginner
So Please Help Me Out For this Scenario1
When an Account's BillingPostalCode is changed:
1.Change the Account Owner to the Sales Representative assigned to the new zip code
Sales Representative is in Another Object Territory(Territory__c)
in this object two Fileds one is ZipCode(Text) and other One is Owner(Lookup Field (User Lookup))
In First Requirement Change the Account Owner to the Owner Field in Territory and BillingPostalCode is also Mapp from ZipCode in territory Object and then Assign the AccountOwner to the Owner(Lookup) of Territory Object
2.Change the Owner field of all the Account’s Contacts to the same OwnerFiled in Territory Object(Sales Representative).
3.Change the Owner field of all the Account’s Open Opportunities to the same OwnerFiled in Territory Object(Sales Representative).
Please Replay as Soon Possible
How to use trigger i dont know about trigger So Please Help me out for this Scenario this is my First Trigger to write So Please Help
Thanks in Advance
Neeraj Sharma
I am New in Salesforce I am Beginner
So Please Help Me Out For this Scenario1
When an Account's BillingPostalCode is changed:
1.Change the Account Owner to the Sales Representative assigned to the new zip code
Sales Representative is in Another Object Territory(Territory__c)
in this object two Fileds one is ZipCode(Text) and other One is Owner(Lookup Field (User Lookup))
In First Requirement Change the Account Owner to the Owner Field in Territory and BillingPostalCode is also Mapp from ZipCode in territory Object and then Assign the AccountOwner to the Owner(Lookup) of Territory Object
2.Change the Owner field of all the Account’s Contacts to the same OwnerFiled in Territory Object(Sales Representative).
3.Change the Owner field of all the Account’s Open Opportunities to the same OwnerFiled in Territory Object(Sales Representative).
Please Replay as Soon Possible
How to use trigger i dont know about trigger So Please Help me out for this Scenario this is my First Trigger to write So Please Help
Thanks in Advance
Neeraj Sharma
Hey Neeraj
As it is always better to practice code, I will try to give you some steps to implement your requirement. Please do try to build it on your own to learn.
A trigger on an object is a code which executes whenever there is a change in the object's record like the update, insert, delete & undelete.
In your case, You can create an before update trigger on Account (Also read about bulk safe trigger )and add the following functionality :
1. You can use "Trigger.new(List of Updated Records)" and "Trigger.old(List of records before the update)" in your code to check whether old records BillingPostalCode is different from new records BillingPostalCode then create a list of record whose BillingPostalCode is different. Now your rest of the code should use that changedRecordList in which the BillingPostalCode has been changed. (If you don't understand why I am saying the list of records then consider a situation when you do a data update of 100 records using data loader).
2. Now on changedRecordList, traverse it on loop and get a set of all zipCode and accountIds of records on which your code will work
Set<string> zipCodeSet = new Set<string>();
Set<Id> setAccntId = new Set<id>();
For(Account acc : changedRecordList){
zipCodeSet.add(acc.BillingPostalCode);
setAccntId.add(acc.Id);
}
3. Get All the Zip code representative details using SOQL
List<Territory__c> listTerrRec = [SELECT ZipCode,Owner FROM Territory__c WHERE ZipCode = :zipCodeSet ];
4. Now For easier access create a map of the above List
Map<String,Id> mapZipCodeToUser = new Map<String,Id>
For ( Territory__c terr : listTerrRec){
mapZipCodeToUser.put(terr.ZipCode,terr.Owner)
}
5. First Part :
Loop changedRecordList like step 1 and update owner
For(Account acc : changedRecordList){
acc.Owner = mapZipCodeToUser.get(acc.BillingPostalCode);
}
6. Use "setAccntId" on SOQL query to get all the opportunity Records and All contact records in two lists, one for contacts and one for opportunity.
7. One by one loop on both lists and update the field values same step 5.
Hint : Use relationship queries and merged field on SOQL e.g [SELECT Id, (Select LastName,AccountId,Accounts.BillingPostalCode FROM Contacts) FROM Account ]
and access them like
For(Account acc: listOfAccounts){
for(Contact con: acc.Contacts){
con.owner = mapZipCodeToUser.get(con.Accounts.BillingPostalCode);
}
}
I understand I have not given you the complete code. It is always better to sit and spent some time and build your own code. You might find different ways to solve this problem.
All the Best.
Thanks
PS: Please neglect all the grammatical mistakes that I might have done :D
All Answers
Hey Neeraj
As it is always better to practice code, I will try to give you some steps to implement your requirement. Please do try to build it on your own to learn.
A trigger on an object is a code which executes whenever there is a change in the object's record like the update, insert, delete & undelete.
In your case, You can create an before update trigger on Account (Also read about bulk safe trigger )and add the following functionality :
1. You can use "Trigger.new(List of Updated Records)" and "Trigger.old(List of records before the update)" in your code to check whether old records BillingPostalCode is different from new records BillingPostalCode then create a list of record whose BillingPostalCode is different. Now your rest of the code should use that changedRecordList in which the BillingPostalCode has been changed. (If you don't understand why I am saying the list of records then consider a situation when you do a data update of 100 records using data loader).
2. Now on changedRecordList, traverse it on loop and get a set of all zipCode and accountIds of records on which your code will work
Set<string> zipCodeSet = new Set<string>();
Set<Id> setAccntId = new Set<id>();
For(Account acc : changedRecordList){
zipCodeSet.add(acc.BillingPostalCode);
setAccntId.add(acc.Id);
}
3. Get All the Zip code representative details using SOQL
List<Territory__c> listTerrRec = [SELECT ZipCode,Owner FROM Territory__c WHERE ZipCode = :zipCodeSet ];
4. Now For easier access create a map of the above List
Map<String,Id> mapZipCodeToUser = new Map<String,Id>
For ( Territory__c terr : listTerrRec){
mapZipCodeToUser.put(terr.ZipCode,terr.Owner)
}
5. First Part :
Loop changedRecordList like step 1 and update owner
For(Account acc : changedRecordList){
acc.Owner = mapZipCodeToUser.get(acc.BillingPostalCode);
}
6. Use "setAccntId" on SOQL query to get all the opportunity Records and All contact records in two lists, one for contacts and one for opportunity.
7. One by one loop on both lists and update the field values same step 5.
Hint : Use relationship queries and merged field on SOQL e.g [SELECT Id, (Select LastName,AccountId,Accounts.BillingPostalCode FROM Contacts) FROM Account ]
and access them like
For(Account acc: listOfAccounts){
for(Contact con: acc.Contacts){
con.owner = mapZipCodeToUser.get(con.Accounts.BillingPostalCode);
}
}
I understand I have not given you the complete code. It is always better to sit and spent some time and build your own code. You might find different ways to solve this problem.
All the Best.
Thanks
PS: Please neglect all the grammatical mistakes that I might have done :D
This is my trigger its work for Account Owner Change for Accounts Records but its not work for Contact Owner and Opportunity Owner of that Account the Contact Owner and Opportunity Owner is not change that in Same in Account Owner of that Account Records
trigger SFDC_TerritoryAssignment on Account (before update, after insert) {
Set<String> zips = new Set<String>();
if (Trigger.isInsert) {
// For inserted Accounts, all Zip Codes are needed
for (Account a : Trigger.new) {
zips.add(a.BillingPostalCode);
}
}
else if (Trigger.IsUpdate) {
// For updated Accounts, only evaluate changed Zip Codes
Set<Id> changedAccs = new Set<Id>();
for (Account a : Trigger.new) {
String oldZip = Trigger.oldMap.get(a.Id).BillingPostalCode;
String newZip = a.BillingPostalCode;
if (newZip != oldZip) {
zips.add(newZip);
changedAccs.add(a.Id);
}
}
}
Map<String, Territory__c> terrMap = new Map<String, Territory__c>();
List<Territory__c> terrs = [SELECT Id, Zip_Code__c,Owner__c FROM Territory__c
WHERE Zip_Code__c IN :zips];
for (Territory__c terr : terrs) {
terrMap.put(terr.Zip_Code__c, terr);
}
for(Account acc:Trigger.new)
{
Territory__c terr = terrMap.get(acc.BillingPostalCode);
if(terr!=null)
{
acc.OwnerId=terr.Owner__c;
}
}
List<Account> acts=[select id,(select OwnerId,AccountId,Lastname,Account.BillingPostalCode from contacts) from account ];
for(Account acc:acts)
{
for(Contact con:acc.Contacts){
Territory__c terr = terrMap.get(con.MailingPostalCode);
if(terr!=null)
{
con.OwnerId=terr.Owner__c;
}
}
}
}
Hey Neeraj
As the trigger is on account it will update the account only and you have to create a list of Contact and Opportunities in your code and then have to do the DML for them.
Update listOfContacts
Update listOfOpportunity
Thanks
thanku so much Your information is Useful for me Now my trigger is worked thanks again
Thanks
Neeraj Sharma