1

I am trying to update the field value in a record using a before trigger, but it was not updating as expected.

trigger opMetricsTrigger on Operational_Metrics__c (before insert,before update) 
{
    Operational_Metrics__c[] op = null;
    Set<string> opIds = new Set<string>();
    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 );
        }

        List<Operational_Metrics__c> opm = [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 ops : opm)
        {
            if(ops.Pile_Completion_Stage__c == 'yes')
            {
                ops.Unique_Id__c = ops.Unique_Pile_ID__c + '-Mir'; 
                System.debug('records1 are ' + ops);
            }  
            else
            {
                ops.Unique_Id__c=ops.Unique_Pile_ID__c + '-Mir'; 
                System.debug('records2 are ' + ops);
            }
        }
    }
}

Could anyone sort it?

  • Can you outline what you mean by 'it was not updating as expected'? What do you expect? What actually happens? What have you tried? What part of it isn't working? – Nick Cook Jul 1 '14 at 5:23
  • If your previous questions have been answered, please mark the answers accordingly. You are asking a lot of questions, you don't appear to be really trying anything yourself, and you're not acknowledging any of the work other people are doing for you. Please take the time to show your appreciation or sites like this will lose their effectiveness. – Nick Cook Jul 1 '14 at 5:24
  • i am trying to store the ops.Unique_Id__c value in the record. I am getting logs perfectly but i can see the ops.Unique_Id__c field as null after saving record. I have tried as the above trigger – Salesforce developer Jul 1 '14 at 5:28
  • done @NickCook thanks. could you help me in this issue – Salesforce developer Jul 1 '14 at 5:30
  • no problems. It's also a good idea to update the original question with any additional information that you supply in these comments. It makes it a lot easier for other people to help if all the information is in the one place. – Nick Cook Jul 1 '14 at 5:31
2

The main reason this isn't working is because you're constructing a new list of Operational_Metrics__c records but doing nothing with them. They're not part of the Trigger, so they're not being updated as part of the Trigger.

EDIT: adding an update opm like I had suggested won't work because it will create a recursive update.

You'll need to create a Map<string, Operational_Metrics__c> to collate related records, iterate within the Trigger, update records within the trigger, then update records not included in the trigger. (sorry, this has turned into a poor answer)

|improve this answer|||||
  • when i wrote update opm its throwing an error execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 2 with id a0dK000000aztaRIAQ; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a0dK000000aztaR) is currently in trigger opMetricsTrigger, therefore it cannot recursively update itself: []: Trigger.opMetricsTrigger: line 52, column 1 i.e at update opm . – Salesforce developer Jul 1 '14 at 5:42
  • Performing an Update for a same object within a before update trigger is not a good idea at all – highfive Jul 1 '14 at 5:50
0

I would either simplify your trigger to only update the current trigger context of records, OR I would write a simple workflow rule that populates the Unique_Id accordingly.

Here's why: any records that are inserted or updated to have a unique id != null / pile completion stage of yes will already by modified with a suffix of "-Mir". Your SOQL loop in this case is very much redundant.

If you're worried about old existing data that has not been updated with the "-Mir" suffix, then simply run an update against those records via data loader or execute anonymous, and you're simplified trigger or workflow rule will take care of those.

Here's an example of a simplified trigger that does not need an additional update DML call:

trigger opMetricsTrigger on Operational_Metrics__c (before insert,before update){
    for(Operational_Metrics__c e : Trigger.new){
        //You could also add some checking in here to see if Unique_Id__c or Pile_Completion_Stage__c has changed via an update.
        if(e.Unique_Pile_ID__c != null && !e.Unique_Pile_Id__c.endsWith('-Mir') && e.Pile_Completion_Stage__c=='yes')
            e.Unique_Pile_Id__c+='-Mir';
        }
    }
}
|improve this answer|||||

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.