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
Synthia BeauvaisSynthia Beauvais 

Incompatible element type Schema.SObjectField for collection of Id

  1. Work Order is created from the Account Object
  2. When work order is saved, LookUp Field: Primary_Sales_Rep_Name__c should populate with the name from field Primary_Sales_Rep_Name__c from the Account Object
  3. Getting Error on Line 5: Incompatible element type Schema.SObjectField for collection of Id
trigger UpdatePSR on WorkOrder (before insert, before update){
 Set<id> accIds = new Set<id>();
 for(WorkOrder Account: trigger.new){
  if(WorkOrder.Primary_Sales_Rep_Name__c!= null){
   accIds.add(WorkOrder.Primary_Sales_Rep_Name__c);
  }
 }
if(accIds.size() > 0){
  Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Primary_Sales_Rep_Name__c from Account where id in :accIds]);
   if(mapOfAccounts.containsKey(Account.Primary_Sales_Rep_Name__c)){
    Account.Primary_Sales_Rep_Name__c = mapOfAccounts.get(Account.Primary_Sales_Rep_Name__c).Primary_Sales_Rep_Name__c;
   }
  }
 }
Thanks in advance! 

 
Naval Sharma4Naval Sharma4
Hi Synthia,

Updated your code.
trigger UpdatePSR on WorkOrder (before insert, before update){
 Set<id> accIds = new Set<id>();
 for(WorkOrder WORecord: trigger.new){
  if(WORecord.Primary_Sales_Rep_Name__c!= null){
   accIds.add(WORecord.AccountId);
  }
 }
if(accIds.size() > 0){
  Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Primary_Sales_Rep_Name__c from Account where id in :accIds]);
   for(WorkOrder WORecord: trigger.new){
      if(mapOfAccounts.containsKey(WORecord.WORecord)){
         WORecord.Primary_Sales_Rep_Name__c =    
         mapOfAccounts.get(Account.WORecord).Primary_Sales_Rep_Name__c;
      }
    }
  }
}


Let me know if you need any help.

Thanks,

Naval

Synthia BeauvaisSynthia Beauvais
Hi Naval, 

I am getting Compile Error: Variable does not exist: Account.WORecord at line 13 column 28
Naval Sharma4Naval Sharma4
Hi,

Please update line 13 to following line.
mapOfAccounts.get(WORecord.AccountId).Primary_Sales_Rep_Name__c;

 
Synthia BeauvaisSynthia Beauvais
Hi Naval, 

Here is my latest update. I renamed the field to Primary_Sales_Rep_2__c on the Work Order Object. I am getting a new error. 

Compile Error: Invalid field WORecord for SObject WorkOrder at line 11 column 36​
 
trigger UpdatePSR on WorkOrder (before insert, before update){
 Set<id> accIds = new Set<id>();
 for(WorkOrder WORecord: trigger.new){
  if(WORecord.Primary_Sales_Rep_2__c!= null){
   accIds.add(WORecord.AccountId);
  }
 }
if(accIds.size() > 0){
  Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Primary_Sales_Rep_Name__c from Account where id in :accIds]);
   for(WorkOrder WORecord: trigger.new){
      if(mapOfAccounts.containsKey(WORecord.WORecord)){
         WORecord.Primary_Sales_Rep_2__c =    
         mapOfAccounts.get(WORecord.AccountId).Primary_Sales_Rep_Name__c;
      }
    }
  }
}

 
Naval Sharma4Naval Sharma4
Sorry for all trouble. Actually I am not compiling this code on my system. Try this please.
trigger UpdatePSR on WorkOrder (before insert, before update){
 Set<id> accIds = new Set<id>();
 for(WorkOrder WORecord: trigger.new){
  if(WORecord.Primary_Sales_Rep_2__c!= null){
   accIds.add(WORecord.AccountId);
  }
 }
if(accIds.size() > 0){
  Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Primary_Sales_Rep_Name__c from Account where id in :accIds]);
   for(WorkOrder WORecord: trigger.new){
      if(mapOfAccounts.containsKey(WORecord.AccountId)){
         WORecord.Primary_Sales_Rep_2__c =    
         mapOfAccounts.get(WORecord.AccountId).Primary_Sales_Rep_Name__c;
      }
    }
  }
}

 
Synthia BeauvaisSynthia Beauvais
Thank you for your help Naval. The code throws no errors however it is not updating the field. 
Naval Sharma4Naval Sharma4
trigger UpdatePSR on WorkOrder (before insert, before update){
 Set<id> accIds = new Set<id>();
 for(WorkOrder WORecord: trigger.new){
  if(WORecord.Primary_Sales_Rep_2__c == null){
   accIds.add(WORecord.AccountId);
  }
 }
if(accIds.size() > 0){
  Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Primary_Sales_Rep_Name__c from Account where id in :accIds]);
   for(WorkOrder WORecord: trigger.new){
      if(mapOfAccounts.containsKey(WORecord.AccountId)){
         WORecord.Primary_Sales_Rep_2__c =    
         mapOfAccounts.get(WORecord.AccountId).Primary_Sales_Rep_Name__c;
      }
    }
  }
}

Try this again.
Synthia BeauvaisSynthia Beauvais
No errors, no update on the filed. It remain blank after update. 
Naval Sharma4Naval Sharma4
Can you name those fields which you want to populate.

Can you try this again.
 
trigger UpdatePSR on WorkOrder (before insert, before update){
 Set<id> accIds = new Set<id>();
 for(WorkOrder WORecord: trigger.new){
   accIds.add(WORecord.AccountId);
 }
if(accIds.size() > 0){
  Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Primary_Sales_Rep_Name__c from Account where id in :accIds]);
   for(WorkOrder WORecord: trigger.new){
      if(mapOfAccounts.containsKey(WORecord.AccountId)){
         WORecord.Primary_Sales_Rep_2__c =    
         mapOfAccounts.get(WORecord.AccountId).Primary_Sales_Rep_Name__c;
      }
    }
  }
}

 
Synthia BeauvaisSynthia Beauvais
Hi Naval, 

Primary_Sales_Rep_Name__c from account to Primary_Sales_Rep_2__c on work order
 
Naval Sharma4Naval Sharma4
Please make sure that your account records does have Primary_Sales_Rep_Name.
Synthia BeauvaisSynthia Beauvais
Hi Naval, 

There is a name in the field.