{"id":445,"date":"2019-10-28T17:19:46","date_gmt":"2019-10-28T17:19:46","guid":{"rendered":"http:\/\/morecpq.com\/?p=445"},"modified":"2019-10-28T17:20:45","modified_gmt":"2019-10-28T17:20:45","slug":"apex-dont-allow-contact-deletion-contact-role","status":"publish","type":"post","link":"https:\/\/morecpq.com\/index.php\/2019\/10\/28\/apex-dont-allow-contact-deletion-contact-role\/","title":{"rendered":"APEX &#8211; Don&#8217;t Allow Contact Deletion if Contact is an Opportunity Contact Role."},"content":{"rendered":"\n<p>This is not CPQ but a short, quick, request from someone for some code.  The request is:  If a Contact is an Opportunity Contact Role of same Account, don&#8217;t allow deleting it.  Below is the code.  I didn&#8217;t think &#8220;of same Account&#8221; matters, so I didn&#8217;t write that into the code.<\/p>\n\n\n\n<p><strong>APEX Trigger &#8211; ContactTriggers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">trigger ContactTriggers on Contact (before delete) {<br>\n    ContactTriggerHelper.helpTrigger();<br>\n}<\/pre>\n\n\n\n<p><strong>APEX Class &#8211; ContactTriggerHelper<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ContactTriggerHelper {\n    public class ContactException extends Exception {}\n    public static void helpTrigger() {\n        \/\/ if is before,\n        if (trigger.isBefore) {\n             \/\/ if is delete,\n             if (trigger.isDelete) {\n                 \/\/ query contact roles for these contact ids.\n                 List&lt;OpportunityContactRole> crList = [select id from OpportunityContactRole where ContactId in :trigger.oldMap.keySet()];\n                 \/\/ if contact roles list is not empty,\n                 if (crList.size() > 0) {\n                     \/\/ throw error.\n                     throw new ContactException('Unable to delete Contact.  This Contact is listed as a Contact Role on an Opportunity.');\n                 }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is not CPQ but a short, quick, request from someone for some code. The request is: If a Contact is an Opportunity Contact Role of same Account, don&#8217;t allow deleting it. Below is the code. I didn&#8217;t think &#8220;of same Account&#8221; matters, so I didn&#8217;t write that into the code. APEX Trigger &#8211; ContactTriggers &hellip;<br \/><a href=\"https:\/\/morecpq.com\/index.php\/2019\/10\/28\/apex-dont-allow-contact-deletion-contact-role\/\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">APEX &#8211; Don&#8217;t Allow Contact Deletion if Contact is an Opportunity Contact Role.<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":2,"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/morecpq.com\/index.php\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}