{"id":2575,"date":"2013-04-08T01:34:21","date_gmt":"2013-04-07T20:34:21","guid":{"rendered":"http:\/\/www.chiragmehta.info\/chirag\/?p=2575"},"modified":"2013-04-08T16:25:54","modified_gmt":"2013-04-08T11:25:54","slug":"javascript-jquery-code-to-fetch-salesforce-lead-convert-custom-field-mappings-in-terms-of-api-names","status":"publish","type":"post","link":"https:\/\/www.chiragmehta.info\/chirag\/2013\/04\/08\/javascript-jquery-code-to-fetch-salesforce-lead-convert-custom-field-mappings-in-terms-of-api-names\/","title":{"rendered":"Javascript (jQuery) code to fetch Salesforce Lead Convert Custom Field Mappings (in terms of API Names)"},"content":{"rendered":"<p>Yesterday came across a requirement to list down Lead Convert Custom Field Mappings in terms of API Names ie Lead Field1__c maps to Account\/Contact\/Opportunity Field1__c. Salesforce UI doesn&#8217;t give that option to extract same. Neither any metadata API got that, really strange that there&#8217;s no way I can extract Lead Convert Custom Field Mappings (in terms of API Names). Is there any? please help in case I&#8217;m missing some standard way of extracting that out?<\/p>\n<p>Easiest option could have been that I sit down and do manual and tedious way of reading each field label finding its API Name and record the same in excel, but this doesn&#8217;t seem viable option for an org with 100+ lead convert field mappings. As I couldn&#8217;t find one using all standard approaches, I started thinking of wicked ways of achieving it. And as usual Javascript (jQuery) comes to rescue party!<\/p>\n<p>Login to Salesforce Org in Chrome or FF Browser,<br \/>\nNavigate to lead convert field mappings page (Select Your Name | Setup | Customize | Leads | Fields | Map Lead Fields),<br \/>\nOpen Javascript Console,<br \/>\nNow load jQuery first (you can do same by loading any existing jQuery booklet or userscript too) by executing below code in JS Console.<\/p>\n<blockquote><p><code><br \/>\nvar jq = document.createElement('script');<br \/>\njq.src = \"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.8.2\/jquery.min.js\";<br \/>\ndocument.getElementsByTagName('head')[0].appendChild(jq);<br \/>\n$j=jQuery.noConflict();<\/code><\/p><\/blockquote>\n<p>Now comes the main code which will output LeadFieldAPIName, Account\/Contact\/Opportunity Field APIName, Account or Contact or Opportunity. Execute below code too in JS Console.<\/p>\n<blockquote><p>\/\/Traverse across all Select elements<br \/>\n<code>$j(\"select\").each(function () {<\/code><\/p>\n<p>\/\/Consider only the ones with a mapping selected<br \/>\n<code>if (this.value != '') {<\/code><\/p>\n<p>\/\/Fetch Object Name to which this particular Lead field is mapped, whether its Account or Contact or Opportunity<br \/>\n<code>var objectname = $(this).find('option:selected').text().split(\".\")[0];<\/code><\/p>\n<p>\/\/Tricky Part : As this lead convert field mappings page doesn&#8217;t store Lead field API Names, it only contains lead field ID (15 digit SFDC ID) so we will have to do AJAX request of LEAD field detail webpage to fetch LEAD API Name corresponding to lead field ID<br \/>\n<code>var contentText = $.ajax({url: \"\/\" + this.name,async: false}).responseText;<\/code><\/p>\n<p>\/\/In fetched content, search and extract field API Name<br \/>\n<code>var fromfield = $(contentText).find('td').filter(function () {<br \/>\nreturn $(this).text().indexOf('API Name') === 0;<br \/>\n}).closest('td').next().html();<\/code><\/p>\n<p>\/\/Similarly AJAX request for Account\/Contact\/Opportunity field detail page to fetch respective field API Name<br \/>\n<code>contentText = $.ajax({url: \"\/\" + this.value,async: false}).responseText;<\/code><\/p>\n<p>\/\/In fetched content, search and extract field API Name<br \/>\n<code>var tofield = $(contentText).find('td').filter(function () {<br \/>\nreturn $(this).text().indexOf('API Name') === 0;<br \/>\n}).closest('td').next().html();<\/code><\/p>\n<p>\/\/Output LeadAPIName, Account\/Contact\/Opportunity APIName, Account or Contact or Opportunity<br \/>\n<code>console.log(fromfield + ',' + tofield + ',' + objectname);<\/code><\/p>\n<p><code>}});<\/code><\/p><\/blockquote>\n<p>Please Note:<\/p>\n<ul>\n<li>This isn&#8217;t most neat and recommended way, so if you come across\u00a0something\u00a0really clean and less network consuming solution please share your solutions in comments below<\/li>\n<li>I&#8217;m not an expert in jQuery, so there might be obvious and recommended ways of coding jQuery and my above coding might sound nascent way of handling jQuery, so please ignore my lack of knowledge around jQuery and help me to optimize same.<\/li>\n<li>Fetching Lead\/Account\/.. Field API Name from Field ID might be done using other ways too (describe API Calls?), but thought of doing it in just one script, so did that using AJAX calls and parsing retrieved webpage content<\/li>\n<li>As always, feedback\/suggestions are always welcome<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday came across a requirement to list down Lead Convert Custom Field Mappings in terms of API Names ie Lead Field1__c maps to Account\/Contact\/Opportunity Field1__c. Salesforce UI doesn&#8217;t give that option to extract same. Neither any metadata API got that, really strange that there&#8217;s no way I can extract Lead Convert Custom Field Mappings (in [&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":[151,150,147,149,146,148,143,144,145,169],"class_list":["post-2575","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-ajax","tag-api-names","tag-chrome","tag-console","tag-field-mappings","tag-firefox","tag-jquery","tag-js","tag-lead-convert","tag-salesforce","post-preview"],"_links":{"self":[{"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/posts\/2575","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=2575"}],"version-history":[{"count":31,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/posts\/2575\/revisions"}],"predecessor-version":[{"id":2598,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/posts\/2575\/revisions\/2598"}],"wp:attachment":[{"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/media?parent=2575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/categories?post=2575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chiragmehta.info\/chirag\/wp-json\/wp\/v2\/tags?post=2575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}