• Developer118
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Hi,
     I have written trigger which manulaay adds a set of users to the OpportunityShare Object whenever the Owner of Opportunity is Changed, This works fine when the Owner is changed once, But once again when i change the Owner of the same Opportunity, the Users manually added to OpportunityShare Object prevuiously are lost.

I need to reatin the sharing on the Opportuntiy once the users are manually added to the Opportuntiy Share Object no matter how many times Owner is Changed.

Can someone help me out on this.

Many Thanks in Advance,
Developer118
Hi Everyone,
                     I have a requirement where i need to check the Owner Territory, and User Title Before Update,
The Previous owner can transfer the particualr opportuntiy to a new Owner if the Parent Territory of both (New Owner, Previous Owner) are same and the new user has title as "Special" .  A particular user can sit on  more then one territory. so only those users can be new owner which satisfy this condition.

I am new to Apex, i wrote something based on S-Control i saw on the community.
 
Code:
trigger NewOwner on Opportunity (before update, after update) 
{
//string profileid =UserInfo.getProfileId();
ID oppsid = Trigger.old[0].Id;
User[] PreviousOwner = [select id, profileid from User where id=:Trigger.old[0].OwnerId Limit 1];
//String OwnerId = user.Id[0];
if(PreviousOwner[0].ProfileId =='00eT0000000dnyE')
{
// Territory of the Owner
UserTerritory[] OwnerTerritory = [select Id, TerritoryId, IsActive from UserTerritory where UserId=:Trigger.old[0].OwnerId and IsActive = TRUE];
for(Integer i=0; i<OwnerTerritory.size(); i++)
{
// Parent Territory of the Owner
Territory[] OwnerParentTerritory = [select Id, Name, ParentTerritoryID from Territory where Id=:OwnerTerritory[i].TerritoryId];
for (Integer j=0; j<OwnerParentTerritory.size(); j++)
{
// Get the New User's Id and Title.
User[] NewOwner = [select Id, Title from User where id=:Trigger.new[0].OwnerId Limit 1];
// To get the New User's Territory
UserTerritory[] NewOwnerTerritory = [select Id, TerritoryId, IsActive from UserTerritory where UserId =:Trigger.new[0].OwnerId and IsActive = TRUE];
Territory[] NewOwnerParentTerritory = [select Id, Name, ParentTerritoryID from Territory where Id=:NewOwnerTerritory[0].TerritoryId];
for(Integer k=0; k<NewOwnerParentTerritory.size(); k++)
if(NewOwner[0].Title == 'Special' && NewOwnerParentTerritory[k].ParentTerritoryID == OwnerParentTerritory[j].ParentTerritoryID)
{
UserTerritory[] OldOwner = [select TerritoryId, UserId from UserTerritory where UserId=:Trigger.old[0].OwnerId and IsActive = TRUE];
for(Integer l=0; l<OldOwner.size(); l++)
{
for(UserTerritory OldOwnerTerritoryUsers:[select UserId from UserTerritory where TerritoryId=:OldOwner[l].TerritoryId and IsActive = TRUE])
{
OpportunityShare oppsshare = new OpportunityShare();
oppsshare.OpportunityId = oppsid ;
oppsshare.UserOrGroupId = OldOwnerTerritoryUsers.UserId;
oppsshare.OpportunityAccessLevel = 'Read';
try
{
insert oppsshare ;
}
catch (Exception e)
{
System.debug(e.getMessage());
}
}
}
}
/*else
{
Trigger.new[0].OwnerId.addError('Deal can only be assigned to User in your parent territory and with title as Special');
}
*/
}
}
}
}
}

The trigger saved and also is valid but at the time of Operation, i recieve this error message.
Error - Apex trigger: NewOwner caused an unexpected exception, contact your administrator: NewOwner: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.NewOwner

Error: Apex trigger NewOwner caused an unexpected exception, contact your administrator: NewOwner: execution of AfterUpdate caused by: System.Exception: Too many SOQL queries: 21: Trigger.NewOwner: line 18, column 19

If someone can please help me with this, would be great help.
Many Thanks in Advance.
Developer118


Message Edited by Developer118 on 02-12-2008 02:16 PM

Message Edited by Developer118 on 02-12-2008 02:17 PM