0

Trying to get a list of cases that are equal to the cases in the variable caseInputs but i get three errors.

1.Invalid bind expression type of Integer for column of type String->its probably regarding my sql query? 2. Method does not exist or incorrect signature: void containsKey(Integer) from the type Map<String,String> 3. Method does not exist or incorrect signature: void get(Integer) from the type Map<String,String>

Never worked with maps but i guess its because ive an integer variable but iam mapping an integer and string variable?

public with sharing class Cases {

  public class CaseOutput {
    @InvocableVariable(required=true)
    public String sCaseStatus;
  }

  public class CaseInput {
    @InvocableVariable(required=true)
    public Integer sCaseNumber;
  }

  @InvocableMethod(label='Get Case Status')
  public static List<CaseOutput> getCaseStatus(List<CaseInput> caseInputs) {
    Set<Integer> caseNumbers = new Set<Integer>(); 

    // Get the case numbers from the input
    for (CaseInput caseInput : caseInputs) {
      caseNumbers.add(caseInput.sCaseNumber);
    }

    // Get the case objects from the set of order numbers
    List<Case> cases = 
      [SELECT casenumber,subject, status FROM Case WHERE casenumber in :caseNumbers];
      
    // Create a map of case numbers and case status values
    Map<String, String> mapNameStatus = new Map<String, String>(); 
    if (cases.size() > 0) {
        for (Case oneCase : cases) {
          mapNameStatus.put(oneCase.Subject, oneCase.Status);
        }
    }
    
    // Build a list of case status values for the output
    List<CaseOutput> caseOutputs = new List<CaseOutput>();
    for (CaseInput caseInput : caseInputs) {
      CaseOutput caseOutput = new CaseOutput();
      
      // Do we have a status for this case number?
      if (mapNameStatus.containsKey(caseInput.sCaseNumber)) {
          // If so, then add the status
          caseOutput.sCaseStatus = mapNameStatus.get(caseInput.sCaseNumber);
      } else {
          // If not, then add an unknown status value
          caseOutput.sCaseStatus = 'Case not found';
      }
      caseOutputs.add(caseOutput);
    }

    return caseOutputs;    
  }
}

0

It is because you are checking String inside the set of Integers. Replace all the "Integer" with "String" and it should resolve those errors.

2
  • Isnt CaseNumber a Number in salesforce? Should i cast it to text in the code above?
    – mazdan
    Feb 2 at 8:28
  • Thats so weird yesterday it didnt found cases but today its working thanks anyway.
    – mazdan
    Feb 2 at 8:35
0

Your comment and code don't agree here:

// Create a map of case numbers and case status values
Map<String, String> mapNameStatus = new Map<String, String>(); 
if (cases.size() > 0) {
    for (Case oneCase : cases) {
      mapNameStatus.put(oneCase.Subject, oneCase.Status);
    }
}

I think you want (changing the map key type as well

// Create a map of case numbers and case status values
Map<Integer, String> mapNameStatus = new Map<Integer, String>(); 
if (cases.size() > 0) {
    for (Case oneCase : cases) {
      mapNameStatus.put(oneCase.CaseNumber, oneCase.Status);
    }
}

This can be rewritten to be clearer (if list is empty, loop will do nothing)

// Create a map of case numbers and case status values
Map<Integer, String> statusByCaseNumber = new Map<Integer, String>(); 
for (Case case : cases) {
      statusByCaseNumber.put(case.CaseNumber, case.Status);
    }
}

Maps should be named foosByBar where foos describe the map's value and Bar describes the map's key. This notation makes it so much easier to understand what a map contains

In addition, since Apex is a by reference system, a better map (more extensible) would be:

// Pivot cases by casenumber
Map<Integer, Case> casesByCaseNumber = new Map<Integer, Case>(); 
for (Case case : cases) {
      casesByCaseNumber.put(case.CaseNumber, case);
    }
}
0

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.