Formula, am I missing something?

Hi

I’m trying to write a generic piece of formula that can be put in a smart icon and re-used on any old database (ND6 client naturally).

Anyway, I’m not sure if I’m missing something on how to get the value of a field if the field name is a variable. I’ve tried @GetField but that’s only good if the list contains only one value, kinda useless when the whole idea is to work with lists.

Anyway here’s my code & I know it’s got problems !

tOptionList := “Overwrite List”: “Replace String in list”: “Add to end of List”;

tChoice := @Prompt([OkCancelList]: [NoSort]; “Field Changer”; “What type of field change would you like to make?”; “”; tOptionList);

@If(tChoice = “”; @Return(“”); “”);

tFieldName := @Prompt([OkCancelEdit]; “Field Name”; “Please enter the field name.”; “”);

@If(tFieldName = “”; @Return(@Prompt([Ok]; “Error”; “Please enter a field name”)); “”);

@If(

tChoice= “Overwrite List”;

@Do(

@If(@IsNull(@GetField(tFieldName)); continue := @Prompt([YesNo]; “New Field”; “Do you want to add the field " + tFieldName + " to the document”); continue := @True);

@If(continue; “”; @Return(“”));

tValue := @Prompt([OkCancelEdit]; “Field Value”; "Please enter the value for field " + tFieldName + “.”; “”);

@SetField(tFieldName; tValue )

);

tChoice= “Replace String in list”;

@Do(

@If(@IsNull(@GetField(tFieldName)); @Return(@Prompt([Ok]; “Error”; “The field you have selected does NOT exit in the document. Please use the overwrite function to create a new field on the document”)); “”);

tList := tList;

tValueReplace := @Prompt([OkCancelEdit]; “Field Value”; “Please enter the string you would like replaced in the " + tFieldName + " list.”; “”);

tValue := @Prompt([OkCancelEdit]; “Field Value”; “Please enter the string you would as the replacement in the " + tFieldName + " field.”; “”);

@Set(tList; @GetField(tFieldName));

@Prompt([Ok]; “list check”; tList);

@If(!@Contains(tList; tValueReplace); @Return(@Prompt([Ok]; “Error”; tValueReplace + " does not exist in " + tFieldName)); “”);

@Replace(tList; tValueReplace; tValue );

@SetField(tFieldName; tList )

);

tChoice=“Add to end of List”;

@Do(

@If(@IsNull(@GetField(tFieldName)); @Return(@Prompt([Ok]; “Error”; “The field you have selected does NOT exit in the document. Please use the overwrite function to create a new field on the document”)); “”);

tValue := @Prompt([OkCancelEdit]; “Field Value”; "Please enter the string you would like appended to the " + tFieldName + “'s list.”; “”);

@SetField(tFieldName; @GetField(tList) );

@SetField(tFieldName; tList: tValue)

);

“”)

Any thoughts or advice greatly appreciated! Once it’s working without problems, I’m going to post it up here as a handy tool for developers when having to fiddle with lists.

TIA

Steve

Subject: RE: Formula, am I missing something???

Perhaps I’m misunderstanding what your problem is. It looks to me like you’re only asking for one fieldname. @GetField and @SetField work just fine when you supply one fieldname – even if the value you’re working with is a list.

If you did have a list of fieldnames, that’s what @For, @While and @Transform are for.

Also note: @IsNull of a field value doesn’t tell you whether the field exists in the document – it doesn’t distinguish between nonexistent fields and fields that exist but are blank. Use @DocFields instead. In fact, why not use @DocFields to present a list of choices? Here’s the toolbar formula I use:

choices := @DocFields;

fullChoices := @Transform(choices; “x”; x + " = " + @Implode(@Text(@GetField(x)); “;”));

fieldName := @Prompt([OkCancelEditCombo]; “Field Name”; “Enter Field Name.”; “”; FullChoices);

fieldName := @Left(fieldName; " = ");

oldValue := @GetField(fieldname);

newValue := @Explode(@Prompt([OkCancelEdit]; “New Field Value”; “Please enter new value (use ";" for multivalues – don’t put space after ;).”; @Implode(@Text(oldvalue); “;”)); “;”);

adjValue :=

@If(@IsNumber(oldValue); @TextToNumber(newValue);

@IsTime(oldValue); @TextToTime(newValue);

newValue);

@If(!@IsError(adjValue);

@SetField(fieldName ; adjValue);

@Prompt([YesNo]; “Change field value”; “New value not of same type as old value. Set field to text?”);

@SetField(fieldName ; newValue);

“”

)

Your logic is a little more complex than mine, but if the user says they want to add a new field you could prompt them for the name separately.

Subject: RE: Formula, am I missing something???

Andre

Thank you for your time, I’ll put together your suggestions & my code and post the outcome.

Much appreciated

Steve

Subject: Formula, am I missing something???

Would @eval work ? The docs don’t say if @eval(VariableContainingFieldName) will return the value fo the field or an error.

Subject: The Finished Version

tOptionList := “Overwrite List”: “Replace String in List”: “Add to end of List”: “Add New Field”;tChoice := @Prompt([OkCancelList]: [NoSort]; “Field Changer”; “What type of field change would you like to make?”; “”; tOptionList);

@If(tChoice = “”; @Return(“”); “”);

@If(tChoice=“Add New Field”;

@Do(

tFieldName := @Prompt([OkCancelEdit]; “New Field Name”; “Please enter the new field name.”; “”);

@If(tFieldName = “”; @Return(@Prompt([Ok]; “Error”; “Please enter a field name”)); “”)

);

@Do(

tFieldchoices := @DocFields;

fullChoices := @Transform(tFieldChoices; “x”; x + " = " + @Implode(@Text(@GetField(x)); “;”));

FieldName := @Prompt([OkCancelList]; “Field Name”; “Please select the field.”; “”; fullChoices);

@If(FieldName = “”; @Return(@Prompt([Ok]; “Error”; “Please enter a field name”)); “”);

tFieldName := @Left(fieldName; " = ");

oldValue := @GetField(tFieldname)

));

@If(

tChoice= “Overwrite List”;

@Do(

newValue := @Prompt([OkCancelEdit]; “Field Value”; "Please enter the value for field " + tFieldName + “.”+ @NewLine + “Seperate multiple list values with a ";".”; “”);

adjValue := @If(@IsNumber(oldValue); @TextToNumber(newValue); @IsTime(oldValue);@TextToTime(newValue); newValue);

@If(!@IsError(adjValue);

@SetField(tFieldName ; adjValue);

@Prompt([YesNo]; “Change field value”; “New value not of same type as old value. Set field to text?”);

@SetField(tFieldName ; newValue);

“”

)

);

tChoice= “Replace String in List”;

@Do(

tValueReplace := @Prompt([OkCancelList]; “Field Value”; “Please select the string you would like replaced in " + tFieldName + " list.”; “”; @Explode(oldValue; “;”));

@If(tValueReplace=“”; @Return(“”); “”);

tValue := @Prompt([OkCancelEdit]; “Field Value”; “Please enter the string you would as the replacement in the " + tFieldName + " field.”; “”);

tList := @Explode(oldValue; “;”);

tList := @Replace(tList; tValueReplace; tValue );

@SetField(tFieldName; tList )

);

tChoice=“Add to end of List”;

@Do(

tValue := @Prompt([OkCancelEdit]; “Field Value”; "Please enter the string you would like appended to the " + tFieldName + “'s list.”; “”);

@SetField(tFieldName; @Explode(oldValue): tValue)

);

tChoice=“Add New Field”;

@Do(

@If(@Contains(@DocFields; tFieldName); @Prompt([Ok]; “Error”; “The field " + tFieldName + " already exists on the document, please use a different option to modify.”); “”);

tNewValue := @Explode(@Prompt([OkCancelEdit]; “New Field Value”; “Please enter new value (use ";" for multivalues – don’t put space after ;).”));

@SetField(tFieldName; tNewValue)

);

“”)