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
Vladimir BessonovVladimir Bessonov 

use enum to check status update

My code is below works to update the parent object status to what ever status all children have. 

How to implement the status change back if for example, someone put status ARRIVED erroneously? 

I was thinking about using enums. 
But I cannot assign the value to enums like 

public enum Status {NOT_SENT = 0 , SENT = 1, ARRIVED =2 , ACCEPTED =3 , AFTERWARRANTY =4}  
in if (part.Status_list__c != NewStatus ) I would substructed 1 went to 1 next status left

How to implement it in APEX?


public without sharing class UpdatePropSetStatus {
public UpdatePropSetStatus() {
}

public void updateSetStatus(ID PropPartID, ID PropID, String NewStatus) {

List<Propulsion_part__c> PartList = New List<Propulsion_part__c>();
PartList = [ SELECT ID, Status_list__c FROM Propulsion_part__c WHERE Propulsion__c =: PropID]; //
System.debug(PropID + ' ' + NewStatus);
if(PartList.isEmpty() ) {
System.debug( 'Part list of prop set shall not be empty: not normal') ;
return;
}
// add ENUM in case the status of part was changed back to SENT from ARRIVED for Example
for (Propulsion_part__c part : PartList)
{
if (part.Status_list__c != NewStatus )
{
System.debug( 'not all parts have the same status');// ApexPages.addMessage...
return;
} else { Propulsion__c PropSetToUpdate = [SELECT ID, Set_Status__c FROM Propulsion__c WHERE ID =: PropID];
PropSetToUpdate.Set_Status__c = NewStatus;
update PropSetToUpdate;
}

}
}
}