• Ebuka Ekwueme
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 0
    Questions
  • 3
    Replies
I have a professional edition org and i want to set sharing rules on Opportunity Object  which is not available. Hence i am looking to go for Apex Managed Sharing. Would it work . If yes please help me with the solution.

Its urgent
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