-4

trigger AccountTeam on Account (after insert) { for(Account acct : trigger.new){ if(trigger.isafter && acct.Parent!= null) { 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,VPA_c, parent.Id,OwnerId from account Where Id=:trigger.new[0].ParentID]; ID uid = a1.OwnerId; Boolean x=a1.VPA_c;

//get the user id of the parent Account, if VPA is checked and will added to the account team if(x == True) { for(Account a:trigger.new) {

AccountTeamMember Teammemberad=new AccountTeamMember(); 
Teammemberad.AccountId=a.id;
 Teammemberad.UserId=uid; 
 Teammemberad.TeamMemberRole = 'Global KA Manager'; 
 newmembers.add(Teammemberad); 
 } 
    Database.SaveResult[] lsr = Database.insert(newmembers,false);
 //insert any valid members then add their share entry if they were successfully added ; 
 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++; }
      }
      }
      }
      }

closed as unclear what you're asking by battery.cord, Gortonington, glls, superfell, Dave Humm Nov 9 '17 at 9:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    When you say doesn't fire as expected. What happens? What do you see in the Debug logs? – haagen Jan 2 '14 at 8:45
  • required (...)+ loop did not match anything at input 'trigger' – user4902 Jan 2 '14 at 8:53
  • 2
    I'm afraid that's still a bit too vague. What is outputted on the debug statements that you put in? Also using the trigger.new[0] in your select query will lead to errors if any more than 1 accounts are inserted/updated at a time. First loop through all accounts in the trigger, collect the parents, and then query for all the parent-owners at once. Store the parentId->ownerId links in a map and use that map to link the Accounts in the trigger to the correct Parent-Owner. – Guy Clairbois Jan 2 '14 at 9:22
  • @guy thank you. – user4902 Jan 2 '14 at 9:52
  • 1
    You would need to define the map as Map<ID, ID> and then you would populate it with values which you get from parent and owner id. – Doug B Jan 2 '14 at 10:53
1

First off, there were a couple syntax errors I found when I was going through formatting the code for you. The first one is on line 3, where you are iterating over an sObject rather than a list or collection:

for(Account a1:a){

Second, you never set any fields on the account record "a", so your if check here will always return false:

 if(trigger.isinsert & a.Parent!=null){

I'm wondering if this was a misedit of the code.

When I take a look at the previous versions of your trigger, you are trying to reference the "Parent" field directly from Trigger.new, which will be blank an still return false for the if condition. So, try changing the if statement to look at a.ParentId instead of a.Parent.

I'm sure there are other problems than this. I would suggest throwing System.debugs in your code and walking through the various errors.

If you post a better code snippet, or the actual code, hopefully we can help a bit more.

Also, you may want to check this out for tips on trigger best practices: http://wiki.developerforce.com/page/Apex_Code_Best_Practices

And also check out this awesome trigger handler pattern, which helps to move your logic out of the trigger and into a more defined class and method structure here: https://github.com/kevinohara80/sfdc-trigger-framework

  • @james.here is the code which i'm using see above – user4902 Jan 3 '14 at 5:44
  • @user4902 can you please go back in, reformat the code above so it looks nice, and also add back your original question with some additional detail on what you're trying to do? – James Loghry Jan 3 '14 at 14:53

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