3

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 record 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 anyone help me out?

5

The opIds variable is a set of IDs, and it sounds like you're passing in the string 10-Ber1 as an ID, when in reality it's a text value.

You need to change Set<Id> opIds = new Set<Id>(); to Set<string> opIds = new Set<string>();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.