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
MyGodItsColdMyGodItsCold 

How to Query AccountTeamMember table to get UserId with specific Role?

In a trigger, I want to assign a field to the UserID of the user who has 'Account Primary' role.
Code:
      for (integer i=0; i<trigger.size; i++) {
       Account acct = Select (Select Id, TeamMemberRole, UserId From AccountTeamMember 
         Where TeamMemberRole = 'Account Primary') From Account 
Where id = :trigger[i].SERVICING_ACCOUNT__c);
trigger[i].APPROVING_USER__c = acct.AccountTeamMember.UserId; }

On the line beginning, "Account acct ...", I'm getting a Problem message of:

Save error: expecting a semi-colon, found 'acct'

There are no missing ' ; ' end of line characters prior to the line.

Once that is working, then the line beginning, "trigger[i].APP..." may cause me problems. I'm not sure if the '__r' designation is required - or, that I've got the naming of the acct.AccountTeamMember.UserId variable correct.

Thanks,
 

Best Answer chosen by Admin (Salesforce Developers) 
MyGodItsColdMyGodItsCold
I hope this helps someone else.

Code:
trigger CorrApprovalInputFinisher on CORR_APPROVAL__c (before insert, before update) {
      boolean dbug_me = false;
      Id tgtId = null;
      List<AccountTeamMember> reslist = null;
      List<CORR_APPROVAL__c> changedApproval = trigger.new;
      //List<CORR_APPROVAL__c> origApproval = trigger.old;

      for (integer i=0; i<trigger.size; i++) {
       tgtId = changedApproval[i].SERVICING_ACCOUNT__c;
 reslist = [SELECT AccountId,TeamMemberRole,UserId FROM AccountTeamMember 
where TeamMemberRole = 'Account Primary' and AccountId = :tgtId limit 1]; //if (reslist.size > 0) { // the above gave: Save error: Initial term of field expression must be
// a concrete SObject: LIST:SOBJECT:AccountTeamMember //if (reslist.size() <> null) { // the above didn't work either, but the line below did (AccountTeamMember must not be a concrete SObject)... if (reslist.isempty() == false) { AccountTeamMember m = reslist[0]; changedApproval[i].APPROVING_USER__c = m.UserId; } changedApproval[i].SUBMITTING_USER__c = UserInfo.getUserId(); } }

 Thanks to the patient support person who helped.

All Answers

RickyGRickyG
Don't you need square brackets around the SOQL statement?  And yes, if you are traversing a relationship, you need the __r.
MyGodItsColdMyGodItsCold
Thanks, I think I'm closer. Here's what I tried:


Code:
trigger CorrApprovalInputFinisher on CORR_APPROVAL__c (before insert) {

Id tgtId = null;
List<CORR_APPROVAL__c> changedApproval = trigger.new;


for (integer i=0; i<trigger.size; i++) {

tgtId = changedApproval[i].SERVICING_ACCOUNT__c;

Account acct = [Select (Select Id, TeamMemberRole, UserId From AccountTeamMember
Where TeamMemberRole = 'Account Primary') From Account Where id = :tgtId];

trigger[i].APPROVING_USER__c = acct.AccountTeamMember__r.UserId;

}

}

I'm getting:

Save error: unexpected token: tgtId

on the line beginning tgtId = ...   - right under the 'for' line

and SERVICING_ACCOUNT__c is a lookup field into Account.

(I'm hoping there's a careless mistake I'm too close to see...)





MyGodItsColdMyGodItsCold
Now, I tried this:

Code:
trigger CorrApprovalInputFinisher on CORR_APPROVAL__c (after insert) {
    Id tgtId = null;
    List<CORR_APPROVAL__c> changedApproval = trigger.new;

    for (integer i=0; i<trigger.size; i++) {
        tgtId = changedApproval[i].SERVICING_ACCOUNT__c;

        List<AccountTeamMember> reslist = [SELECT AccountId,TeamMemberRole,UserId 
         FROM AccountTeamMember 
         where TeamMemberRole = 'Account Primary' and AccountId = :tgtId limit 1];

        if (reslist != null) {
            AccountTeamMember m = reslist[0];
            trigger[i].APPROVING_USER__c = m.UserId;
        }

    }

} 

The error message I'm getting when I try to assign 'm',  (2nd code line from last code line), is:

"Save error: unexpected token: ["

Thanks


MyGodItsColdMyGodItsCold
I hope this helps someone else.

Code:
trigger CorrApprovalInputFinisher on CORR_APPROVAL__c (before insert, before update) {
      boolean dbug_me = false;
      Id tgtId = null;
      List<AccountTeamMember> reslist = null;
      List<CORR_APPROVAL__c> changedApproval = trigger.new;
      //List<CORR_APPROVAL__c> origApproval = trigger.old;

      for (integer i=0; i<trigger.size; i++) {
       tgtId = changedApproval[i].SERVICING_ACCOUNT__c;
 reslist = [SELECT AccountId,TeamMemberRole,UserId FROM AccountTeamMember 
where TeamMemberRole = 'Account Primary' and AccountId = :tgtId limit 1]; //if (reslist.size > 0) { // the above gave: Save error: Initial term of field expression must be
// a concrete SObject: LIST:SOBJECT:AccountTeamMember //if (reslist.size() <> null) { // the above didn't work either, but the line below did (AccountTeamMember must not be a concrete SObject)... if (reslist.isempty() == false) { AccountTeamMember m = reslist[0]; changedApproval[i].APPROVING_USER__c = m.UserId; } changedApproval[i].SUBMITTING_USER__c = UserInfo.getUserId(); } }

 Thanks to the patient support person who helped.

This was selected as the best answer