개발/Salesforce

Apex Error) List has no rows for assignment to SObject

daramii 2023. 11. 13. 21:58
SELECTλŠ” 일반적으둜 λ°°μ—΄/λͺ©λ‘μ„ λ°˜ν™˜ν•˜μ§€λ§Œ ν•΄λ‹Ή 문은 ν•œ ν–‰λ§Œ λ°˜ν™˜λœλ‹€κ³  κ°€μ •ν•œλ‹€. 
0행을 λ°˜ν™˜ν•  수 μžˆλŠ” WHERE 문을 μ‚¬μš©ν•˜λŠ” 경우 ν˜Ήμ€ μ‚¬μš©μž μ»€μŠ€ν…€ κ°μ²΄μ—μ„œ μ΄λŸ¬ν•œ 였λ₯˜κ°€ λ°œμƒν•  수 μžˆλ‹€.
 
쿼리 결과에 λŒ€ν•΄ null 검사λ₯Ό μ•„λž˜μ²˜λŸΌν•˜λ‹ˆ List has no rows for assignment to SObject μ—λŸ¬κ°€ λ°œμƒν–ˆλ‹€.
Player__c player = [SELECT Id from Player__c where Name = :username]; 
if (player != null)  p = player.Id;

μœ„μ— μ½”λ“œ 처럼 μ‚¬μš©μž 이름과 μΌμΉ˜ν•˜λŠ” Player__c λ ˆμ½”λ“œκ°€ 없을 경우 μœ„ μ½”λ“œλŠ” μ‹€νŒ¨ν•˜λ©°, μ‹€μ œλ‘œλŠ” null을 λ°˜ν™˜ν•˜μ§€ μ•ŠλŠ”λ‹€.
 
 

λ‹€μŒκ³Ό 같이 μˆ˜ν–‰ν•˜λŠ” 것이 더 μ•ˆμ „ν•˜λ‹€.
Player__c[] players = [SELECT Id from Player__c where Name = :username]; 
if (players.size() > 0) p = players[0].Id;