Groovy web console

subscribe to the feed Subscribe
to this
site

Nullsafe and Conditions Test Sample Script

Published 4 months ago by S V with tags nullsafe operator conditions array objects
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
//Sample groovy scripttry{
    def Line1 = [ContactRoleId:"300000333771225", Status: "S",ShipFromPartyId:null]
    def Line = null
    def Header = [FulfillOrgId_Custom: "OrgId1234",CustomerId : "Cust1234"]
    String InvOrgID = "";
    String status = "";
    def Res
      if (Line?.ShipFromPartyId  == null )
      {
        InvOrgID = "X";
      }
      else
      {
        InvOrgID = "Y";
      }
    println("Value of InvOrgID: " + InvOrgID);
//Line?.Status makes sure that even if Line does not exist, it does not throw nullpointer. ? is the nullsafe operator
    status = Line?.Status;
    println("Value of status: " + status);
    def Result = [InvOrgID: (Line?.ShipFromPartyId?:Header.FulfillOrgId_Custom), EBSOrderNumber:(Line?.ContactRoleId ?: Header.CustomerId)]
    println("Value of Result: " + Result);
    }
catch (ex)
{
    println('ERROR : ' + ex.getMessage());
}