ASSIGN – Life made easy.
Moderator notice The techniques described in this blog are NOT recommended by SAP and many independent professionals would also not recommend their use. It is far better to use the customisation techniques provided by SAP.
If you use the technique described in this blog, you do so at your own risk and you should read the comments section carefully. “It works” is simply not a good enough reason when you are dealing with customer systems.
Before I start on the topic of ASSIGN statement in ABAP, I would like to take the opportunity to introduce myself here. I am a 10 years experienced SAP professional with 4 years experience as a technical consultant and rest as functional. Though I am more of a functional now, I still do not mind diving deep into ABAP getting my hands dirty in coding when the situation asks for it.
Recently I was working on a problem in which I had to populate the value date for the clearing document which is posted after manually clearing the open items for the customer. The tricky thing was to determine at runtime which are the two selected open items selected by the user to clear.
The solution was obvious – FI substitution for value date.
However, knowing that the solution was easy, implementing it, turned out to be quite tricky.
Obstacle 1: Value date was not present in the list of fields to be substituted in BSEG. How to substitute then?
Solution: After doing sufficient amount of research on the internet and exploring the issue with the seniors I learnt that we can open certain fields from the BSEG table for substitution which are initially not allowed by SAP. Yeah I agree opening certain fields from the table “VWTYGB01” is a modification, but looking the possible options at hand and the dire customer requirement we had limited choices and had to take this path. For more details on this kindly look at the hereby mentioned SAP Notes: 842318, 42615.
Also follow the below mentioned links for more information on this.
http://scn.sap.com/thread/3168871
http://scn.sap.com/thread/1254973
Anyways I do not want to digress too much from the core topic here.
Obstacle 2: Finding out the two document which are to be cleared manually by the user ?
Solution: With no clear approach in sight we started debugging the standard program “SAPMF05A” for F-32 i.e. “Clear Customer data”. Within 5 min of debugging (I am not exaggerating here) we found out that everything tied to the internal table POSTAB[]. My hunch was to read this table in the substitution to dynamically identify the line items. I created a sample substitution and hooked it in the FI postings. For more details on how to create substitution please refer to the link.
The table data was present in the ABAP memory at the time the code control came to the substitution exit sub routine. Please refer to the ABAP memory stack screen shot shown below.
The sole task remaining was to now to read that data. The magic is was done by sole three statements coming to the rescue.
The general syntax as mentioned in the F1 help is ASSIGN (memory_area). Now the interpretation of the memory area is quite interesting.
Assign (lt_itab[]) to <fs>
This will assign the starting memory address of the internal table to <fs>.
Assign (program_name) to <fs>.
This will assign the starting memory address of the complete program ABAP memory to the field symbol.
Assign (‘(program_name)lt_itab[]’ ) to <fs>.
This will assign the starting address of the internal table lt_itab[] from the program_name to the field symbol. As a side note we have to make sure that the program_name is loaded into the ABAP memory at runtime for one to access.
From the SAP help, we do understand the usage of ASSIGN statement, but this implementation will surely stick to my head for forever after this.
Hope this helps other developers as well.
This particular usage of the ASSIGN statement has been debated upon N number of times in the forums. The key point to note here is that this construct is for "internal use" only.
As an alternative, if you are on SAP release > ECC6.0, you can make use of an implicit enhancement to export the value of the required global data. This IMHO is a more cleaner approach.
Hi Suhas,
Thanks for your comment. Appreciate your view.
However, why burden the customer with maintaining these implicit enhancements and watching out for them in case of upgrades when there is no need, and the data is availaible to you at runtime always.
Ashish
What do you mean by "watching out for them in case of upgrades"? Implicit enhancements are part of the Enhancement Framework (EhFW), which also include Enhancement Spots & Kernel BAdIs. So do you mean that by implementing elements of the EhFW we are "burdening" the customer?
From ABAP Keyword Documentation, i quote -
That is a valid enough reason not to use it, when there are easy alternatives available.
Hi Suhas,
The sole purpose of following this approach was to reuse something already existing in the memory rather than creating a enhancement.Well save some work for myself. 🙂
Ashish
Saving some work for yourself is not a good reason to add risk to a project. Using anything that's marked for internal use only means you're doing so at your own risk. If it goes horribly wrong and you wreck your database, then you have zero comeback on SAP.
I've been involved in SAP development for more than 15 years and have not once seen a scenario where this technique was necessary to meet a business requirement.
Hi Matt,
My comment of "Saving some work for myself" was not because of laziness, but because of certain reasons as i have mentioned in one of the comments below. However individuals may differ in opinion. I myself have worked with SAP labs for 5 years in custom development team before moving on, and duely understand the complexity involved in handling the standard programs. I would never take a un-informed dicision.
Ashish
I have seen this used (by a consultant) in a VOFM routine to get some values that were needed for the business logic but were not available in any of the parameters. No disrespect to the consultants, but unfortunately the "hired guns" would be more inclined to put stuff like that in our systems since usually they don't need to deal with the consequences.
Want to thank the moderators for adding the note on the top. Even I have perceived this "solution" as not great but somewhat benign and the comments here outline perfectly the risks as well as provide an alternative solution (an enhancement). Live and learn!
I'm absolutely with you!
1+ to the moderator, which has added the note.
~Florian
PS: vofm is a lovely place to find such stuff 😉
You'll "save" some work, until such time SAP does change the internal implementation or behaviour (up to getting rid of the internal table entirely) and nobody during upgrade will have the slightest idea that there might be some customer logic relying on that behaviour or internal table. It's virtually "invisible" dependency...
If there would be an enhancement or modification doing normal assignment of POSTAB[] there would be syntax error if SAP has removed the internal table, and even if the there is no syntax error somebody would have to (hopefully) at least take a look during SPAU, whether or not the enhancement logic is still working.
The way you have implemented access, the custom logic might fail silently after upgrade - which is the worst way for logic to fail.
If it's something critical, where we implicitly rely on SAP functionality being certain way (not even talking about grabbing some random SAP internal data out of memory), we sometimes add enhancements doing absolutely nothing other than documenting that we rely on this and that being this and that way. Just so someone has to look at the stuff during upgrade... That is trying to "save work" - the work needed to clean up mess left by custom logic failing silently, sometimes for months.
cheers
Janis
Hi Jaice,
Thanks for your valuable comments.Appreciate it.
By my remark "saving myself some work" i did not intend to be lazy to code something or not knowing how to implement implicit enhancement. The choice i made was intentional. Here are the reasons.
- The whole SAP transaction F-32 is based on POSTAB[]. I am making this statement based on debug analysis. So till the time the transaction stays, the internal table will definately stay, unless there is a complete re implementation of the standard transaction. Please validate.
- There is a explicit use of the field symbol and there is a underling advantage to this. The ASSIGN statement will assign the starting memory address of the internal table to the field system and thereafter the complete data stored in the memory area is moved into another internal table(as shown in the code screen shot). So if there is a enhancement of the table structure that is well coved.
- There has been well emphasis laid on the fact while implementing the code, that there is no "playing around" with the assign statement which will hold the starting address of the internal table. Once the complete table is moved into a local table and then used only for read only purposes. Apologoies for not highlighting this piece of information above.
However i totally understand, that there might be different opinions.
- Also the above post is in relation to the substitution, in which the calling program SAPMV05A is still in the ABAP stack. I have pasted the screen shot of that as well. The amount of attention this blog has got, has forced me to search existing content on SCN, and here are some more links. Kindly refer to them as well.
http://scn.sap.com/thread/1469530
http://scn.sap.com/thread/1721038
http://scn.sap.com/thread/103427
http://scn.sap.com/thread/126545
Ashish
I shouldn't really be "pronouncing" anything on something so extensive as SAPMF05A (and the related code) after looking at it for 15 minutes, but, given these two choices to come up with clearing code that can be (easier) maintained and extended for the "next 20 years" for "Simple Finance":
a) trying to write the functionality from scratch;
b) trying to refactor code;
I'd choose b) without even thinking long about it. And the business logic then gets carved out of the module pool, and all the globals, other than those used directly in the dynpros, obviously must go then. Starting from those used most often... F-32 can stay...
You should start your post with "DO NOT USE!" warning...
It is much worse than modification.
Hi Shai,
Appreciate your comments and the basis on which you made the comments.Would appreciate if you can share the reasons.
Kindly read through my reasons of following this approach, and follow some old posts as well.
http://scn.sap.com/thread/1469530
http://scn.sap.com/thread/1721038
http://scn.sap.com/thread/103427
http://scn.sap.com/thread/126545
Best Regards,.
Ashish
Hi,
In short, you are referring to internal data, which you shouldn't refer to.
Moreover, as Ashish Singhal mentioned, the usage of this memory area isn't documented anywhere and cannot be tracked down.
I would prefer (as a last resort) a modification over it.
The links you were referring to only explain about the technical usage of it, but says nothing about the consequences of it, which is the main issue here.
You would prefer a "modification" over it...I mean really..that explains it all.
Ashish
Yes, because modifications are tracked and highlighted and there are tools for dealing with them during upgrade/patch applications.
Modifications are definitely to be preferred over this kind of manipulation. They're also to be preferred over copying a SAP program and modifying it for your own purposes - for exactly the same reason.
Until you've had the pain of having to fix a cloned program, or dealing with a sudden loss of a variable due to SAP changes, you may not really understand why this is so important. But perhaps it's a good idea to trust the knowledge and experience of developers with far more experience than you.
This blog has been reported to me for moderation. It is fairly well written, and in my view meets the criteria for blogging on a technical issue from a personal perspective.
However, the comments by Shai Sinai and Suhas Saha stand. This technique is not recommended. Therefore I've edited your blog. The notice MUST stay or I will be forced to reject the blog entirely.
Hi Matt,
I totally appreciate your view point, and respect the hard work you are putting in order to make the SCN place a more collaborative platform.
I would really be thankful to you if you can keep the complete remarks from everyone in view before adding the disclaimer. You are the best to decide.
Kindly go through the above memtioned comments by me and the below links as well.
http://scn.sap.com/thread/1469530
http://scn.sap.com/thread/1721038
http://scn.sap.com/thread/103427
http://scn.sap.com/thread/126545
Best Regards,
Ashish
Ashish, there is no need to copy-paste the same content in the comments - we can see all the comments as well as everyone who commented gets an email notification.
From the threads you refer to the latest one is dated 2010. Lots of things happened since then, in particular we have much better awareness now about the enhancement framework (even the dinosaurs like me caught up! 🙂 ). If something was said on SCN 4 years ago it does not mean that it's still valid or a good practice today.
Thank you.
Hi,
I am very happy to see this blog post but the real lesson comes from the debate that follows.... .
A very good example of NOT to do .
Few years back we had a BADI where "(PROG)DOBJ" addition was used. (I was raising hell about it...) .
Later I found out that CALL FUNCTION 'DYNP_VALUES_READ' can replace the assign. (After fixing the code I slept better...) .
One thing I learned from 41 years of bytes shuffling (yes I am not a kid...) : A system is like a mule you have to treat it with respect else you get:
Regards .