Skip to content

Instantly share code, notes, and snippets.

@patrick711
Last active March 4, 2022 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrick711/9ff5b6e9da90bcc52e14803269be5dd7 to your computer and use it in GitHub Desktop.
Save patrick711/9ff5b6e9da90bcc52e14803269be5dd7 to your computer and use it in GitHub Desktop.
Check incoming data for excessive string lengths and then insert/update.
/***************************
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