Amazing what just typing it out can do for you...
I think I figured out the problem. The error message was misleading me. The problem was that, as part of the error said, there was already a record with that value, so it would have been a duplicate value. Doh!
So what's up with the red-herring of adding the field to a layout?
If all your layouts are based on Table A, why change layouts at all? Set field can update fields without them being physically present on the layout.
It is not possible to be able to tell you how to correct the specific validation error that you are getting without more information. It's possible that a commit records event is tripping the error and then, when the script continues after the error, it then is set to the desired value and there are a number of possible reasons why it is only triped part of the time. You might also check to see if maybe the value in the field is not unique.
You may find it useful to set up a "utility layout" based on table A where all fields are present. Then redesign your script to go to this one layout to set the needed values to the needed fields and then return to the original layout or where ever you need it to. THen, if the validation error is tripped, you can see data is present in each field of that record and this may provide a clue as to why you are getting an error.
If you have FileMaker Advanced, you might also try using the script debugger and data viewer to step through the script to spot where exactly you ge the validation error. IF you don't have advanced, you can add a few show custom dialog steps with different messages and then run the script to better narrow down where in the script this validation error is tripped.
I had a similar issue and found the problem, at least what it was in my database:
Basically check that the next auto assigned ID in the portal ID record is higher than the record with the highest ID number.
The error is thrown because an ID is assigned that already exists, which causes a validation error if you have "Unique" validation selected.
My error was caused when I imported data which contained higher ID's than the field was defined to assign as the next ID.
The problem is that the error text does not directly convey that it is the validation check which fails. In my case it was the "Unique" validation that failed.
The error text misdirects your attention, by stating that the field requires a value (making you think the ID is empty, when it is not) and that the field is not available on the layout (which in this case is irrelevant as the ID field is autogenerated).
What the error SHOULD say is: "The portal field "xxx::yyy_ID" failed validation as it is not Unique."
That would direct your attention to the actual error.
Just like Justin Close said...