Submitted by viking on 2022/11/06 17:31

I have a text field (popup list of items) "Ingredients"
I create a simple Auto-assign rule for "Ingredients": AME: #onion

When I change the Ingredients field the onion Tag is properly checked.

I then change the Auto-assign rule for "Ingredients" to use a function: AME: SetIngredientsTags([Ingredients])

with the function in the VB editor:
Function SetIngredientsTags(Ingredients)
    SetIngredientsTags="#onion"
end function

However, when I change the Ingredients field, I get an error.
IQ doesn't recognize SetIngredientsTags([Ingredients]) as a function. I believe that it thinks that it is a field?

How should I write the Auto-assign rule so that the function is recognized, executed, returns #onion and sets the onion Tag?

 

 

Comments

I am sorry, but I don't understand.
"Ingredients" is a field, and the Auto-assign rule works fine to set the Tag (when I use AME: #onion).

I think that my syntax is wrong because it doesn't recognize my function:  AME: SetIngredientsTags([Ingredients])

The function is just supposed to return the string #onion.

OK, got it. In the next version:

  • New: Field auto-assign can now assign tags using a VBScript function. Use AME: AFunction([AField1]) where AFunction returns a string, such as #Tag1

@Pierre, thanks for v.121pr1 !

Unfortunately, I can still not get the Auto-assign rule to work .

For the text Field "Ingredients", I have the rule:

AME:SetIngredientsTags([Ingredients])

In User Code, I have:

Function SetIngredientsTags([Ingredients])
            SetIngredientsTags="#onion"
end function

When I change the Ingredients Field I get the same error as before (that the Field does not exists)

 

p.s. I E-mailed you my test file. I hope that the user code is stored in the SNDB file. Last time, when I closed and opened the file, the code was not saved...

I need some help with my VBScript and I was hoping to discuss at the CS today, but it seems that it was cancelled?

I have tried several versions. Here is a simple one that gives the popup error:

Try1

Function SetIngredientsTags(Ingredients)
        if Ingredients ="{Onion}" then SetIngredientsTags="#onion"
end Function

Here is another simple version that is closer to what I want (it also gives the error)
Try2

Function SetIngredientsTags(Ingredients)
        if Ingredients.Contains("Onion") then SetIngredientsTags="#onion" 
end Function

Slightly more complicated but even closer to what I want (it also gives the error)
Try3

Function SetIngredientsTags(Ingredients)
        dim Tagstring
        if Ingredients.Contains("Onion") then Tagstring="#onion"  
        SetIngredientsTags=Tagstring
end Function

p.s. I get a compilation error when I declare Tagstring as a string using "dim Tagstring As String"

Hi viking,

The issue here is that if Ingredients is not equal to "{Onion}" then function returns empty which IQ interprets as an error

So simply set SetIngredientsTags="" at the start of the function

Also the function was converted to upper case so you must test for {ONION}. This is fixed in v121Pre2.

Lastly, VBScript strings functions are case-sensitive, so "if s="onion" will return false when the input is "Onion". You can use compare fonctions (such as compare or instr) and specify how to compare string (link)

I am not following. If I set Ingredients to "{Onion}", I still get the error.
Anyway, I am also not sure if I understood your solution. I tried this and still got the error.

Function SetIngredientsTags(Ingredients)
        SetIngredientsTags=""
        if Ingredients ="{Onion}" then SetIngredientsTags="#onion"
end Function

EDIT:
I tried this:

Function SetIngredientsTags(Ingredients)
        if Ingredients ="{Onion}" then
                    SetIngredientsTags="#onion"
                    else SetIngredientsTags="#Pepper"
                end if
end Function

This doesn't give the error, but always returns "#Pepper". Thus when I set Ingredients to {Onion}, it is not recognized.

Thanks, v121Pre2 fixes it so that this function now works!:
Function SetIngredientsTags(Ingredients)
        if Ingredients ="{Onion}" then
                    SetIngredientsTags="#onion"
                    else SetIngredientsTags="#Pepper"
                end if
end Function

 

I now also need a way to remove #Onion Tag
This works fine as Auto-assignment for "Ingredients" to remove the #Onion Tag
AME: #Onion="" - two double quotes
(AME:#Onion='' - two single quotes also works)
 

However,  this function:

Function SetIngredientsTags(Ingredients)
        if Ingredients ="{Onion}" then
                    SetIngredientsTags="#Onion"
                    else SetIngredientsTags="#Onion="""
         end if    
end Function

The #Onion Tag is not removed..
(I also tried
else SetIngredientsTags="#Onion=''" - that is using 2 single quotes followed by a double quote after the =)

Is there another way to remove the #Onion Tag by the function?

AME: #Onion | #Pepper

There are 2 rules ( | separates rules), so the second rule is #Pepper, which if it works, does not have the proper syntax and may not work in the future 

Yes, I am aware that | separates the rules and that is why I used

AME: #Onion | #Pepper

Is there another way to set both the Onion and Pepper Tags using a single rule?

Can the function return something to set both the Onion and Pepper Tags?

Maybe the only way is to create a separate function for each Tag?  If I have 10 Tags in my multi-select text Field "Ingredients", I would need to have 10 functions where each functions checks for each Tag?
If so, I guess that it would need:
AME:SetIngredientsTags01([Ingredients])
AME:SetIngredientsTags02([Ingredients])
.
.
AME:SetIngredientsTags10([Ingredients])

(and then repeat this for each multi-select text Tag - I will probably end up with close to 100 functions)

You can do it with a single function, with the tag as a second parameter, but you'll need separate rules for each tag.

As you know, from the start, I didn't find your approach to be very "elegant" 😮, though you're correct that it "works" 👍

Could you give an example on how to use a single function to set both the Onion and Pepper Tags?

(I don't understand what you mean by using the Tag as a second parameter)

 

p.s. I 100% agree that my approach is not "elegant". However, it is the only way that I can think of to be able to use Forms to set Tags....
(to manually set the Tags using the Tags Pane is not using a Form)

I can't figure out how to do it with a single function if returning "#Onion | #Pepper" fails.

Is it correct that I will need to do the following to make it work (in the next version)?

A. For each multi-select text Field, I will need these Equations (e.g. if I have 10 Ingredients Tags):

AME:SetIngredientsTags1([Ingredients])
AME:SetIngredientsTags2([Ingredients])
.
.
AME:SetIngredientsTags10([Ingredients])

B. I will then need 10 VB scripts, one for each Ingredient Tag:

Function SetIngredientsTags1(Ingredients)
                SetIngredientsTags1="#Onion=0"
                If Instr(Ingredients,"Onion") then SetIngredientsTags1="#Onion"
end Function

Function SetIngredientsTags2(Ingredients)
                SetIngredientsTags2="#Pepper=0"
                If Instr(Ingredients,"Pepper") then SetIngredientsTags2="#Pepper"
end Function
.
.
etc

 

p.s. For some reason, I get a compilation error with an else statement:
                If Instr(Ingredients,"Onion") then SetIngredientsTags1="#Onion"
                     else SetIngredientsTags1="#Onion=0"
                End If

How do I ?