+ Start a Discussion
Vasu@blrVasu@blr 

unable to restrict update opeartion for my trigger

My code should not fire in case of update for below condition

(Trigger.isupdate && Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&& userproId.get(accmap.get(acccid))==p.id)

 

I took all necessary steps, please help me where I have to change.

 

trigger My_InsertAccountTeamMember_US on Account (after insert,after update) {

    List<AccountTeamMember> lstAcTeam = new List<AccountTeamMember>();    
    map<ID,set<ID>> usermap=new map<ID,set<ID>>();   
    map<Id,Id> accmap=new map<Id,Id>();  
    set<ID> setOwner=new set<Id>();
    List<User> userList=new List<User>();    
    set<ID> Parentlst=new set<ID>();
    map<Id,Id> userproId=new Map<Id,Id>();
    set<Id> TeamMember=new Set<Id>();
    List<AccountShare> lstAccShare =new List<AccountShare>();
    List<AccountShare> lstUpdateAccShare = new List<AccountShare>();    
    set<Id> accid=new set<Id>();
    
    Map<Id,UserRole> maprole = new Map<Id,UserRole>([select id,ParentRoleId from UserRole]);
    Profile p=[select Id from Profile where name='Standard User Level 2'];   

    userList=[select id ,Userroleid,ProfileId from user where IsActive=true ];
    for(User u1:userList){
        userproId.put(u1.id,u1.ProfileId);
        if(usermap.get(u1.userroleid)!=null){        
            usermap.get(u1.userroleId).add(u1.id);        
        }
        else{
            set<Id> setuid=new set<ID>();
            setuid.add(u1.id);
            usermap.put(u1.userroleid,setuid);        
        }
    }

    for(Account acc:trigger.new){
        accmap.put(acc.id,acc.ownerid);
        setOwner.add(acc.OwnerId);
        accid.add(acc.id);
    }
    
    map<Id,user> mapownerrole=new map<Id,user>([select id ,userroleid from user where id in:setOwner]);
    Userrole adminrole1=[select id from Userrole where Name='System Administrator'];   
    Userrole chinarole=[select id from Userrole where Name='China Users'];
    Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
    Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
    Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
    Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];   
    
    
    for(Id acccid:accmap.keyset()){
        if((Trigger.isInsert ||(Trigger.isupdate && Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&& userproId.get(accmap.get(acccid))==p.id){             
            id role=mapownerrole.get(accmap.get(acccid)).userroleid;
            Id ParentRoleNext=maprole.get(mapownerrole.get(accmap.get(acccid)).userroleid).ParentRoleId;                      
            if( role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id){            
                while(ParentRoleNext!=ProSalesSVP.id){                    
                    if(maprole.containskey(ParentRoleNext))                    
                    Parentlst.add(ParentRoleNext);                   
                    ParentRoleNext=maprole.get(ParentRoleNext).ParentRoleId;
                }                
                for(Id pid: Parentlst){
                    for(id uid1:usermap.get(pid)){                                           
                        lstAcTeam.add(new AccountTeamMember(UserId =uid1,
                        AccountId = acccid,
                        TeamMemberRole = 'Sales Manager'));
                        TeamMember.add(uid1);
                    }          
                }            
            }            
            lstAcTeam.add(new AccountTeamMember(UserId =accmap.get(acccid),
            AccountId = acccid,
            TeamMemberRole = 'Sales Rep'));                        
        }
        Parentlst.clear();
    }
    
    if(lstAcTeam.size()>0){    
        insert lstAcTeam;    
        lstAccShare = [Select Id, AccountId, OpportunityAccessLevel, AccountAccessLevel,ContactAccessLevel, UserOrGroupId ,
                                CaseAccessLevel From AccountShare where AccountId IN: accid
                                  AND UserOrGroupId IN: TeamMember] ;     
    }       

    for (AccountShare ash:lstAccShare ) {
        ash.AccountAccessLevel='Edit';
        ash.ContactAccessLevel='Edit';
        ash.OpportunityAccessLevel='Edit';
        ash.CaseAccessLevel ='Edit';
        lstUpdateAccShare.add(ash);
    }      
    If(lstUpdateAccShare.size()>0) {        
        update lstUpdateAccShare;
    }                       
}

 

 

MagulanDuraipandianMagulanDuraipandian

trigger triggerName on Object(after or before)

{

if(trigger.isupdate)

{

    for(object obj:trigger.new)

    {

        if(Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&&userproId.get(accmap.get(acccid))==p.id)

        {

                 ........................

                 ........................

        }

    }

}

}

 

Regards,

Magulan D

Salesforce.com certified Force.com Developer.

SFDC Blog

If this post is your solution, kindly mark this as the solution.