Whilst this is fine for UIType 15 and 16 picklists - it doesn't work for muti-select picklists. I think the same bug is present in the remove() method too. If you have a multi-select picklist and the field contains more than one value then the query is wrong because the column will contain something like:
$query='SELECT tablename, fieldid, columnname, uitype FROM vtiger_field WHERE fieldname=? and presence IN (0,2)';$result=$db->pquery($query,array($pickListFieldName));$num_rows=$db->num_rows($result);//As older look utf8 characters are pushed as html-entities,and in new utf8 characters are pushed to database//so we are checking for both the values$primaryKey=Vtiger_Util_Helper::getPickListId($pickListFieldName);if(!empty($color)){$query='UPDATE '.$this->getPickListTableName($pickListFieldName).' SET '.$pickListFieldName.'= ?, color = ? WHERE '.$primaryKey.' = ?';$db->pquery($query,array($newValue,$color,$id));}else{$query='UPDATE '.$this->getPickListTableName($pickListFieldName).' SET '.$pickListFieldName.'=? WHERE '.$primaryKey.' = ?';$db->pquery($query,array($newValue,$id));}for($i=0;$i<$num_rows;$i++){$row=$db->query_result_rowdata($result,$i);$tableName=$row['tablename'];$columnName=$row['columnname'];if($row['uitype']==33){$query='UPDATE '.$tableName.' SET '.$columnName.' = REPLACE('.$columnName.', ?, ?)';$db->pquery($query,array($oldValue,$newValue));}else{$query='UPDATE '.$tableName.' SET '.$columnName.'=? WHERE '.$columnName.'=?';$db->pquery($query,array($newValue,$oldValue));}}
I added the uitype to the initial query and then made use of MySQL's REPLACE() function which is like php's str_replace() so it only replaces a matching substring.
if($row['uitype']==33){foreach($pickListValuesas$oldValue){$query='UPDATE '.$tableName.' SET '.$columnName.' = REPLACE('.$columnName.', ?, ?)';$db->pquery($query,array($oldValue,$replaceValue));}}else{$query='UPDATE '.$tableName.' SET '.$columnName.'=? WHERE '.$columnName.' IN ('.generateQuestionMarks($pickListValues).')';$params=array($replaceValue);array_push($params,$pickListValues);$db->pquery($query,$params);}```
@lord_alan Right! updating the fieldvalue in related table will break if multiple values are selected. But renaming picklist values in picklist tables will be fine.
The suggestion above works fine and is fast. If you resort to using php str_replace/preg_replace and iterate over all rows that will be incredibly slow on large systems where there are hundreds of thousands or millions of rows of data (and we have some of these systems).