You need to sign in to do that
Don't have an account?
Vijay Zutshi
Before Update not updating contacts and opportunities with the same postal code and account owner
My trigger is as follows:-
trigger accountCreation on Account (before insert, before update) {
//Store all Zip Codes in a SET
SET<String> zipCodes = new SET<STRING>();
if(Trigger.isInsert) {
if(Trigger.isBefore) {
LIST<CONTACT> newContact = new LIST<CONTACT>();
LIST<OPPORTUNITY> newOpportunity = new LIST<OPPORTUNITY>();
for(Account newAccount : Trigger.new) {
zipCodes.add(newAccount.BillingPostalCode);
//create new contacts
contact newCon = new contact();
newCon.OwnerId = newAccount.OwnerId;
newContact.add(newCon);
//create new opportunity
OPPORTUNITY newOpp = new OPPORTUNITY();
newOpp.OwnerId = newAccount.OwnerId;
newOpportunity.add(newOpp);
}
if(newOpportunity != NULL && newOpportunity.size()>0) {
insert newOpportunity;
}
if(newContact != NULL && newContact.size()>0) {
insert newContact;
}
}
}
//Find changed Zip Codes
LIST<ACCOUNT> updateAccount = new LIST<ACCOUNT>();
LIST<CONTACT> updateContact = new LIST<CONTACT>();
LIST<OPPORTUNITY> updateOpp = new LIST<OPPORTUNITY>();
if(Trigger.isUpdate) {
if(Trigger.isBefore) {
SET<ID> changedAccount = new SET<ID>();
for(Account changedAcc : Trigger.new) {
String oldZip = Trigger.oldMap.get(changedAcc.Id).BillingPostalCode;
Id newuserId = Trigger.newmap.get(changedAcc.Id).OwnerId;
String newZip = changedAcc.BillingPostalCode;
if(newZip != oldZip) {
zipCodes.add(newZip);
changedAccount.add(changedAcc.Id);
updateAccount.add(changedAcc);
//When account's billing postal code is changed
//change the owner field of all the Account's Contacts to the same rep
LIST<CONTACT> updateContact = [Select Id,OwnerId from Contact where accountId in :changedAccount];
for(Contact con : updateContact) {
if(con.OwnerId != newuserId) {
con.OwnerId = newuserId;
//updateContact.add(con);
}
}
update updateContact;
//When account's billing postal code is changed
//change the owner field of all the Account's Open Opportunities to the same rep
LIST<OPPORTUNITY> updateOpp = [Select Id,OwnerId from Opportunity where accountId in :changedAccount and StageName != 'Closed Won'];
for(Opportunity opp : updateOpp) {
if(opp.OwnerId != newuserId) {
opp.OwnerId = newuserId;
//updateOpp.add(opp);
}
}
update updateOpp;
}
}
}
//Query for Territory__c records matching the set of Zip Codes
//and storing the query results in a MAP
MAP<String, Territory__c> terrMap = new MAP<String, Territory__c>();
LIST<Territory__c> zipMatch = [SELECT Id, Name, Territory__c.Owner__c, OwnerID, Zip_Code__c
FROM Territory__c
WHERE Zip_Code__c IN:zipCodes];
for(Territory__c terr : zipMatch) {
terrMap.put(terr.Zip_Code__c, terr);
}
//Iterate over the Account again for those Accounts
//with changed Zip Codes and look for associated
//Territory via Zip and assign the Account's OwnerId
//field to the value of that Territory's Owner__c field
for(Account iterAccount : updateAccount) {
Territory__c lookTerr = terrMap.get(iterAccount.BillingPostalCode);
if(lookTerr != NULL) {
iterAccount.OwnerId = lookTerr.Owner__c;
}
}
}
}
When I change the Accounts postal code, then owners field for all Accounts Contacts should also change to the same sales rep. In the same way change the owners field for all the Account's open opportunity to the same sales rep. Kindly help as I am new to this.
Thanks
Vijay Zutshi
trigger accountCreation on Account (before insert, before update) {
//Store all Zip Codes in a SET
SET<String> zipCodes = new SET<STRING>();
if(Trigger.isInsert) {
if(Trigger.isBefore) {
LIST<CONTACT> newContact = new LIST<CONTACT>();
LIST<OPPORTUNITY> newOpportunity = new LIST<OPPORTUNITY>();
for(Account newAccount : Trigger.new) {
zipCodes.add(newAccount.BillingPostalCode);
//create new contacts
contact newCon = new contact();
newCon.OwnerId = newAccount.OwnerId;
newContact.add(newCon);
//create new opportunity
OPPORTUNITY newOpp = new OPPORTUNITY();
newOpp.OwnerId = newAccount.OwnerId;
newOpportunity.add(newOpp);
}
if(newOpportunity != NULL && newOpportunity.size()>0) {
insert newOpportunity;
}
if(newContact != NULL && newContact.size()>0) {
insert newContact;
}
}
}
//Find changed Zip Codes
LIST<ACCOUNT> updateAccount = new LIST<ACCOUNT>();
LIST<CONTACT> updateContact = new LIST<CONTACT>();
LIST<OPPORTUNITY> updateOpp = new LIST<OPPORTUNITY>();
if(Trigger.isUpdate) {
if(Trigger.isBefore) {
SET<ID> changedAccount = new SET<ID>();
for(Account changedAcc : Trigger.new) {
String oldZip = Trigger.oldMap.get(changedAcc.Id).BillingPostalCode;
Id newuserId = Trigger.newmap.get(changedAcc.Id).OwnerId;
String newZip = changedAcc.BillingPostalCode;
if(newZip != oldZip) {
zipCodes.add(newZip);
changedAccount.add(changedAcc.Id);
updateAccount.add(changedAcc);
//When account's billing postal code is changed
//change the owner field of all the Account's Contacts to the same rep
LIST<CONTACT> updateContact = [Select Id,OwnerId from Contact where accountId in :changedAccount];
for(Contact con : updateContact) {
if(con.OwnerId != newuserId) {
con.OwnerId = newuserId;
//updateContact.add(con);
}
}
update updateContact;
//When account's billing postal code is changed
//change the owner field of all the Account's Open Opportunities to the same rep
LIST<OPPORTUNITY> updateOpp = [Select Id,OwnerId from Opportunity where accountId in :changedAccount and StageName != 'Closed Won'];
for(Opportunity opp : updateOpp) {
if(opp.OwnerId != newuserId) {
opp.OwnerId = newuserId;
//updateOpp.add(opp);
}
}
update updateOpp;
}
}
}
//Query for Territory__c records matching the set of Zip Codes
//and storing the query results in a MAP
MAP<String, Territory__c> terrMap = new MAP<String, Territory__c>();
LIST<Territory__c> zipMatch = [SELECT Id, Name, Territory__c.Owner__c, OwnerID, Zip_Code__c
FROM Territory__c
WHERE Zip_Code__c IN:zipCodes];
for(Territory__c terr : zipMatch) {
terrMap.put(terr.Zip_Code__c, terr);
}
//Iterate over the Account again for those Accounts
//with changed Zip Codes and look for associated
//Territory via Zip and assign the Account's OwnerId
//field to the value of that Territory's Owner__c field
for(Account iterAccount : updateAccount) {
Territory__c lookTerr = terrMap.get(iterAccount.BillingPostalCode);
if(lookTerr != NULL) {
iterAccount.OwnerId = lookTerr.Owner__c;
}
}
}
}
When I change the Accounts postal code, then owners field for all Accounts Contacts should also change to the same sales rep. In the same way change the owners field for all the Account's open opportunity to the same sales rep. Kindly help as I am new to this.
Thanks
Vijay Zutshi
Can you provide an example and mention the fields and objects you wat to modify so as to check further and respond.
Looking forward to your response.
Thanks.