AnsweredAssumed Answered

check for duplicate name in javascript

Question asked by sharifu on Feb 7, 2014
Latest reply on Feb 11, 2014 by kaynezhang
I have the following javascript which i execute on a folder manually in /alfresco.

*   validates document metadata and moves to correct folder
*   @author sharifu

var thirdParties =["sp:thirdParties"];
var letter = "";
var location =["sp:location"];
var responsibleEmp =["sp:responsibleEmp"];
var contractRef =["sp:contractRef"];
var logNumber =["sp:logNumber"];
var effectiveDate =["sp:effectiveDate"];

var sourceFile = document;
var targetFolder = "";
var success = true;

//Check to see if values are null
if( thirdParties == null || location == "" || responsibleEmp == null || contractRef == null || logNumber == null || effectiveDate == null  || location.toString() != "UK" ){
   success = false;
   var effectiveDate = new Date (["sp:effectiveDate"] );
   var vendor = thirdParties.replace(/[|;$%@"<>()+,\/.]/g,"").replace(".","").replace(/^\s+|\s+$/g,'');
   //Check if folder exist
   if(checkFolderExists(location, thirdParties )){
      letter = thirdParties.charAt(0);
      targetFolder = space.parent.childByNamePath(location.toString()).childByNamePath("Contracts").childByNamePath(letter.toLowerCase()).childByNamePath(vendor);
      success = false; // do not move doc no folder exists

   var targetFile = targetFolder.childByNamePath(;
   if(targetFile == null){
      var targetFileWorkingCopy = targetFile.checkout();
      targetFileWorkingCopy.content = sourceFile.content;

/**   Function checks if vendor folder exists if not then it creates it
*   @param String site
*   @param String vendor
*   @return Boolean true if creates or exists, false all others
function checkFolderExists(site, thirdParties){
   var vendor = thirdParties.replace(/[|;$%@"<>()+,\/.]/g,"").replace(".","").replace(/^\s+|\s+$/g,'');
   var letter = vendor.charAt(0);
   if( space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ) == null ){
      space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").createFolder( letter.toLowerCase() );
      space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).createFolder( vendor );
      return true;
   }else if( !space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).childByNamePath( vendor) ){
      space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).createFolder( vendor );
      return true;
   }else if ( space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).childByNamePath( vendor) ) {
      return true;
      return false;


The problem I am having is if the same name is capitalized share does not recognize it as duplicate, however when you try to replicate the contents replication moans about duplicate nodes. The function i need to check whether it is already created is in <i>checkFolderExists</i>, i need a check on <i>!space.parent.childByNamePath(site.toString()).childByNamePath("Contracts").childByNamePath( letter.toLowerCase() ).childByNamePath( vendor)</i>. I have to make sure vendor exists only once and not in different capitalization.