Convert String into CDT

Hi All,

 

I am getting the response from exec procedure like below:

[success:true,error:,parameters:[BOOKS_CURSOR:[quantity:23,author:Collectif,price:3.74,title:Peppa Pig: Fun at the Fair]; [quantity:7,author:Judith Simanovsky,price:4.99,title:Panda Goes to the Olympics]],result:]

 

But I need to save the values of BOOKS_CURSOR in Books CDT which has quantity, author,price & title fields. Can you please guide how can I do it?

 

Thanks

  Discussion posts and replies are publicly visible

Parents Reply
  • I tried to use it like

    cast(typeof(type!Book),local!value)

    Here Book is my CDT & local!value is
    [success:true,error:,parameters:[BOOKS_CURSOR:[quantity:23,author:Collectif,price:3.74,title:Peppa Pig: Fun at the Fair]; [quantity:7,author:Judith Simanovsky,price:4.99,title:Panda Goes to the Olympics]],result:]

    But this is giving error:

    Expression evaluation error at function 'cast' [line 15]: Could not cast from Text to Type. Details: CastInvalidCould not cast from Text to Type. Details: CastInvalid
Children
  • It should be formatted as below, and your label names should match your CDT field names:

    cast(
        typeof({type!Book()}),
            {{quantity:23,author:"Collectif",price:3.74,title:"Peppa Pig: Fun at the Fair"},
            {quantity:7,author:"Judith Simanovsky",price:4.99,title:"Panda Goes to the Olympics"}})

    Let me know if that works for you.

  • 0
    Certified Lead Developer
    in reply to rishub

    I believe you need to use the extract() function.  I suggest making a modular expression rule specifically to handle this conversion, for ease of access and updating in the future.  I've written one previously to translate the results of the "getprocessmodeldetailsbyuuid()" plug-in function into dictionary, and it works fairly well.

    The following code assumes you will create it as an expression rule and the ri! will be the string (and that if at any point you want to do multiple book strings, you'll apply() this rule over the list).

    with(
      local!quantity: extract(ri!rawText, "quantity:", ",author"),
      local!author: extract(ri!rawText, "author:", ",price"),
      local!price: extract(ri!rawText, "price:", ",title"),
      local!title: extract(ri!rawText, "title:", "]"),
    
      {
        author: local!author,
        title: local!title, 
        quantity: local!quantity,
        price: local!price
      }
    )
       

     

    In this example I establish each value as its own with() variable before wrapping up everything in a dictionary to be returned at the end.  This might seem slightly overcomplicated but separating the processing in such a way helps if you ever decide the dictionary names should be different, or if one particular field value needs pre-processing before being returned, etc.

  • When I am trying the data that I get from rule in the expression by copying the data like you have done, it works but when I use local!value in that expression it gives me blank result.