문자열 값에서 동적으로 sObject 필드 참조

Oct 23 2020

문자열 값에서 동적으로 sObject의 필드 (이 예에서는 기회)를 참조 할 수 있습니까?

예를 들어, 다음 코드를 사용하십시오.

 for(Opportunity o: oppList) {
      for(String s : strList) {
           // I'd like to set the opportunity field values based on the string 
           // value that contains the API name of the opportunity field
      }
 }

따라서 변수 strList에는 기회 API 필드 이름이 포함됩니다. 업데이트 전 트리거에서 기회에 대한 일부 필드를 지우려고하지만 strList.

답변

2 sfdcfox Oct 23 2020 at 04:09

예, "get"및 "put"메서드를 사용하면 필드 이름으로 필드를 읽고 쓸 수 있습니다.

for(Opportunity record: records) {
  for(String fieldName: fieldNames) {
    Object value = record.get(fieldName); // Get a field value!
    record.put(fieldName, null); // Clear a field!
  }
}