So I'm working on a custom tool that generates KMZs for reports to go out ot various different departments. One thing that we do is turn certain fields off not to show every attribute in the table. Thus, I settled on using MakeFeatureLayer_management tool to create a new feature layer with only the fields I want shown in the report. Here is what my code looks like:
visibleFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Field1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Field2"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Field3"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Field4"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Field4"</SPAN><SPAN class="punctuation token">]</SPAN>
field_info <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Input Feature Layer'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>fieldInfo
<SPAN class="keyword token">for</SPAN> index <SPAN class="keyword token">in</SPAN> xrange<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> field_info<SPAN class="punctuation token">.</SPAN>count<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> field_info<SPAN class="punctuation token">.</SPAN>getfieldname<SPAN class="punctuation token">(</SPAN>index<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> visibleFields<SPAN class="punctuation token">:</SPAN>
field_info<SPAN class="punctuation token">.</SPAN>setvisible<SPAN class="punctuation token">(</SPAN>index<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"HIDDEN"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Input Feature Layer'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Output Feature Layer'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>field_info<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>RefreshTOC<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>RefreshActiveView<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
When I run this in the python module in ArcMap, the output feature layer is automatically added to the Table of Contents. But when I run this in my custom tool, the output feature layer isn't added to the Table of Contents and my custom tool spits out an error because the tool cannot progress as the output feature layer does not exist in the Table of Contents and cannot be referred on. So how would I be able to add the newly created feature layer to the table of contents in the script when the feature layer is created in program memory?