Pandora - Sample - Create Company Hierarchy
From SmartWiki
The following Pandora script can be used to create a company hierarchy.
Steps Required
- Import or Autoload the companies into a company container.
- Add the script to a static HTML page in a SmartFolder, including a button to trigger the script
The script works as follows:
- For each company found within the CompanyContainer, move the company under the parent company (match parent company name in a custom field on the autoloaded company)
- A maximum of 200 companies will be moved. For more click the button multiple times.
- The rootCompany must not be within the companyContainer so that the companies are moved out as the script runs.
function goMove(){ ss_developer_key="12345sdgagfi+Cf8Kwg3eA="; var parentCompanyName="cf_555555"; //fieldid of the company custom field with the name of the parent company for each company. var companyContainer=605040; //[[companyid]] of the company containing the imported/autoloaded companies to be moved into a hierarchy. var rootCompany=501000; //[[companyid]] of the container the companies should be placed into. var crs=company_find(false,"","name,catlist,parentlist,companyid,"+parentCompanyName," parentlist like '%,"+companyContainer+",%'","",0,200,""); for(var i=1;i<=crs.recordcount;i++){ var cpname=crs.getfieldbyname(i,'name'); var pname=crs.getfieldbyname(i,parentCompanyName); if(pname==""){ //alert("No parent so move "+cpname+" to root"); moveCompany(crs.getfieldbyname(i,'companyid'),rootCompany); }else{ var ncp=company_find(false,"","name,catlist,parentlist,companyid,"+parentCompanyName," name='"+pname+"'","",0,200,""); if(ncp.recordcount==1){ //alert("Parent is "+crs.getfieldbyname(i,parentCompanyName)+" so move "+cpname+" to "+ncp.getfieldbyname(1,'name')); moveCompany(crs.getfieldbyname(i,'companyid'),ncp.getfieldbyname(1,'companyid')); }else alert("Error: "+ncp.recordcount+" companies match parent company name "+pname); } } } function moveCompany(coid,pcoid){ ss_developer_key="12345sdgagfi+Cf8Kwg3eA="; var companyCategoryAdd=1940; //company category to be added to companies ss_developer_key="12345sdgagfi+Cf8Kwg3eA="; var rset=new ssRecordSet(); rset.addfieldnames("catlist,companyid,pcompany"); var idx=rset.getnewrecnumber(); rset.addfieldvaluebyname(idx,'companyid',coid); rset.addfieldvaluebyname(idx,'pcompany',pcoid); rset.addfieldvaluebyname(idx,"catlist",","+companyCategoryAdd+","); var rsc = company_update(false,'',rset); var rsc2=category_update(false,"",rset); }
Move contact under parent company field (match company name in a custom field on the contact to the company)
function goMove(){ ss_developer_key="12345sdgagfi+Cf8Kwg3eA="; var userContainer=407080; //companyid of the company the users to be moved are within. var parentCompanyName="cf_1340001" //id of contact custom field with the name of the parent company. var contactrs = contact_find(false,'',"firstname,lastname,userid,companyid,"+parentCompanyName,"companyid='"+userContainer+"'",'',0,200,''); for(var j=1;j<=contactrs.recordcount;j++){ var pname=contactrs.getfieldbyname(j,parentCompanyName); var crs=company_find(false,"","name,companyid"," name='"+pname+"'","",0,2,""); if((crs) && crs.recordcount==1) moveUser(contactrs.getfieldbyname(j,'userid'),crs.getfieldbyname(1,'companyid')); else alert("Cannot move user "+contactrs.getfieldbyname(j,'firstname')+" "+contactrs.getfieldbyname(j,'lastname')+". Company "+pname+" not found.") } } function moveUser(uid,pcoid){ ss_developer_key="12345sdgagfi+Cf8Kwg3eA="; var rset=new ssRecordSet(); var idx=rset.getnewrecnumber(); rset.addfieldnames("userid,companyid,isexternal"); rset.addfieldvaluebyname(idx,'userid',uid); rset.addfieldvaluebyname(idx,'companyid',pcoid); rset.addfieldvaluebyname(idx,'isexternal','1'); var rsc = contact_update(false,'',rset); }