Skip to Content
0

compare changed values in onSave event

Jan 04 at 01:09 PM

120

avatar image

Dear Experts,

I have a requirement to compare if a field of an Opportunity has been changed in the onSave event.

I already found a post that suggests to use FromDB Query but this does also not work. (

https://archive.sap.com/discussions/thread/3918665)

Please see my example:

var query = Opportunity.QueryByElements;
var selParameter = query.CreateSelectionParams();
    selParameter.Add(query.ID.content, "I", "EQ", ls_this.ID.content);
var result = query.ExecuteFromDB(selParameter); 
foreach (var ls_result in result){
var eins1 = ls_result.SalesAndServiceBusinessArea.SalesTerritoryID.content;
var zwei2 = ls_this.SalesAndServiceBusinessArea.SalesTerritoryID.content;
var eins = ls_result.Name;
var zwei = ls_this.Name; 
} 

For vars eins1 and zwei2 the values always match, so the change in to noticed.

However for vars eins and zwei it works perfectly.

I suspect this problem occurs, as the SalesTerritoryID is part of the SalesAndServiceBusinessArea node and not the Root node.

Does anyone of you have an idea how to resolve this? Is there anyway to get the DB values of the subnodes?

Thanks for your help

Best Regards

Tobias Träger

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
s sin Jan 11 at 03:45 PM
0

Hi Tobias,

I also had the same issue as you, I solved it like this:
I created a custom fields for old value (Old_Value) and bind it to related value and in the Event After Modify you can compare "this.Value" with "this.Old_Value" (since Old_Vlaue still has the previous value not the new one)

Hope it can help

Sin

Share
10 |10000 characters needed characters left characters exceeded
Tobias Kuhn Jan 04 at 05:04 PM
0

Hi Tobias,

As an quick idea, have already tried to created an validation on save script at the node SalesAndServiceBusinessArea and checked what there are the results?

Theory:

As an anticipation from my side (not really sure if this is right), it could be because the part "ls_result.SalesAndServiceBusinessArea" resolves an association and I'm not sure if this is also an DB only resolution or from the buffer. As mentioned in the documentation the ExecuteFromDB returns the data from the DB not from the buffer, but the behavior of the association resolution is not directly specified. Its more or less the expected behavior...

ExecuteFromDB Function

The ExecuteFromDB function provides the same parameters for selection and sorting as the Execute function. However,ExecuteFromDB function returns the data that is saved on the Database and not from the buffer. Hence, the data returned via this function will not contain any unsaved changes done in the current transaction.

Best regards

Tobias Kuhn

Share
10 |10000 characters needed characters left characters exceeded
Tobias Träger Jan 05 at 12:49 PM
0

Hi Tobias,

thanks for your reply. The Validation script does not really help, as we want to save additional data in the onsave script itself. But just in case the Territory ID has changed from "something" to a "specific" territory. Due to performance reasons this routine should only be processed in detail if this condition matches.

Like you pointed out the Query gets the result from the DB as vars eins and zwei proof.

Unfortunely I fear that it's like you think... the root node is retrieved from the DB but the associations come from the buffer. which is unexpected behavior in my opinion, as I told the query to use the DB. Can anyone from SAP proof this Theory?

So the question at the end is: is there any possibility to get those associations/sub nodes/compositions also from the DB instead of the buffer? Because as far as I know this sub-notes in the example can only be accessed by a association, there are no queries etc. definable.

Best Regards

Tobias

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Hello Tobias,

As you have already detected the buffer content is replaced by executing the query.

The correct query would be ExecuteFromDBDataOnly(...).
This will return only a data set with the values. Unfortunately this data set does not support the navigation to the sub-nodes (it's only a data set). So you need a query direct on the node for which you want to retrieve the elements.

If this is not possible you may add transient Indicator-type fields (only for ESF2 enabled SAP BOs) which are set to true during the AfterModify if the resp. value has been changed.

HTH,
. Horst

0

Hi Horst,

thank you for your answer.

But for SAP BOs I'm not able to create my own queries, right? So basically I have no change at all to get the DB values of sub-nodes of SAP BOs at all.

Thanks unfortunate, but if this is the case we will use some own field in the BO to insert the old value there for comparing. Not the optimal Solution, but it looks like the only possible.

Best Regards

Tobias

0

Hello Tobias,

If there is no suitable query at the req. node then it's like it is.

Sorry,
. Horst

0