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
BobBob 

Account Contact Role

I have a trigger that I need to find a specific role name if the account contact role name is 
"Primary Service Contact" the trigger will inserted or update the field named (
Account_Case_Contact__c with the account contact role name. I tried added this code to my Where statement, but it obvioysly didn't work. Can anyone help me?
)
trigger Trigger_CasePrimaryContact on Account (before insert, before update) {


   for (Account a : Trigger.new) {
   // a.NextStep = 'DEBUG: Running function...';



       AccountContactRole[] contactRoleArray =
       [select ContactID, Role from AccountContactRole where AccountId = :a.id && Role='Primary Service Contact' ORDER BY Role DESC, createdDate];
       if (contactRoleArray.size() > 0) {
         
         
           a.Account_Case_Contact__c = contactRoleArray[0].ContactID;
          
       }else{
      
           // IF NO CONTACT ROLES EXIST RETURN NULL...
           a.Account_Case_Contact__c = null;
           // a.NextStep = 'DEBUG; CONTACT = null ';
       }
   }
    


}
Best Answer chosen by Bob
William TranWilliam Tran
Try using AND instead of && so

select ContactID, Role from AccountContactRole where AccountId = :a.id AND Role='Primary Service Contact' ORDER BY createdDate

Thx

All Answers

William TranWilliam Tran
How do you know it doesn't work?  It errored out? No rows are returned? Something else?

I need to find a specific role name if the account contact role name is "Primary Service Contact" - what does this mean? specific role name?

select ContactID, Role from AccountContactRole where AccountId = :a.id &&Role='Primary Service Contact' ORDER BY Role DESC, createdDate

What are you trying to do? Does the role 'Primary Service Contact' exist? You probably don't need ORDER BY Role DESC since only 1 role is returned.

Thx
BobBob
Yes i need to find that specific role name 
Primary Service Contact

The error I get is below while trying to save the change. 
Error: Compile Error: Variable does not exist: Role at line 18 column 83

I will remove the Order By DESC. 
William TranWilliam Tran
Try using AND instead of && so

select ContactID, Role from AccountContactRole where AccountId = :a.id AND Role='Primary Service Contact' ORDER BY createdDate

Thx
This was selected as the best answer
BobBob
Thank you for your help! The AND worked