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
strangebiscuitstrangebiscuit 

Invalid bind expression type - trigger connecting entries between two related custom objects

I've been trying to create a trigger to connect entries between two custom objects upon bulk upload...I was able to get some help with the code but there's a compile error that I can't figure out. Can anyone help? It'd be greatly appreciated.

 

What we're trying to do:  basically we have two custom objects, "Sales" and "Reps". We upload a list of sales (in .csv format) into the Sales object each week. We want to make it so that every time a Sale entry gets created it is assigned to the Rep who made that sale (Sales has a master-detail relationship with Reps). Each sale lists the Name and ID# of the rep who made it, but not the Salesforce ID of the corresponding Rep entry. So we want the apex trigger to match each Sale to the correct Rep by comparing the Name and/or ID fields in both objects.

 

The code we've got so far is:

trigger SaleTrigger on Sale__c (before insert){

   //This set will store all the RepId's.

   Set<String> repIDs = new Set<String>();

   //Loop through all the new sales to be created.
   for(Sale__c sale : Trigger.new){
      //Get the rep's ID of each sale and add it to the Set
      //Note: I am assuming the API/Unique name of the Rep ID     
      //field is Rep_Id__c;
      repIDs.add(sale.Rep_ID__c);
   }

   //Now we have all the Rep ID's in the set.
   // Issue an single SOQL to get the Salesforce ID's of all the reps.
   
   Map<String,ID> mapReps = new Map<String,ID>();
   for(Rep__c repRecord : [Select ID,Rep_ID__c From Rep__c WHERE Rep_ID__c IN (:repIDs)]){
      mapReps.put(repRecord.Rep_ID__c,repRecord.ID);
   }
   
  //The mapReps has a map of all Rep ID's with Rep Salesforce ID's
   for(Sale__c sale : Trigger.new){
      ID repSalesforceID = mapReps.get(sale.Rep_ID__c);
      //This is the MD field in Sale__c.
      sale.Rep__c = repSalesforceID;
   }
   
}

 

The error I'm getting on trying to save is:  "Invalid bind expression type of SET<String> for column of type String"

It occurs on line 19 which is: 

 

for(Rep__c repRecord : [Select ID,Rep_ID__c From Rep__c WHERE Rep_ID__c IN (:repIDs)]){

 

Can anyone help me figure this out? I'd be extremely grateful.

 

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi,

 

Its simple...Change your code in line # 19 to

for(Rep__c repRecord : [Select ID,Rep_ID__c From Rep__c WHERE Rep_ID__c IN :repIDs]){

 

All Answers

magicforce9magicforce9

Hi,

 

Its simple...Change your code in line # 19 to

for(Rep__c repRecord : [Select ID,Rep_ID__c From Rep__c WHERE Rep_ID__c IN :repIDs]){

 

This was selected as the best answer
strangebiscuitstrangebiscuit

I think that did it! It certainly was a simple change...I'm quite new to Apex and just couldn't see it. I guess I don't really understand why that was wrapped in parenthesis in the first place.

 

Anyway, I did a small test upload using the Data Loader and it seems to work! All the Sales got assigned to the correct Reps and there was no Salesforce ID listed in the .csv upload. I just had to avoid setting a map for the standard name field ("Rep") itself.

 

I've also got a quick followup question that you may or may not be able to answer...this works by grabbing the Rep ID from the custom Rep_ID__C field in my Rep object. But what if I wanted to use the Rep IDs as the actual standard "name" field on my custom object, rather than keeping them in a separate custom field? Would this trigger still be feasable if this were the case? Could I just change Rep_ID__C in the code to Rep__C?

 

Thanks again in any case, you really helped me out a lot!!

magicforce9magicforce9

I'm glad it worked...Let me ask you: is the field Rep_ID__C an external ID ? I can see the relationship between Sales__c and Rep__c object, I think you should be able to use Rep__c field on Sales__c because of the relationship which actually holds the Id of Rep__c object record that its looking up to. If you are using Force.com IDE or Eclipse then look'up the related field API names in Schema explorer.