Skip to Content
avatar image
Former Member

remove null in the SAP PI mapping context queue

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.

capture.jpg (16.1 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • May 08, 2017 at 03:53 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • May 05, 2017 at 11:26 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • May 05, 2017 at 10:21 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded