2

I have a requirement that based on the account owner and country ,account team has to be

automatically assigned .what are ways we can do it?

make a comment if you dont understand the question.

  • can you make it clear u r question..?? – Sathya Jan 7 '14 at 7:45
  • in account 2 fields are there account owner and country – Teja Jan 7 '14 at 8:34
  • Example :if account owner is "teja" country "india" then account team member is "alex".if account owner is "devi" country"france" then account team member "lois". In the similar way account owner and country is some other it has take account team member as defined – Teja Jan 7 '14 at 8:37
  • Use MAP in apex code. Take owner name in if condition and map country , account team member. – Sindoora Jan 7 '14 at 10:29
  • can you send me some sample code it would be more helpful – Teja Jan 7 '14 at 10:56
3
trigger AccountTeam on Account (after update) {
    Integer newcnt = 0;
    Integer newcnt0 = 0;
    AccountTeamMember[] newmembers = new AccountTeamMember[]{};

    //list of new team members to add
    AccountShare[] newShare = new AccountShare[]{};

    //list of new shares to add
    Account a1 = [select id, parent.Id,Owner from account Where Id=:trigger.new.ParentID];
    ID uid = a1.Ownerid;


    //get the user id of the user running the trigger, anyone that changes the Account will added to the account team
    for(Account a:trigger.new) {
        AccountTeamMember Teammemberad=new AccountTeamMember();
        Teammemberad.AccountId=a.id;
        Teammemberad.UserId=uid;
        Teammemberad.TeamMemberRole = 'Account Modifier';
        newmembers.add(Teammemberad);
    }

    Database.SaveResult[] lsr = Database.insert(newmembers,false);
    //insert any valid members then add their share entry if they were successfully added Integer newcnt=0;
    for(Database.SaveResult sr:lsr) {
        if(!sr.isSuccess()) {
            Database.Error emsg =sr.getErrors()[0];

            system.debug('\n\nERROR ADDING TEAM MEMBER:'+emsg);
        } else {

            newShare.add(new AccountShare(UserOrGroupId=newmembers[newcnt].UserId, AccountId=newmembers[newcnt].Accountid, AccountAccessLevel='Read',OpportunityAccessLevel='Read'));
        }
        newcnt++;
    }

    Database.SaveResult[] lsr0 =Database.insert(newShare,false);
    //insert the new shares Integer newcnt0=0;
    for(Database.SaveResult sr0:lsr0) {
        if(!sr0.isSuccess()) {
            Database.Error emsg0 = sr0.getErrors()[0];
            system.debug('\n\nERROR ADDING SHARING:'+newShare[newcnt0]+'::'+emsg0); 
        } 
        newcnt0++; 
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.