function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MrkennyMrkenny 

Trigger not working

Trigger is supposed to manage partner authorization and access to manage support on behalf of their end users

Trigger functionality: When a partner is added to the partner related list in an account the account is marked as an Authorized support partner which is a checkbox feild I created. After the partner is manually entered in the partner related list in the account and the Authorized support partner feild is checked the partner should automatically get sharing access to the account here is my code below can someone help me out please:

 

 

trigger PartnerSharing onAccount (afterinsert, afterupdate){

set<id> account_id = new set<id>();

set<id> account_to = new set<id>();

map<id,id> ShareFromTo = new map<id,id>();

map<id, AccountTeamMember> matm = newmap<id, AccountTeamMember>();

map<id, map<id, AccountTeamMember>> mmatm = new map<id, map<id, AccountTeamMember>>();

map<id, AccountTeamMember> current_matm = newmap<id, AccountTeamMember>();

map<id, map<id, AccountTeamMember>> current_mmatm = new map<id, map<id, AccountTeamMember>>();

map<id, UserRole> UserRoleMapByRoleID = new map<id, UserRole>();

map<id, map<id, UserRole>>  UserRoleMapByRoleIDbyAccountId = new map<id, map<id, UserRole>>();  

integer i=0; 

UserRole urole = newUserRole();

list<AccountShare> listnewAccountShare = new list<AccountShare>();

AccountShare newAccountShare;

list<UserRole> urolelist = new list<UserRole>();

for (Account acc: trigger.new){

if (acc.Authorized_Support_Partner__c == true){

account_Id.add(acc.id);

system.debug('Added this ID to Account_ID ' + acc.id);

}

system.debug(account_Id);

system.debug('fire');

 

list<Partner> p = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountToId in: account_id and role = 'Channel Partner - Auth Support'];

 

 

for(Partner pc: p){

if(!account_to.contains(pc.accountToId)){

account_to.add(pc.accountToId);

system.debug('Account to id is: ');

}

if(!ShareFromTo.containsKey(pc.accountFromId)){

ShareFromTo.put(pc.accountFromId, pc.accountToId);

system.debug('Account From and Account to are: ');

}else{

if(pc.accountToId!=ShareFromTo.get(pc.accountFromId)){

ShareFromTo.put(pc.accountFromId, pc.accountToId);

system.debug('ShareFromTo: ');

}

}

}

 

list<UserRole> ur = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId, 

u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner, 

u.CaseAccessForAccountOwner 

From UserRole

where u.PortalAccountOwnerId in :account_to];

for(UserRole uri:ur){

system.debug('Get user role id' + uri.id);

if(!UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId)){

UserRoleMapByRoleID.put(uri.id, uri);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}else{

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(uri.PortalAccountId);

if(!UserRoleMapByRoleID.containsKey(uri.id)){

UserRoleMapByRoleID.put(uri.id, uri);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}

}

 

list<AccountTeamMember> ls = [Select a.UserId, a.TeamMemberRole, a.AccountId, a.AccountAccessLevel From AccountTeamMember a where a.AccountId in :account_Id ];

 

for(AccountTeamMember atm: ls){

if(mmatm.containsKey(atm.AccountId)){

current_matm = mmatm.get(atm.AccountId);

if(!current_matm.containsKey(atm.UserId)){

current_matm.put(atm.UserId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}else{

current_matm.put(atm.UserId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}

 

for(id aid:account_Id){

if(UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId)){

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(aid);

urolelist = UserRoleMapByRoleID.Values();

for(UserRole eachUserRole:urolelist){

if(mmatm.containsKey(aid)){

current_matm = mmatm.get(aid);

if(!current_matm.containsKey(eachUserRole.id)){

newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'Private';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}else{

system.debug('ShareExists');

}

}else{

 

            newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'Private';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}

 

}

}

}

 

system.debug(newAccountShare);

   list<Database.Saveresult> lsr = Database.insert(listnewAccountShare,false);

  // Create counter 

i=0;

// Process the save results 

  

    for(Database.SaveResult sr : lsr){

        if(!sr.isSuccess()){

            // Get the first save result error 

            Database.Error err = sr.getErrors()[0];

            // Check if the error is related to a trivial access level 

            // Access levels equal or more permissive than the object's default  

            // access level are not allowed.  

            // These sharing records are not required and thus an insert exception is  

            // acceptable.  

            if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  

                                        &&  err.getMessage().contains('AccessLevel'))){

                // Throw an error when the error is not related to trivial access level. 

               trigger.newMap.get(sr.Id).

               addError('Unable to grant sharing access due to following exception: ' + err.getMessage());

            }

        } 

        i++;

    }

}

}

sf_evolutionsf_evolution

Hi-

 

I don't know much about account sharing, and I'm only providing a sounding board - apologies in advance, but the FIELD_FILTER_VALIDATION_EXCEPTION usually occurrs (at least with me) when I try to modify a lookup-relation field.

 

 

My guess is that at least one of the fields you are trying to modify is being modified to a user that doesn't exist in your account sharing team. But I'm really only guessing.

 

MrkennyMrkenny

I have it working now but i need to figure out how to take out sharing if the authorized for support isnt checked. That is if you check the box and the sharing rules are added once the checkbox is unchecked i need to remove the sharing. If you have any suggestions pleas reply to this here is my code below. The part highlighted in red is what i cant seem to figure out.

 

 

trigger PartnerSharing onAccount (afterinsert, afterupdate){

/*This left to do:

1. Check if trigger is insert or Update

a. If Update check if Authorized_Support_Partner__c was updated using trigger.old (it,s a map, so you can get the correct account object using .get(acc.id))

  This is an update that occurs whenever Authorized Support P 

b. if was tru now false remove shares else add (add is complete)

2. comment the **bleep** out of this mess

*/

 

set<id> account_id = new set<id>();

set<id> account_to = new set<id>();

set<id> account_from;

map<id,set<id>> ShareFromTo = new map<id,set<id>>();

map<id, AccountShare> matm = new map<id, AccountShare>();

map<id, map<id, AccountShare>> mmatm = new map<id, map<id, AccountShare>>();

map<id, AccountShare> current_matm = new map<id, AccountShare>();

map<id, map<id, AccountShare>> current_mmatm = new map<id, map<id, AccountShare>>();

map<id, Group> UserRoleMapByRoleID = new map<id, Group>();

map<id, map<id, Group>> UserRoleMapByRoleIDbyAccountId = new map<id, map<id, Group>>();  

integer i=0; 

UserRole urole = newUserRole();

set<id> UserRoleIDs = new set<id>();

list<AccountShare> listnewAccountShare = new list<AccountShare>();

list<AccountShare> listnewAccountShare2 = new list<AccountShare>();

AccountShare newAccountShare;

list<Group> urolelist = new list<Group>();

for (Account acc: trigger.new){

//check trigger old map if is update trigger so this only runs if the field has been changed

if (acc.Authorized_Support_Partner__c == true){

account_Id.add(acc.id);

system.debug('Added this ID to Account_ID ' + acc.id);

}

 

system.debug(account_Id);

system.debug('fire');

 

list<Partner> p = [Select p.Role, p.AccountToId, p.AccountFromId From Partner p where p.AccountFromId in: account_id and role = 'Channel Partner - Auth Support'];

 

 

for(Partner pc: p){

if(!account_to.contains(pc.accountToId)){

account_to.add(pc.accountToId);

system.debug('Account to id is: ' + pc.accountToId);

}

account_from = new set<id>();

if(!ShareFromTo.containsKey(pc.accountToId)){

account_from.add(pc.accountToId);

ShareFromTo.put(pc.accountFromId, account_from);

system.debug('Account From and Account to are: ' + pc.accountToId);

}else{

 

for(id sfi:ShareFromTo.get(pc.accountToId)){

if(pc.accountFromId!=sfi){

account_from = ShareFromTo.get(pc.accountFromId);

account_from.add(pc.accountToId);

ShareFromTo.put(pc.accountFromId, account_from);

system.debug('ShareFromTo: ');

}

}

}

}

 

//Select g.Type, g.RelatedId, g.Id, g.DeveloperName From Group g where g.relatedID = '00E80000001DBoTEAW'  and g.type = 'Role'

 

 

 

list<UserRole> ur = [Select u.PortalType, u.PortalRole, u.PortalAccountOwnerId, 

u.PortalAccountId, u.Name, u.Id, u.ContactAccessForAccountOwner, 

u.CaseAccessForAccountOwner 

From UserRole

where u.PortalAccountId in :account_to];

for(UserRole eur:ur){

UserRoleIds.add(eur.id);

}

list<Group> UserGroups = new list<Group>();

 

UserGroups = [Select g.Type, g.RelatedId, g.Id, g.DeveloperName From Group g where g.relatedID in :UserRoleIds  and g.type = 'Role'];

map<id, Group> UserGroupsMap = new map<id, Group>();

for (Group gm: UserGroups){

if(!UserGroupsMap.containsKey(gm.RelatedId)){

UserGroupsMap.put(gm.RelatedId, gm);

}

}

 

for(UserRole uri:ur){

system.debug(uri);

if(!UserRoleMapByRoleIDbyAccountId.containsKey(uri.PortalAccountId)){

system.debug('New UserRoleMap by Account id for Account: ' + uri.PortalAccountId);

if(UserGroupsMap.containsKey(uri.id)){

UserRoleMapByRoleID.put(uri.id, UserGroupsMap.get(uri.id));

system.debug('New UserRoleMap by id : ' + uri.id);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}else{

system.debug('Group does Not Exist');

}

}else{

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(uri.PortalAccountId);

system.debug('Existing UserRoleMap by Account id for Account: ' + uri.PortalAccountId);

if(!UserRoleMapByRoleID.containsKey(uri.id)){

system.debug('New UserRoleMap by id: ' + uri.Id);

if(UserGroupsMap.containsKey(uri.id)){

UserRoleMapByRoleID.put(uri.id, UserGroupsMap.get(uri.id));

system.debug('New UserRoleMap by id : ' + uri.id);

UserRoleMapByRoleIDbyAccountId.put(uri.PortalAccountId, UserRoleMapByRoleID);

}else{

system.debug('Group does Not Exist');

}

}else{

system.debug('User role id already in Map: ' + uri.id);

}

}

}

 

list<AccountShare> ls = [Select a.UserOrGroupId, a.RowCause, a.OpportunityAccessLevel, a.Id, a.CaseAccessLevel, a.AccountId, a.AccountAccessLevel From AccountShare a where a.AccountId in :account_Id ];

system.debug(ls);

 

for(AccountShare atm: ls){

if(mmatm.containsKey(atm.AccountId)){

system.debug('mmatm already contains : ' + atm.AccountId);

current_matm = mmatm.get(atm.AccountId);

system.debug(current_matm);

if(!current_matm.containsKey(atm.UserOrGroupId)){

system.debug('new current_matm :' + atm.UserOrGroupId );

current_matm.put(atm.UserOrGroupId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}else{

system.debug('New mmatm : ' + atm.AccountId);

system.debug('new current_matm :' + atm.UserOrGroupId );

current_matm.put(atm.UserOrGroupId, atm);

mmatm.put(atm.AccountID,current_matm);

}

}

 

for(id aid:account_Id){

system.debug('Account ID = ' + aid);

account_from = ShareFromTo.get(aid);

    for(id PartnerID : account_from){

if(UserRoleMapByRoleIDbyAccountId.containsKey(PartnerID)){

system.debug('Account Sharing Map contains :' +aid);

UserRoleMapByRoleID = UserRoleMapByRoleIDbyAccountId.get(PartnerID);

system.debug(UserRoleMapByRoleID);

urolelist = UserRoleMapByRoleID.values();

for(Group eachUserRole:urolelist){

system.debug('Check if UserRole exist, ' + eachUserRole);

if(mmatm.containsKey(aid)){

current_matm = mmatm.get(aid);

if(!current_matm.containsKey(eachUserRole.id)){

newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'None';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}else{

system.debug('ShareExists');

}

}else{

 

            newAccountShare = new AccountShare();

    newAccountShare.AccountAccessLevel = 'Read';

    newAccountShare.AccountId = aid;

    newAccountShare.OpportunityAccessLevel = 'None';

    newAccountShare.CaseAccessLevel='Edit';

    newAccountShare.UserOrGroupId = eachUserRole.id;

    listnewAccountShare.add(newAccountShare);

}

 

}

}

}

}

 

for(Account acc : Trigger.New){

    if(acc.Authorized_Support_Partner__c == false)

    {

    AccountShare newAccountShare = [select id from AccountShare where AccountId = :acc.id and UserOrGroupId = eachUserRole.id];

 

    listnewAccountShare2.add(newAccountShare);

    }

    if(listnewAccountShare2.size()>0){

    delete listnewAccountShare2;

    }

system.debug(newAccountShare);

   list<Database.Saveresult> lsr = Database.insert(listnewAccountShare,false);

  // Create counter 

i=0;

// Process the save results 

  

    for(Database.SaveResult sr : lsr){

        if(!sr.isSuccess()){

            // Get the first save result error 

            Database.Error[] err = sr.getErrors();

for(Database.Error err1:err){

system.debug(err1);

}

        }else{

        system.debug(sr);

        }

         

        i++;

    }

}

}