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
Sandeep M 1Sandeep M 1 

execution of BeforeUpdate caused by: System.StringException: Invalid id: in trigger

I want to update the field in multiple records based on the unique_pile_id__c that is unique for all the multiple records. I am trying to write trigger like this

trigger opMetricsTrigger on Operational_Metrics__c (before insert,before update)
{
Operational_Metrics__c[] op=null;
Set<Id> opIds = new Set<Id>();
public static boolean run = true;
if(run)
{

   run=false;
for (Operational_Metrics__c e : Trigger.new)
{
     if(e.Unique_Pile_ID__c != null)
     opIds.add(e.Unique_Pile_ID__c );
}

  Map<Id, Operational_Metrics__c> y = new Map<Id, Operational_Metrics__c>([select Unique_Pile_ID__c,unique_id__C, Project__c,Composting_Method__c,Pile_Monitoring_Date__c,Average_Temperature__c,Moisture_Test__c,Pile_Monitoring_Action_Taken__c,Other_Action__c,Pile_Input_Register_Date__c,Total_Waste_Input_on_the_Pile__c,Pile_Completion_Stage__c,Date_Pile_COmpleted__c,Curing_Batch_Register_Date__c,No_of_Days__c  from operational_metrics__c where Unique_Pile_ID__c in :opIds]);
for (Operational_Metrics__c oc : [select Unique_Pile_ID__c,unique_id__C, Project__c,Composting_Method__c,Pile_Monitoring_Date__c,Average_Temperature__c,Moisture_Test__c,Pile_Monitoring_Action_Taken__c,Other_Action__c,Pile_Input_Register_Date__c,Total_Waste_Input_on_the_Pile__c,Pile_Completion_Stage__c,Date_Pile_COmpleted__c,Curing_Batch_Register_Date__c,No_of_Days__c  from operational_metrics__c where Unique_Pile_ID__c in :opIds])
{
      System.debug('oc '+y.get(oc.Unique_Pile_ID__c));
}


}
}

but when i am saving a records it is throwing execution of BeforeUpdate caused by: System.StringException: Invalid id: 10-Ber1: External entry point here 10-Ber1 is the unique_pile_id__c could any one help me out
Abhinav GuptaAbhinav Gupta
If  "unique_pile_id__c" value is not an ID you can't use "Set<Id> opIds" to store it. 
Please change it as follows:

// Before
Set<Id> opIds = new Set<Id>();

// After
Set<String> opIds = new Set<String>();