cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

[MDK] CreateRelatedMedia override media property from rule not working

kevinbraun
Explorer
0 Likes
435

I am using MDK version 24.11.3-001 on Android.  I have an Attachment Control where the user will be adding several images at once, then will click a Save button to upload the attachments to the backend.  The backend is a v2 OData service, and I have a Media entity set that is access through a navigation path (eg /Delivery('123')/Attachment/$value).

I have a CreateRelatedMedia action to perform the upload.  Once the save is complete, I would like to take another action, such as closing the page and returning to the list.  Furthermore, I'd like to add a SLUG header to include the filename for each individual file in the attachment control.

To accomplish these two desires, I believe the best way would be to loop through the attachments in a rule, and execute the action by overriding the properties for each loop.  The documentation indicates that the Media property should be an object with a "content" property and a "contentType" property.  

    let attachments = clientAPI.evaluateTargetPath('#Page:DeliveryAttachment/#Control:FormCellAttachment0/#Value');
    let uploadPromises = [];
    for (let i = 0; i < attachments.length; i++) {
        const attachment = attachments[i];
        const content = attachment.content;
        const contentType = attachment.contentType;
        const filename = attachment.urlString;
        const actionPromise = clientAPI.executeAction({
            "Name": '/myapp/Actions/DeliveryAttachments/OData/CreateDeliveryAttachmentRelatedMedia.action',
            "Properties": {
                "Media": {
                    "content": content,
                    "contentType": contentType
                },
                "Headers": {
                    "SLUG": filename
                }         
            }
        });
        uploadPromises.push(actionPromise);        
    }
    return Promise.all(uploadPromises).then(
        () => {
            clientAPI.executeAction('/myapp/Actions/ClosePage.action')
        }
    );

 I've tested this on both Web and Android and I get an exception from the MDK code (although each version throws an exception at different spots - I'm really only concerned with the Android functionality, so below I've shown the error from the Android device).  I've tried several different variations of this code, so I think the code is correct, but please let me know if I've done something wrong in my code.

On the Android device, the exception is raised from node_modules/mdk-sap/ODate/crud/ODataRelatedMediaCreator.ts:

Screenshot 2025-05-29 121915.png

As mentioned I get a similar error in Web, although from a different place in the code.

Have I encountered an issue or is there something I'm doing wrong in the code, or should I take a completely different approach?  (Note I've tried just executing the action and using the attachment control #Value in the media property, which will upload each attachment in the control - but the issue is that if I put another action in the Success Action, this success action gets executed for each attachment in the control).

Thanks!

Accepted Solutions (0)

Answers (1)

Answers (1)

bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Likes

Without setting up a test app your looping rule looks ok assuming your CreateRelatedMedia properly defines the ParentLink and optionally the Properties.  One thing you could try is to make the media an array with just the one object.  Perhaps the client always expecting an array and the error is saying since you sent in a single object it can't loop over it. 

I will have to try and setup a test to replicate it sometime next week when I have some free time.