So I've been doing some research into this but have gotten conflicting outputs on whether or not it is possible to turn fields on and off using python in custom tools. I am working on a custom tool that generates KMZs on a state by state level for different departments within the company, but we do not let them see all the attributes within each feature class, thus turning some fields off before we use Map to KML tool. Since I am currently trying to automate this process, I want the custom tool to first turn the fields I do not want shown in the report off before the tool gets to the actual execution in regards to generating the KMZs. One such discussion I looked at was this on titled Turn fields off by script. While Wayne's solution seemed to work for the OP, there were parts of that solution I did not understand. Is there an easier way to approach this or if someone can help break down Wayne's solution, that would be appreciated. I look forward to seeing the ideas everyone has for this.
For reference, here's what the solution to the other discussion:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>addOutputsToMap <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'CURRENT'</SPAN><SPAN class="punctuation token">)</SPAN>
df <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrames<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'*'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
LayerNeedsFieldsTurnedOff <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'your target layer name'</SPAN><SPAN class="punctuation token">,</SPAN> df<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># fill in your desired fields to remain visible </SPAN>
desiredFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'fieldname0'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'fieldname1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'fieldname2'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'etc'</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>LayerNeedsFieldsTurnedOff<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>fieldInfo
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<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>i<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> desiredFields<SPAN class="punctuation token">:</SPAN>
field_info<SPAN class="punctuation token">.</SPAN>setvisible<SPAN class="punctuation token">(</SPAN>i<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>LayerNeedsFieldsTurnedOff<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'temp_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>
refLyr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>Layer<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'temp_layer'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># rename the ref layer the same as your target layer</SPAN>
refLyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">=</SPAN> <SPAN class="string token">'your target layer name'</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ApplySymbologyFromLayer_management<SPAN class="punctuation token">(</SPAN>refLyr<SPAN class="punctuation token">,</SPAN> LayerNeedsFieldsTurnedOff<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>UpdateLayer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> LayerNeedsFieldsTurnedOff<SPAN class="punctuation token">,</SPAN> refLyr<SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'cleaning up-'</SPAN>
<SPAN class="keyword token">if</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Exists<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'temp_layer'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'\'temp_layer\' still in memory...deleting now...'</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Delete_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'temp_layer'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'deleting obj refs...'</SPAN>
<SPAN class="keyword token">del</SPAN> mxd<SPAN class="punctuation token">,</SPAN>LayerNeedsFieldsTurnedOff<SPAN class="punctuation token">,</SPAN> refLyr <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'done.'</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>