• Vijay Zutshi
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 5
    Replies
My trigger is as follows:-

//Trigger to count sales rep assigned to a single zip code
//Display an error if user attemps to associate another sales
//rep to the same zip code
trigger salesRepCount on Territory__c (before insert, before update, after update) {
     //List<Account> NewAccountlisttoUpdate = new List<Account>();
     //List<Account> AccountsToUpdateOwner = new List<Account>();
     //SET<ID> setAccountId = new SET<ID>();
    //Store all zip codes in a set
    SET<String> territoryZipCodes = new  SET<String>();
    MAP<String, Territory__c> territoryMap = 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:territoryZipCodes];
    //If a Territory record sales rep is changed
    //check if old and new owners are different
    if(Trigger.isUpdate) {
        if(Trigger.isAfter) {
            List<Account> NewAccountlisttoUpdate = new List<Account>();
            for(Territory__c changedSalesRep : Trigger.new) {
                String oldId = Trigger.oldMap.get(changedSalesRep.Id).OwnerId;
                String newuserId = Trigger.newmap.get(changedSalesRep.Id).OwnerId; 
                if(oldId != newuserId) {  
                     String Territoryzipcode = Trigger.newmap.get(changedSalesRep.Id).Zip_Code__c; 
                     List<Account> AccountsToUpdateOwner = [SELECT ID, OwnerId
                                                               FROM ACCOUNT
                                                               WHERE BillingPostalCode =:Territoryzipcode]; 
                    SET<ID> setAccountId = new SET<ID>();
                    for(Account A : AccountsToUpdateOwner) {
                        if(A.OwnerId != newuserId) {
                            A.OwnerId = newuserId;
                            NewAccountlisttoUpdate.add(A);
                            setAccountId.add(A.Id);
                        }
                    }   
                    update NewAccountlisttoUpdate;
                    
                    //For the Above updated Accounts, check the Contacts and Update with new Owner;
                    LIST<Contact> newContactListToUpdate = [SELECT Id, OwnerId
                                                            FROM Contact
                                                            WHERE AccountId IN :setAccountId];
                    for(Contact updateCon: newContactListToUpdate) {
                        if(updateCon.OwnerId != newuserId) {
                            updateCon.OwnerId = newuserId;
                            newContactListToUpdate.add(updateCon);
                        }
                    }                    
                    update newContactListToUpdate;
                    
                    //For the Above updated Accounts, check the Opportunities and Update with new Owner;
                    LIST<Opportunity> newOpportunityListToUpdate = [SELECT ID, OwnerID 
                                                                    FROM Opportunity
                                                                    WHERE AccountId IN:setAccountId
                                                                    AND StageName != 'Closed Won'];
                    for(Opportunity updateOppty : newOpportunityListToUpdate) {
                        if(updateOppty.OwnerId != newuserId) {
                            updateOppty.OwnerId = newuserId;
                            newOpportunityListToUpdate.add(updateOppty);
                        }
                    }
                    update newOpportunityListToUpdate;
                }
            }
        }
    }
    for(Territory__c repCount : Trigger.new) {
        Integer countSalesRep = [SELECT count() from Territory__c
                                 WHERE Territory__c.OwnerID =:repCount.OwnerId
                                 AND Territory__c.Id !=:repCount.Id
                                 AND Territory__c.Zip_Code__c =:repCount.Zip_Code__c];
        if(countSalesRep>3) {
            repCount.addError('we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)'+countSalesrep); 
        }
    }
}

So when I update an owner in the Territory__c object it should also update all Accounts with the same owner. The trigger is not oing that. Rest of my trigger for other things si working. So please help.

Thanks
Vijay
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
My accountCreation is giving error when I update an account. My trigger is as follows:- 

The error is in line 43

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) {
            for(Account newAccount : Trigger.new) {
              zipCodes.add(newAccount.BillingPostalCode);  
            } 
        }
    }
    //Find changed Zip Codes
    LIST<ACCOUNT> updateAccount = new LIST<ACCOUNT>();
    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;
                String newZip = changedAcc.BillingPostalCode;
                    if(newZip != oldZip) {
                    zipCodes.add(newZip);
                    //changedAccount.add(changedAcc.Id); 
                    updateAccount.add(changedAcc);              
      
                }   
            }    
        }
     //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, 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; 
        }
    }
  }

}

Please help as I am not able to figure out.

Thanks
Vijay Zutshi
I have to recreate a ZIP Code spreadsheet. The custom object will have following fields:

1. Zip Code (Text) - The standard name field. Each record will be named after its specific zip code
2. Owner - Lookup(User) - The standard owner field. The sales rep assigned to this territory.

As I am new to APEX please help as I am stuck and not able to move further.

Thanks
 
I have to recreate the Zip Code spreadsheet. At this point it is done manually where the Account Owner field is set to sales rep user record when the Account Zip Code belongs to the sales rep. For this purpose I have already created Custom Territory Object as Territory__c. This object has following field: 
1. Territory Name - Name
2. Zip Code - Zip_Code__c(Text)
3. User - Owner__c
I am new to this  and I want to write a trigger for this purpose . I will require help from experienced people like you. Thanks
Vijay
My trigger is as follows:-

//Trigger to count sales rep assigned to a single zip code
//Display an error if user attemps to associate another sales
//rep to the same zip code
trigger salesRepCount on Territory__c (before insert, before update, after update) {
     //List<Account> NewAccountlisttoUpdate = new List<Account>();
     //List<Account> AccountsToUpdateOwner = new List<Account>();
     //SET<ID> setAccountId = new SET<ID>();
    //Store all zip codes in a set
    SET<String> territoryZipCodes = new  SET<String>();
    MAP<String, Territory__c> territoryMap = 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:territoryZipCodes];
    //If a Territory record sales rep is changed
    //check if old and new owners are different
    if(Trigger.isUpdate) {
        if(Trigger.isAfter) {
            List<Account> NewAccountlisttoUpdate = new List<Account>();
            for(Territory__c changedSalesRep : Trigger.new) {
                String oldId = Trigger.oldMap.get(changedSalesRep.Id).OwnerId;
                String newuserId = Trigger.newmap.get(changedSalesRep.Id).OwnerId; 
                if(oldId != newuserId) {  
                     String Territoryzipcode = Trigger.newmap.get(changedSalesRep.Id).Zip_Code__c; 
                     List<Account> AccountsToUpdateOwner = [SELECT ID, OwnerId
                                                               FROM ACCOUNT
                                                               WHERE BillingPostalCode =:Territoryzipcode]; 
                    SET<ID> setAccountId = new SET<ID>();
                    for(Account A : AccountsToUpdateOwner) {
                        if(A.OwnerId != newuserId) {
                            A.OwnerId = newuserId;
                            NewAccountlisttoUpdate.add(A);
                            setAccountId.add(A.Id);
                        }
                    }   
                    update NewAccountlisttoUpdate;
                    
                    //For the Above updated Accounts, check the Contacts and Update with new Owner;
                    LIST<Contact> newContactListToUpdate = [SELECT Id, OwnerId
                                                            FROM Contact
                                                            WHERE AccountId IN :setAccountId];
                    for(Contact updateCon: newContactListToUpdate) {
                        if(updateCon.OwnerId != newuserId) {
                            updateCon.OwnerId = newuserId;
                            newContactListToUpdate.add(updateCon);
                        }
                    }                    
                    update newContactListToUpdate;
                    
                    //For the Above updated Accounts, check the Opportunities and Update with new Owner;
                    LIST<Opportunity> newOpportunityListToUpdate = [SELECT ID, OwnerID 
                                                                    FROM Opportunity
                                                                    WHERE AccountId IN:setAccountId
                                                                    AND StageName != 'Closed Won'];
                    for(Opportunity updateOppty : newOpportunityListToUpdate) {
                        if(updateOppty.OwnerId != newuserId) {
                            updateOppty.OwnerId = newuserId;
                            newOpportunityListToUpdate.add(updateOppty);
                        }
                    }
                    update newOpportunityListToUpdate;
                }
            }
        }
    }
    for(Territory__c repCount : Trigger.new) {
        Integer countSalesRep = [SELECT count() from Territory__c
                                 WHERE Territory__c.OwnerID =:repCount.OwnerId
                                 AND Territory__c.Id !=:repCount.Id
                                 AND Territory__c.Zip_Code__c =:repCount.Zip_Code__c];
        if(countSalesRep>3) {
            repCount.addError('we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)'+countSalesrep); 
        }
    }
}

So when I update an owner in the Territory__c object it should also update all Accounts with the same owner. The trigger is not oing that. Rest of my trigger for other things si working. So please help.

Thanks
Vijay
My trigger is as follows:-

//Trigger to count sales rep assigned to a single zip code
//Display an error if user attemps to associate another sales
//rep to the same zip code
trigger salesRepCount on Territory__c (before insert, before update, after update) {
     //List<Account> NewAccountlisttoUpdate = new List<Account>();
     //List<Account> AccountsToUpdateOwner = new List<Account>();
     //SET<ID> setAccountId = new SET<ID>();
    //Store all zip codes in a set
    SET<String> territoryZipCodes = new  SET<String>();
    MAP<String, Territory__c> territoryMap = 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:territoryZipCodes];
    //If a Territory record sales rep is changed
    //check if old and new owners are different
    if(Trigger.isUpdate) {
        if(Trigger.isAfter) {
            List<Account> NewAccountlisttoUpdate = new List<Account>();
            for(Territory__c changedSalesRep : Trigger.new) {
                String oldId = Trigger.oldMap.get(changedSalesRep.Id).OwnerId;
                String newuserId = Trigger.newmap.get(changedSalesRep.Id).OwnerId; 
                if(oldId != newuserId) {  
                     String Territoryzipcode = Trigger.newmap.get(changedSalesRep.Id).Zip_Code__c; 
                     List<Account> AccountsToUpdateOwner = [SELECT ID, OwnerId
                                                               FROM ACCOUNT
                                                               WHERE BillingPostalCode =:Territoryzipcode]; 
                    SET<ID> setAccountId = new SET<ID>();
                    for(Account A : AccountsToUpdateOwner) {
                        if(A.OwnerId != newuserId) {
                            A.OwnerId = newuserId;
                            NewAccountlisttoUpdate.add(A);
                            setAccountId.add(A.Id);
                        }
                    }   
                    update NewAccountlisttoUpdate;
                    
                    //For the Above updated Accounts, check the Contacts and Update with new Owner;
                    LIST<Contact> newContactListToUpdate = [SELECT Id, OwnerId
                                                            FROM Contact
                                                            WHERE AccountId IN :setAccountId];
                    for(Contact updateCon: newContactListToUpdate) {
                        if(updateCon.OwnerId != newuserId) {
                            updateCon.OwnerId = newuserId;
                            newContactListToUpdate.add(updateCon);
                        }
                    }                    
                    update newContactListToUpdate;
                    
                    //For the Above updated Accounts, check the Opportunities and Update with new Owner;
                    LIST<Opportunity> newOpportunityListToUpdate = [SELECT ID, OwnerID 
                                                                    FROM Opportunity
                                                                    WHERE AccountId IN:setAccountId
                                                                    AND StageName != 'Closed Won'];
                    for(Opportunity updateOppty : newOpportunityListToUpdate) {
                        if(updateOppty.OwnerId != newuserId) {
                            updateOppty.OwnerId = newuserId;
                            newOpportunityListToUpdate.add(updateOppty);
                        }
                    }
                    update newOpportunityListToUpdate;
                }
            }
        }
    }
    for(Territory__c repCount : Trigger.new) {
        Integer countSalesRep = [SELECT count() from Territory__c
                                 WHERE Territory__c.OwnerID =:repCount.OwnerId
                                 AND Territory__c.Id !=:repCount.Id
                                 AND Territory__c.Zip_Code__c =:repCount.Zip_Code__c];
        if(countSalesRep>3) {
            repCount.addError('we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)'+countSalesrep); 
        }
    }
}

So when I update an owner in the Territory__c object it should also update all Accounts with the same owner. The trigger is not oing that. Rest of my trigger for other things si working. So please help.

Thanks
Vijay
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
I have to recreate a ZIP Code spreadsheet. The custom object will have following fields:

1. Zip Code (Text) - The standard name field. Each record will be named after its specific zip code
2. Owner - Lookup(User) - The standard owner field. The sales rep assigned to this territory.

As I am new to APEX please help as I am stuck and not able to move further.

Thanks