{"id":867,"date":"2021-01-03T19:13:20","date_gmt":"2021-01-03T19:13:20","guid":{"rendered":"http:\/\/puneetkhosla.com\/?p=867"},"modified":"2021-01-03T19:13:22","modified_gmt":"2021-01-03T19:13:22","slug":"safe-navigation-operator","status":"publish","type":"post","link":"https:\/\/puneetkhosla.com\/?p=867","title":{"rendered":"Safe Navigation Operator"},"content":{"rendered":"\n<p>As a developer, we have often run into scenarios where we are doing some assignments, running parent object queries, etc. but forget to do a null check. As a result we get a NullPointer Exception.<\/p>\n\n\n\n<p>To avoid this, we have to explicitly do some checks, thus increasing our lines of code. <\/p>\n\n\n\n<p>With the <strong><span class=\"has-inline-color has-bright-blue-color\">Safe Navigation Operator<\/span><\/strong> (<strong><span class=\"has-inline-color has-bright-blue-color\">?.<\/span><\/strong>), Salesforce now gives the developers the option to do the null checks (in some scenarios) in a single line. This simplifies the readability and reduces the lines of code.<\/p>\n\n\n\n<p>In short a<strong><span class=\"has-inline-color has-bright-blue-color\">?.<\/span><\/strong>b \/\/ Evaluates to: a == null ? null : a.b<\/p>\n\n\n\n<p>Let&#8217;s go through a use case (really its just for explaining and thus a very simple case). I want to replace the &#8220;Case&#8221; Subject with the first letter of Account Name, else make it null. Just to explain the difference, I have queried only 2 case records.<\/p>\n\n\n\n<p>So to start, here is a small block of code<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default img-border\"><img fetchpriority=\"high\" decoding=\"async\" width=\"825\" height=\"228\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO1.png\" alt=\"\" class=\"wp-image-869\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO1.png 825w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO1-300x83.png 300w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO1-768x212.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/figure>\n\n\n\n<p>The moment I execute this I get a NullPointer Exception.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default img-border\"><img decoding=\"async\" width=\"392\" height=\"115\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO2.png\" alt=\"\" class=\"wp-image-871\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO2.png 392w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO2-300x88.png 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/figure>\n\n\n\n<p>If I check the debug logs, I see the error is on the line containing the cs.Account.Name.left(1)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default img-border\"><img decoding=\"async\" width=\"580\" height=\"62\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO4.png\" alt=\"\" class=\"wp-image-876\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO4.png 580w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO4-300x32.png 300w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/figure>\n\n\n\n<p>Even though I queried Account.Name, it gives an exception as one of the case records did not have Account lookup populated.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default img-border\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO3-1-1024x68.png\" alt=\"\" class=\"wp-image-874\" width=\"593\" height=\"39\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO3-1-1024x68.png 1024w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO3-1-300x20.png 300w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO3-1-768x51.png 768w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO3-1-1200x80.png 1200w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO3-1.png 1512w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/figure>\n\n\n\n<p>A few ways to avoid this can be <br\/>\n<ul>\n<li>Do null check before assignment<\/li>\n<li>Assign a string variable through a ternary operator<\/li>\n<li>Check using String.IsNotBlank (as Account Name is string)<\/li>\n<\/ul><\/p>\n\n\n\n<p>Adding a <strong><span class=\"has-inline-color has-bright-blue-color\">String.IsNotBlank<\/span><\/strong> check, would not cause an error, but would require additional code in <strong><span class=\"has-inline-color has-bright-blue-color\">else<\/span><\/strong>, to handle the null record.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default img-border\"><img loading=\"lazy\" decoding=\"async\" width=\"824\" height=\"255\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO5.png\" alt=\"\" class=\"wp-image-877\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO5.png 824w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO5-300x93.png 300w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO5-768x238.png 768w\" sizes=\"(max-width: 824px) 100vw, 824px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default img-border\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO6-1024x42.png\" alt=\"\" class=\"wp-image-878\" width=\"631\" height=\"25\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO6-1024x42.png 1024w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO6-300x12.png 300w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO6-768x32.png 768w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO6.png 1136w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/figure>\n\n\n\n<p>Now lets add the <strong><span class=\"has-inline-color has-bright-blue-color\">Safe Navigation Operator<\/span><\/strong> to see the result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default img-border\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"235\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO7.png\" alt=\"\" class=\"wp-image-880\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO7.png 837w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO7-300x84.png 300w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO7-768x216.png 768w\" sizes=\"(max-width: 837px) 100vw, 837px\" \/><\/figure>\n\n\n\n<p>The code works fine and the end result is as follows :-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default img-border\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO8-1024x69.png\" alt=\"\" class=\"wp-image-881\" width=\"825\" height=\"55\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO8-1024x69.png 1024w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO8-300x20.png 300w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO8-768x52.png 768w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO8-1200x81.png 1200w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO8.png 1315w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/figure>\n\n\n\n<p>We could have also added <strong><span class=\"has-inline-color has-bright-blue-color\">?.<\/span><\/strong> in front of Account.Name and that would have worked the same way.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default img-border\"><img loading=\"lazy\" decoding=\"async\" width=\"833\" height=\"241\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO9.png\" alt=\"\" class=\"wp-image-882\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO9.png 833w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO9-300x87.png 300w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO9-768x222.png 768w\" sizes=\"(max-width: 833px) 100vw, 833px\" \/><\/figure>\n\n\n\n<p>Another example of using Safe Navigation Operator is as follows :-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default img-border\"><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"260\" src=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO10.png\" alt=\"\" class=\"wp-image-883\" srcset=\"https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO10.png 703w, https:\/\/puneetkhosla.com\/wp-content\/uploads\/2021\/01\/SNO10-300x111.png 300w\" sizes=\"(max-width: 703px) 100vw, 703px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>As a developer, we have often run into scenarios where we are doing some assignments, running parent object queries, etc. but forget to do a null check. As a result we get a NullPointer Exception. To avoid this, we have to explicitly do some checks, thus increasing our lines of code. With the Safe Navigation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[20,16],"class_list":["post-867","post","type-post","status-publish","format-standard","hentry","category-apex","tag-apex","tag-salesforce"],"_links":{"self":[{"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=\/wp\/v2\/posts\/867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=867"}],"version-history":[{"count":9,"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=\/wp\/v2\/posts\/867\/revisions"}],"predecessor-version":[{"id":887,"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=\/wp\/v2\/posts\/867\/revisions\/887"}],"wp:attachment":[{"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/puneetkhosla.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}