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 

cast in loop

How I can cast types? 

See the code below. In comments, I mark what I need to code.
 
public class ObjectStatusUpdate {
    public ObjectStatusUpdate() {
        
    }

    public enum Status {NOT_SENT , SENT, ARRIVED, ACCEPTED, AFTERWARRANTY}
    Status ComponentState = Status.NOT_SENT;
    public void updateComponentStatus(ID PartID, String NewStatus, ID ComponentID ) {

        switch on NewStatus {
            when 'SENT' {
                ComponentState = Status.SENT;
            }
            when 'ARRIVED' {
                ComponentState = Status.ARRIVED;
            }
            when 'ACCEPTED/WARRANTY' {
                ComponentState = Status.ACCEPTED;
            }
            when 'AFTER WARRANTY' {
                ComponentState = Status.AFTERWARRANTY;
            }
            when else {
                ComponentState = Status.NOT_SENT;
            }
        }

                // Part type
                Schema.SObjectType PartType = PartID.getSObjectType();
                String ListPartType = 'List<' + PartType + '>';
                System.debug(PartType); // Propulsion_Part__c or Truck_Part__c
                // Comp type 
                Schema.SObjectType CompType = ComponentID.getSObjectType();
                String ListCompType = 'List<' + CompType + '>';
                System.debug(CompType); // Propulsion__c or Truck__c
                
                List<SObject> PartList = (List<SObject>)Type.forName(ListPartType).newInstance();
                String qryStringPartList = 'SELECT Id, Status__c FROM ' + String.valueOf(PartType) + ' WHERE ' + String.valueOf(CompType) + '=' + '\'' + ComponentID + '\'';  
                system.debug('***********************Build Query == ' + qryStringPartList);                  
                // SObject resultObject = Database.query(qryString);
                PartList = Database.query(qryStringPartList);

        if(PartList.isEmpty() ) { 
            System.debug( 'Part list of prop set shall not be empty: not normal') ; 
           return;
        }
        
        // How to cast part to <PartType>?  Here I can an error that Status__c does not exists
        for (SObject part : PartList) {

        if (part.Status__c != NewStatus && NewStatus != 'NOT SENT')
            {
                Status tempStatus;
                switch on part.Status__c {
                    when 'SENT' {
                        tempStatus = Status.SENT;
                    }
                    when 'ARRIVED' {
                        tempStatus = Status.ARRIVED;
                    }
                    when 'ACCEPTED/WARRANTY' {
                        tempStatus = Status.ACCEPTED;
                    }
                    when 'AFTER WARRANTY' {
                        tempStatus = Status.AFTERWARRANTY;
                    }
                    when else {
                        tempStatus = Status.NOT_SENT;
                    }
                }


            if (TempStatus.ordinal() < ComponentState.ordinal()) {
                ComponentState = TempStatus;
            } 

            switch on ComponentState {
                when SENT {
                    NewStatus = 'SENT';
                }
                when ARRIVED {
                    NewStatus = 'ARRIVED';
                }
                when ACCEPTED {
                    NewStatus = 'ACCEPTED/WARRANTY';
                }
                when AFTERWARRANTY {
                    NewStatus = 'AFTER WARRANTY';
                }
                when else {
                    NewStatus = 'NOT SENT';
                }
            }

// Here again I need to replace Propusion__c to CompType . How? 
            Propulsion__c ComponentToUpdate = [SELECT ID, Status__c FROM Propulsion__c WHERE ID =: ComponentID];
            ComponentToUpdate.Status__c = NewStatus;
                update ComponentToUpdate;

            return;
            } else {            
// replace Propulsion__c to PartType. How ?
Propulsion__c ComponentToUpdate = [SELECT ID, Status__c FROM Propulsion__c WHERE ID =: ComponentID];
                ComponentToUpdate.Status__c = NewStatus;
                update ComponentToUpdate; 
            }


        }
    }

}

 
Abhishek BansalAbhishek Bansal
While working with sObject you should use get('fieldName') method, so instead of using part.Status__c you should use part.get('Status__c')

Regaring the creation of new sObject type you can use this link http://santanuboral.blogspot.com/2019/03/efficient-way-of-dynamically-casting.html