{"id":1672,"date":"2009-09-11T00:51:02","date_gmt":"2009-09-10T19:51:02","guid":{"rendered":"http:\/\/www.chiragmehta.info\/chirag\/?p=1672"},"modified":"2009-09-11T00:58:55","modified_gmt":"2009-09-10T19:58:55","slug":"apex-update-non-setup-objects-from-a-setup-object-like-user","status":"publish","type":"post","link":"https:\/\/www.chiragmehta.info\/chirag\/2009\/09\/11\/apex-update-non-setup-objects-from-a-setup-object-like-user\/","title":{"rendered":"Apex &#8211; Update &#8220;non-setup&#8221; objects from a &#8220;setup&#8221; object like User"},"content":{"rendered":"<p><strong>Dilemma<\/strong><\/p>\n<p>As\u00a0<a style=\"outline-width: 0px; outline-style: initial; outline-color: initial; color: #236bad; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_dml_non_mix_sobjects.htm\">this page of the Apex docs<\/a> indicates, you can&#8217;t just make a User trigger that updates a Contact or an Account, because it&#8217;s forbidden to modify those &#8220;non-setup&#8221; objects from a &#8220;setup&#8221; object like User.<\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>Fortunately there is a simple solution:\u00a0<a style=\"outline-width: 0px; outline-style: initial; outline-color: initial; color: #236bad; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_classes_annotation.htm#future_topic\">the @future annotation<\/a>.\u00a0 The @future annotation allows you to create Apex that runs asynchronously at some point in the future (in my tests I&#8217;ve found that it runs immediately, or at least very soon after the trigger executes).\u00a0 Methods that use @future are subject to different limits than normal trigger operations because @future methods don&#8217;t hold up the trigger (and therefore the entire user experience) while they&#8217;re working.\u00a0 Therefore @future methods are often used to perform long-running web service callouts from triggers asynchronously.\u00a0 However, they can also be handy for a case like ours, where we want to update an object that we&#8217;re not normally eligible to update.<\/p>\n<p>Calling a method that has been marked as @future is just like calling any other static method.\u00a0 Here&#8217;s my trigger, short and sweet:<\/p>\n<blockquote>\n<pre style=\"outline-width: 0px; outline-style: initial; outline-color: initial; padding: 0px; margin: 0px; border: 0px initial initial;\">trigger UpdateContactFromPortalUser on User (after update) {\r\n\t\/\/We only want to run on the single item that the user edited\r\n\tif (Trigger.new.size()==1) {\r\n\t\tUser u =\u00a0 Trigger.new[0];\r\n\t\t\/\/And only if it's a portal user\r\n\t\tif (u.ContactId!=null) {\r\n\t\tUpdateContactFromPortalUser.updateContacts(u.Id);\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<\/blockquote>\n<p>Let&#8217;s see how that works.\u00a0 In the example given here, I just update a couple of fields from the user record: the name, the email address, and the title.<\/p>\n<blockquote>\n<pre style=\"outline-width: 0px; outline-style: initial; outline-color: initial; padding: 0px; margin: 0px; border: 0px initial initial;\">global class UpdateContactFromPortalUser {\r\n\u00a0 \u00a0 @future\r\n\u00a0 \u00a0 public static void updateContacts(String userId) {\r\n\u00a0 \u00a0 \tUser u = [select ContactId,Email,FirstName,LastName,Title\r\n\u00a0 \u00a0 \t\t\t\tfrom User\r\n\u00a0 \u00a0 \t\t\t\twhere Id=:userId];\r\n\r\n\u00a0 \u00a0 \tif (u!=null &amp;&amp; u.ContactId!=null) {\r\n\t\u00a0 \u00a0 \tContact c = new Contact(Id=u.ContactId);\r\n\r\n\t\t\tc.Email = u.Email;\r\n\t\t\tc.FirstName=u.FirstName;\r\n\t\t\tc.LastName=u.LastName;\r\n\t\t\tc.Title=u.Title;\r\n\t\t\tupdate c;\r\n\t\t}<\/pre>\n<pre style=\"outline-width: 0px; outline-style: initial; outline-color: initial; padding: 0px; margin: 0px; border: 0px initial initial;\"><\/pre>\n<\/blockquote>\n<p>Source : \u00a0<a href=\"http:\/\/blogs.salesforce.com\/support\/2009\/01\/index.html\">http:\/\/blogs.salesforce.com\/support\/2009\/01\/index.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dilemma As\u00a0this page of the Apex docs indicates, you can&#8217;t just make a User trigger that updates a Contact or an Account, because it&#8217;s forbidden to modify those &#8220;non-setup&#8221; objects from a &#8220;setup&#8221; object like User. Solution Fortunately there is a simple solution:\u00a0the @future annotation.\u00a0 The @future annotation allows you to create Apex that runs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[169],"class_list":["post-1672","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-salesforce","post-preview"],"_links":{"self":[{"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/posts\/1672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/comments?post=1672"}],"version-history":[{"count":11,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/posts\/1672\/revisions"}],"predecessor-version":[{"id":1683,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/posts\/1672\/revisions\/1683"}],"wp:attachment":[{"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/media?parent=1672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/categories?post=1672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/tags?post=1672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}