# Global variables

**URL:** <https://www.dynamicsuser.net/t/global-variables/8632>\
**Category:** Developers Forum\
**Created:** [April 30, 2004, 9:23am UTC](https://www.dynamicsuser.net/t/global-variables/8632 "2004-04-30T09:23:36Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![philipv](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@philipv](https://www.dynamicsuser.net/u/philipv)\
**Post date:** [April 30, 2004, 9:23am UTC](https://www.dynamicsuser.net/t/global-variables/8632/1 "2004-04-30T09:23:36Z")

</div>

I need to define a global variable in a Table which is a boolean flag. When I copy fields from another table in a field validation I turn this flag on. In insert trigger I check this flag and if it is off some fields take default values. BUT this flag is always off. Why the global variable does not keeps its value in Insert trigger. How can I handle this situation ?

---

<div class="post-metadata">

**Author:** ![Anna\_Perotti](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anna\_Perotti](https://www.dynamicsuser.net/u/Anna_Perotti)\
**Post date:** [April 30, 2004, 9:39am UTC](https://www.dynamicsuser.net/t/global-variables/8632/2 "2004-04-30T09:39:15Z")

</div>

> quote:
> 
> * * *
> 
> _Originally posted by philipv_  
> I need to define a global variable in a Table which is a boolean flag. When I copy fields from another table in a field validation I turn this flag on. In insert trigger I check this flag and if it is off some fields take default values. BUT this flag is always off. Why the global variable does not keeps its value in Insert trigger. How can I handle this situation ?
> 
> * * *

Forgive the stupid question - are you sure the trigger is executed? Anna

---

<div class="post-metadata">

**Author:** ![philipv](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@philipv](https://www.dynamicsuser.net/u/philipv)\
**Post date:** [April 30, 2004, 9:41am UTC](https://www.dynamicsuser.net/t/global-variables/8632/3 "2004-04-30T09:41:31Z")

</div>

> quote:
> 
> * * *
> 
> _Originally posted by Anna Perotti_
> 
> > quote:
> > 
> > * * *
> > 
> > _Originally posted by philipv_  
> > I need to define a global variable in a Table which is a boolean flag. When I copy fields from another table in a field validation I turn this flag on. In insert trigger I check this flag and if it is off some fields take default values. BUT this flag is always off. Why the global variable does not keeps its value in Insert trigger. How can I handle this situation ?
> > 
> > * * *
> 
> Forgive the stupid question - are you sure the trigger is executed? Anna
> 
> * * *

Yes I am

---

<div class="post-metadata">

**Author:** ![FormerMember](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FormerMember](https://www.dynamicsuser.net/u/FormerMember)\
**Post date:** [April 30, 2004, 9:51am UTC](https://www.dynamicsuser.net/t/global-variables/8632/4 "2004-04-30T09:51:10Z")

</div>

Hi Philip, are you sure that you are using the same variable for copying fields and INSERT. Or do you use a RESET before INSERT. Both leads to clearing all global variables. Br Josef Metz

---

<div class="post-metadata">

**Author:** ![philipv](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@philipv](https://www.dynamicsuser.net/u/philipv)\
**Post date:** [April 30, 2004, 9:58am UTC](https://www.dynamicsuser.net/t/global-variables/8632/5 "2004-04-30T09:58:57Z")

</div>

Here is the code : Equipment No. - OnValidate() IF Type=Type::A THEN BEGIN EquipLine.GET(…); VALIDATE(“Is Standard”,EquipLine.“Is Standard”); VALIDATE(“Basic Price”,EquipLine.“Basic Price”); VALIDATE(Markup,EquipLine.Markup); VALIDATE(“Final Price”,EquipLine.“Final Price”); VALIDATE(ETT,EquipLine.ETT); CopyFlag:=TRUE; END; OnInsert() IF NOT CopyFlag THEN BEGIN IF VMgtSetup.GET THEN Markup:=VehicleMgtSetup.MarkUp; END ELSE CopyFlag:=FALSE;

---

<div class="post-metadata">

**Author:** ![FormerMember](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FormerMember](https://www.dynamicsuser.net/u/FormerMember)\
**Post date:** [April 30, 2004, 10:38am UTC](https://www.dynamicsuser.net/t/global-variables/8632/6 "2004-04-30T10:38:57Z")

</div>

Hi Philip, how do you call the OnValidate and the INSERT br Josef Metz

---

<div class="post-metadata">

**Author:** ![philipv](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@philipv](https://www.dynamicsuser.net/u/philipv)\
**Post date:** [April 30, 2004, 10:42am UTC](https://www.dynamicsuser.net/t/global-variables/8632/7 "2004-04-30T10:42:17Z")

</div>

> quote:
> 
> * * *
> 
> _Originally posted by jm_  
> Hi Philip, how do you call the OnValidate and the INSERT br Josef Metz
> 
> * * *

They are called during form data entry

---

<div class="post-metadata">

**Author:** ![philipv](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@philipv](https://www.dynamicsuser.net/u/philipv)\
**Post date:** [April 30, 2004, 11:01am UTC](https://www.dynamicsuser.net/t/global-variables/8632/8 "2004-04-30T11:01:46Z")

</div>

The only solution that works in this case is to define a temporary table with this flag and accessing the flag from this table. But then what the purpose of global variables ? What globals they are if their values implicity initialized in every trigger ? [:(]

---

<div class="post-metadata">

**Author:** ![FredBergmann](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@FredBergmann](https://www.dynamicsuser.net/u/FredBergmann)\
**Post date:** [April 30, 2004, 11:31am UTC](https://www.dynamicsuser.net/t/global-variables/8632/9 "2004-04-30T11:31:07Z")

</div>

Hey have you checked, that you have not define a local variable which you have named the same like the global variable? Fred

---

<div class="post-metadata">

**Author:** ![philipv](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@philipv](https://www.dynamicsuser.net/u/philipv)\
**Post date:** [April 30, 2004, 11:55am UTC](https://www.dynamicsuser.net/t/global-variables/8632/10 "2004-04-30T11:55:51Z")

</div>

> quote:
> 
> * * *
> 
> _Originally posted by FredBergmann_  
> Hey have you checked, that you have not define a local variable which you have named the same like the global variable? Fred
> 
> * * *

Sure yes. Make just a test a table and try it.

---

<div class="post-metadata">

**Author:** ![FormerMember](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FormerMember](https://www.dynamicsuser.net/u/FormerMember)\
**Post date:** [April 30, 2004, 12:41pm UTC](https://www.dynamicsuser.net/t/global-variables/8632/11 "2004-04-30T12:41:34Z")

</div>

Your problem is that you are looking at two variables…The Rec from the Form and the Rec from the Table (The Table gets the Flag set, but the Form is Inserted [}:)]). Some of the easier options might include creating CopyFlag as a field on the table, or creating a function to set copyflag on the Insert Trigger of the table.

---

<div class="post-metadata">

**Author:** ![FPulsfort](https://avatars.discourse-cdn.com/v4/letter/f/ba8739/32.png) [@FPulsfort](https://www.dynamicsuser.net/u/FPulsfort)\
**Post date:** [April 30, 2004, 1:02pm UTC](https://www.dynamicsuser.net/t/global-variables/8632/12 "2004-04-30T13:02:46Z")

</div>

Have you tried using the same triggers and a global in the form? I know, that it is better to work in the tables, but in such a situation this should work. Greetings, Frank

---

<div class="post-metadata">

**Author:** ![philipv](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@philipv](https://www.dynamicsuser.net/u/philipv)\
**Post date:** [April 30, 2004, 1:07pm UTC](https://www.dynamicsuser.net/t/global-variables/8632/13 "2004-04-30T13:07:06Z")

</div>

> quote:
> 
> * * *
> 
> _Originally posted by FPulsfort_  
> Have you tried using the same triggers and a global in the form? I know, that it is better to work in the tables, but in such a situation this should work. Greetings, Frank
> 
> * * *

Thanks Frank. This seems to work but I prefer to keep the code in table and handle this with temporary table.

---

<div class="post-metadata">

**Author:** ![apertierra](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@apertierra](https://www.dynamicsuser.net/u/apertierra)\
**Post date:** [April 30, 2004, 4:48pm UTC](https://www.dynamicsuser.net/t/global-variables/8632/14 "2004-04-30T16:48:12Z")

</div>

> quote:
> 
> * * *
> 
> _Originally posted by philipv_ Thanks Frank. This seems to work but I prefer to keep the code in table and handle this with temporary table.
> 
> * * *

In your case you’re having several options: 1) Create a field on the table instead of using a variable. 2) Create the function on the form. 3) Adding a function on the oninsert trigger that checks if it’s copied in a different way (like checking on insert if it’s having any value on the field you’re validating and it’s type A then it’s copied) (example: IF ((“Equipment No.” \<\>‘’) AND (Type = Type::A)) THEN Copyflag := true; ) The problem is that the form won’t keep the value on the variable as it will just “refresh” getting the variable all time.

---

<div class="post-metadata">

**Author:** ![FormerMember](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@FormerMember](https://www.dynamicsuser.net/u/FormerMember)\
**Post date:** [May 4, 2004, 10:35pm UTC](https://www.dynamicsuser.net/t/global-variables/8632/15 "2004-05-04T22:35:10Z")

</div>

The scope of globals is not that global then you would like. In cases like this I use Additional field. It’s the most straightforward. You already have added few fileds one boolean more is not big deal. Maybe you can even tell by some other field that the this OnValidate has been run, for example “Basic Price”\<\>0.

---

<div class="post-metadata">

**Author:** ![FGabriels](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@FGabriels](https://www.dynamicsuser.net/u/FGabriels)\
**Post date:** [May 5, 2004, 8:40am UTC](https://www.dynamicsuser.net/t/global-variables/8632/16 "2004-05-05T08:40:11Z")

</div>

Just a question Field named “Type” are a menber of the primary Key?

---

<div class="post-metadata">

**Author:** ![pr1](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@pr1](https://www.dynamicsuser.net/u/pr1)\
**Post date:** [May 7, 2004, 5:58am UTC](https://www.dynamicsuser.net/t/global-variables/8632/17 "2004-05-07T05:58:58Z")

</div>

Another way to go: **Use a SingleInstance Codeunit.** Only catch here is that users can open the same form twice, so you need some version checking.
