How do I create an "Update [ContractId X]" from a list of template X?
- lashenhurstOP
Hi all,
I’m looking to refine my choice logic, to make it more elegant and hopefully a little more performant.
For the choice in question, we take in a list of product details, we need to process those details to create
Productcontracts for each, and the perform secondary logic to update the instance ofProductOwnerwith relevant Product information.The intention was to do something like the below:
let processProductInformation(productInfo: ProductInformation) = do ... let newProduct = Product with ... return newProduct let updateProductOwner(newProduct: Product) = do ... create ProductOwner with ... let newProducts = map processProductInformation productsInfo newProductOwners <- mapA updateProductOwner newProducts newProductCids <- mapA create newProducts return newProductCidsBut I seem to have gone wrong with those last two lines, I’m trying to have the choice return
[ContractId Product]but not certain exactly what’s needed to get there.Any advice would be greatly appreciated!
- cocreature
It looks like
processProductInformationalready has typeProductionInformation -> Update Product. And you only usemaphere sonewProducts : [Update Product]. You then try tomapA createover it which fails becausecreateexpectsProductnotUpdate Product.To fix the issue try using a monadic bind and
mapA:newProducts <- mapA processProductInformation productsInfo newProductOwners <- mapA updateProductOwner newProducts newProductCids <- mapA create newProducts return newProductCidsNote that
newProductOwnersis unused which seems a bit suspicious. - lashenhurst
You’re right on
newProductOwners, that was my mistake when writing the question, I am in fact usingmapA_in my DAML.I tried your suggestion, and now the IDE says I’m returning
Update (Update [ContractId Product])rather than the expectedUpdate [ContractId Product] - cocreature
That sounds like you are using a
letinstead of a monadic bind<-fornewProductCidsmaybe? - lashenhurst 2
Okay, that was my mistake again, it warned me of redundant return, so I tried
return (mapA create newProducts)whereas all I really needed wasmapA create newProductsas the last line.Thanks for that!