Search code examples
javainterceptorsap-commerce-cloud

get values from impex into InitDefaultInterceptor Hybris


I have an impex file(example)

INSERT_UPDATE Subscriber;firstName[unique=false];lastName[unique=false];email[unique=true];bulk(code)[default=false]
;FirstName;lastName;email@gmail.com;true;

and an InitDefaultInterceptor

 public class MyInterceptor implement InitDefaultInterceptor<SubscriberModel>{

onInitDefaults(SubscriberModel model, InterceptorContext ctx)
}

How can I get values from the impex in this interceptor? I try to use

model.getFirstName(); 
....

but all the methods returns "null". What can I do to get the values ? I need to implement a logic before I save them into db.


Solution

  • If you want to make use of the values that are being sent, you need to use a PrepareInterceptor instead of a InitDefaultInterceptor interceptor.

    • InitDefaultInterceptor : The Init Defaults Interceptor is called when a model is filled with its default values. In your case, this happens at creation of the new instance of the object you want to add to the database. This interceptor is used to add default values (next to the ones you already defined in your items.xml). Only the defaults, marked in your items.xml are inserted at this point. No data from your impex is loaded here, as this just handles the defaults for new objects, no matter the content that will be added at a later stage.

    • PrepareInterceptor : The Prepare Interceptor is called before a model is saved to the database. Use this to add values to the model or modify existing ones before they are saved. In this interceptor, the values of your impex will be filled in the model object. You can add or modify your data here depending on your usecase.

    For more info on all type of interceptors, there is a help page from SAP that describes all of them.