0

I have the following Apex method:

public static void createJourney(Id accountId) {
        
         // Step 1: get account record type
        Account accRecTypeName = [SELECT Id, RecordType.DeveloperName FROM Account WHERE Id = :accountId];
        
        if(accRecTypeName.RecordType?.DeveloperName == 'Patient_Account') {
            
            // Step 2: Create a new Patient Journey record
            List<Patient_Journey__c> patientJourney = new List<Patient_Journey__c>();
            
            patientJourney.add(
                new Patient_Journey__c(
                    Name = 'Test Patient Journey',
                    Patient__c = accRecTypeName.Id,
                    Status__c = 'Active'
                )
            );
            
            insert patientJourney;
            
            // Get ID of the newly inserted Patient Journey record
            for(Patient_Journey__c getPJID : patientJourney) {
                patientJourneyID = getPJID.Id; // Line 25
            }
          
            // Step 3: Read records from custom metadata type 'Journey Step'
            Map<String, Journey_Step__mdt> journeyStepMD = Journey_Step__mdt.getAll();
            List<Patient_Journey_Step__c> patientJourneySteps = new List<Patient_Journey_Step__c>();

            for (String getJSteps : journeyStepMD.keySet()) {
                
                // Step 4: Create a new Patient Journey Step records based on 'Journey Step'
                patientJourneySteps.add(
                    new Patient_Journey_Step__c(
                        Step_Name__c = journeyStepMD.get(getJSteps).Step_Name__c,
                        Order__c = journeyStepMD.get(getJSteps).Order__c,
                        Journey__c = patientJourneyID // Line 39
                    )
                );
            }
        }
    }

I want to get the Id of the newly created 'Patient_Journey__c' record. I get the following errors when saving the file:

  • Line 25: Variable does not exist: patientJourneyID
  • Line 39: Variable does not exist: patientJourneyID
2
  • Any specific reason to use list when inserting a single record?
    – Raul
    Aug 17 at 11:07
  • I am new to Apex and don't have a good understanding of when to use and not to use List. This is how I do normally
    – MShal
    Aug 17 at 11:09
2

Let's simplify this code a bit

// Step 1: get account record type
Account accRecTypeName = [SELECT Id, RecordType.DeveloperName FROM Account WHERE Id = :accountId];
    
if(accRecTypeName.RecordType?.DeveloperName != 'Patient_Account') {return;}

        
// Step 2: Create a new Patient Journey record
Patient_Journey__c> patientJourney = new Patient_Journey__c(
                Name = 'Test Patient Journey',
                Patient__c = accRecTypeName.Id,
                Status__c = 'Active'
            );
        );
        
insert patientJourney;
        
      
// Step 3: Read records from custom metadata type 'Journey Step'
Map<String, Journey_Step__mdt> journeyStepsByName = Journey_Step__mdt.getAll();
List<Patient_Journey_Step__c> patientJourneySteps = new List<Patient_Journey_Step__c>();

for (String jStepName : journeyStepsByName.keySet()) {
            
  // Step 4: Create a new Patient Journey Step records based on 'Journey Step'
  patientJourneySteps.add(
          new Patient_Journey_Step__c(
              Step_Name__c = journeyStepMD.get(jStepName).Step_Name__c,
              Order__c = journeyStepMD.get(jStepName).Order__c,
              Journey__c = patientJourney.Id 
                )
            );
}
insert patientJourneySteps; 

Pro tip -- list/set variables should be named as plurals - e.g. patientJourneys; maps should be named thingsBykey e.g. journeySytepsByName; for loop iteration variable should be named to align with its type; e.g. pj instead of getPJId. Good variable names make debugging easier

1

You need to instantiate the patientJourneyID as string or id like this.

Id patientJourneyID;
// Get ID of the newly inserted Patient Journey record
   for(Patient_Journey__c getPJID : patientJourney) {
       patientJourneyID = getPJID.Id; // Line 25
   }

3
  • I did this but it still shows me error in line 39.
    – MShal
    Aug 17 at 11:10
  • 1
    Id would need to declared outside of the loop for having a proper scoping
    – Raul
    Aug 17 at 11:13
  • 1
    you are right, updated the answer
    – Damecek
    Aug 17 at 11:27

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.