Last active
March 4, 2022 22:09
-
-
Save patrick711/9ff5b6e9da90bcc52e14803269be5dd7 to your computer and use it in GitHub Desktop.
Check incoming data for excessive string lengths and then insert/update.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************** | |
Check_Field_Lengths_InsertUpdate - Check incoming data for excessive string lengths and then insert/update. | |
Inputs | |
myCache - Data View Cache | |
mylist - List of DAC objects that corresponds to the myCache | |
Patrick Chen SPS Commerce | |
02-2022 | |
***************************/ | |
public void Check_Field_Lengths_InsertUpdate<T>(PXCache myCache, List<T> mylist) | |
{ | |
//I get the name of the DAC so that I can create a warning after a truncation. | |
var myName = (PXCacheNameAttribute)myCache.BqlTable.GetCustomAttributes(typeof(PXCacheNameAttribute), true)[0]; | |
foreach (var item in mylist) | |
{ | |
foreach (var FieldName in myCache.Fields) | |
{ | |
foreach (PXDBStringAttribute attr in myCache.GetAttributesReadonly(FieldName).Where(attr => attr is PXDBStringAttribute).Cast<PXDBStringAttribute>()) | |
{ | |
var Length = attr.Length; | |
if (Length <= 0) continue; | |
Object value = item.GetType().GetProperty(FieldName).GetValue(item); | |
if (value == null) continue; | |
if (value.ToString().Length > Length) | |
{ | |
item.GetType().GetProperty(FieldName).SetValue(item, value.ToString().Substring(0, Length)); | |
if (!string.IsNullOrWhiteSpace(value.ToString())) | |
{ | |
//Notify your users that a truncation happened. | |
} | |
} | |
} | |
} | |
myCache.Update(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment