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
sindoorasindoora 

Adding Account team member dynamically

Hi All,

 

I'm trying to add Account team member dynamically but my code fires error msg.Please go through the steps below.

 

Parent Account(Lookup Field)----------------on Account Object

VPA(Checkbox)------------------------------------on Account Object.

If VPA==true

then the owner of parent account to which it is lookup should be added as account teammember on account page.

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[0].ParentID];
ID uid = a1.Owner;


//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++; }
}

 

 

Thank you inadvance

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
Change "Owner" to OwnerId.

Regards,
Bhavi Sharma
Certified Salesforce Consultant
bhavi@simplyforce.com/bhawani.sh.sharma@gmail.com
+91-9928130589
LinkedIn |
Twitter
| Blog |
Community
www.simplyforce.com

All Answers

Bhawani SharmaBhawani Sharma
What this error message is ?
sindoorasindoora


Error: Compile Error: No such column 'Owner' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 17

sindoorasindoora

Here i'm trying to get the owner id,name of parentAccount.But this displays an error message

Bhawani SharmaBhawani Sharma
Change "Owner" to OwnerId.

Regards,
Bhavi Sharma
Certified Salesforce Consultant
bhavi@simplyforce.com/bhawani.sh.sharma@gmail.com
+91-9928130589
LinkedIn |
Twitter
| Blog |
Community
www.simplyforce.com
This was selected as the best answer
sindoorasindoora

oops i missed the id.Thank you.

sindoorasindoora
could you tell me where shoul i add the condition like if(Account.VPA!=null) then this code should be runned