SETRANGE not applicable on value red|blue

Hi all,

i want add restriction on posting of item journal as per location for that i have done following implementation:

usersetup table:91 created field :loc. area code 200 run table adding value to this field as red|blue|green

on item jounal onaction of post addid code as:

UserSetup.RESET;
UserSetup.SETRANGE(“Loc area”,Rec.“Location Code”);
IF UserSetup.FIND(’-’) THEN
BEGIN
CODEUNIT.RUN(CODEUNIT::“Item Jnl.-Post”,Rec);
CurrentJnlBatchName := GETRANGEMAX(“Journal Batch Name”);
CurrPage.UPDATE(FALSE);
END
ELSE
MESSAGE(‘USER NOT VALID FOR THIS LOCATION: %1’,“Location Code”);

now it not allowing to post user with location code as blue,and red on item journal it giviong me error message why so.

Hi MDSRK365,

This line seems to be missing from your code :

UserSetup.SETRANGE(“User ID”,USERID);

i have tried that also but not working

Alright.

I think there is something wrong with your logic here. You want to prevent users with certain location code to post item journal, is that correct ?
If so, I recommend you to add a field (a boolean) in the Location table to identify which Location you want to block.

For example, you create the field “Allow posting item journal”, and you don’t check it for Blue and Red locations, and check it for the others.

Then, in the post action you do :

UserSetup.RESET;
UserSetup.SETRANGE("User ID",USERID);
UserSetup.SETRANGE("Loc area",Rec."Location Code"); //Rec can contain multiple Location Codes, so...
IF UserSetup.FINDFIRST THEN
BEGIN
	Location.RESET;
	Location.SETRANGE("Allow posting journal",TRUE);
	Location.SETRANGE(Code,UserSetup."Loc area");
	IF Location.FINDFIRST THEN
	BEGIN
		CODEUNIT.RUN(CODEUNIT::"Item Jnl.-Post",Rec);
		CurrentJnlBatchName := GETRANGEMAX("Journal Batch Name");
		CurrPage.UPDATE(FALSE);
	END ELSE
		ERROR('USER NOT VALID FOR THIS LOCATION: %1',"Location Code");
END ELSE
    ERROR('User setup does not exist for %1, location : %2',USERID,Rec."Location Code");

Of course, you have to create every user who are likely to post item journal in the User Setup Table.

i will try this and let you know thanks

One thing I noticed is that you are trying to put a filter string ‘red|blue|green’ into SETRANGE, which I think is the cause of your error. Read this to learn about the SETRANGE itself: https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setrange-method

You can only use SETRANGE for a single value, or a ‘fromValue / toValue’ pair of values, so everything between ‘fromValue’ and 'toValue inclusive.

If you are building a filter string like ‘red|blue|green’, you will need to use SETFILTER instead, read this: https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setfilter-method