cancel
Showing results for 
Search instead for 
Did you mean: 

remove null in the SAP PI mapping context queue

Former Member
0 Kudos

Hello Experts,

How to check null value and xsi:nil in the queue when we have multiple instances in the context.

there are two possibilities in input which I need to handle as below

1. node does not exist the source message then the value would be "<null>" in the display queue

2. node exist but with attribute nil = true then the value would be "xsi:nil" in the display queue

I need True of False when I get the above input and I would like to handle both the scenarios in single UDF.

I created a UDF as per mentioned in the below link

https://blogs.sap.com/2013/02/05/null-in-the-sap-pi-mapping-context-queue/

for(int i=0;i<var1.length;i++){

if(var1[i] == null){ result.addValue(“true”);}

else{ result.addValue(“false”);}}

but this UDF is not working in when input is <null> scenario.

and I tried as below

if (var1.length == 0) result.addValue("false");

else result.addValue("true");

return;}

this is working when I have only one instance in the context.

Please share your comments on this.

Accepted Solutions (0)

Answers (3)

Answers (3)

markangelo_dihiansan
Active Contributor

Hi,

The code is below:

try{
	if(inp.length>0){
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		factory.setNamespaceAware(false);
		factory.setValidating(false);
		for(int a=0;a<inp.length;a++){
			InputStream is = new ByteArrayInputStream(inp[a].getBytes());
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse(is);
			doc.getDocumentElement().normalize();
			Element root = doc.getDocumentElement();
			String nil = root.getAttribute("xsi:nil");
			if(nil.equals("")||nil.equals("false")){
				result.addValue("true");
			}
			else{
				result.addValue("false");
			}
			is.close();
		}
	}
	else{
		result.addValue("false");
	}
}
catch(Exception e){
	e.getMessage();
}

Test

Regards,

Mark

Jitender
Advisor
Advisor
0 Kudos

Hi Suman,

There are other better ways to handle the scenario. Use standard function "exists" and "mapwithdefault" to handle such cases.

If you must use UDF, adjust the context of the queue and use mapwithdefault before you pass the queue to the UDF.

Regards,
Jitender

Former Member
0 Kudos

Thanks a lot for you reply Jitender,

Null value will be removed when I map sorece field ---> mapwithdefault ---> UDF which I mentioned above.

I am trying to achieve this with UDF alone since there are two things which I need to handle one is "null" and other one "xsi:nil". That is the reason where I am thinking to handle both the scenarios in the same.

Please share your suggestions.

Thanks,

Suman

Jitender
Advisor
Advisor
0 Kudos

Hi Suman,

Could you please share your exact requirements?
The problem you stated with multiple null entries are not being handled correct, looks to be caused due to missing context in the queue and hence I suggested you to adjust the context and use map with default to replace null with something else. Than you can build your UDF logic.

Try checking what context level you have set your source field to. Post a screenshot of your source queue as well to help us understand what you are getting in the source queue.

Regards,
Jitender

former_member190293
Active Contributor
0 Kudos

Hi Suman!

Your UDF doesn't work because according to your screenshot you have no elements in input queue, so your for{} loop isn't executed.

Further actions depend on your mapping logic. For example, you can put the condition in your code and add the single value to output queue if your input queue has zero length.

Regards, Evgeniy.

Former Member
0 Kudos

Thanks a lot for reply.

If I have null with other elements also my for{} is not getting executed. Please have a look at the screen shot below.

input-output-queue.jpg

In the input there are four contexts including null, in this scenario also for{} is not getting executed for the context which has null value.

As you mentioned above how can we check the queue size?

former_member190293
Active Contributor
0 Kudos

Hi Suman!

In your case you have contexts with elements containing null values (xsi:nil) and context without elements inside (null context).

So, if you want to check if your queue contains any elements, it's enough to check its length, like: var1.length > 0.

Regards, Evgeniy.

Former Member
0 Kudos

Thanks you very much Evgeniy,

Already I tried to check the length also but it does not give length for null, in output it shows as null it self and it gives length as 12 for xsi:nil.

Any other suggestions on this ?

Thanks,

Suman

former_member190293
Active Contributor
0 Kudos

Hi Suman!

Wouldn't you please provide the code with length checking?

Regards, Evgeniy.