Need a recipe for fileupload. Store uploaded file in documents folder and the document Id to be stored in DB table.

 Need a recipe for fileupload. Store uploaded file in documents folder and the document Id to be stored in DB table. To facilitate an API GET type, to retrieve uploaded document with document Id.

Please an example recipe. I have tried using fileUpload component and used a!writetoDataStoreEntity() as follows and it is throwing an error stating the valueToStore cannot be null, when trying to upload a file.

Error for the below code is:

Could not display interface. Please check definition and inputs.

Interface Definition: Expression evaluation error: An error occurred while executing a smart service: An error occurred while trying to convert the given data to the type of the specified entity “Technologies” [id=db1da466-b3a3-4e93-bd4a-81566d516b20@4053, type=Technologies (id=2670)] (data store: Radar_DataStore). Value that could not be converted: ActivityClassParameter[name=DynamicInputs,it=3,v=[Document:4089]] Details: Could not cast from Text to Technologies. Details: CastInvalid (APNX-1-4400-000) This error will not be displayed to the user viewing this form. Click here to dismiss the error message and see the resulting interface.

 

Need to store only Document Id on DB not the image itself. Image should be stored in folder configured on the fileUpload component.

 

=load(
a!gridLayout(
label: "",
instructions: "This is a grid layout",
headerCells: {
a!gridLayoutHeaderCell(label: "Image")
},
rows: {
a!gridRowLayout(
contents: {
a!fileUploadField(
helptooltip: "Upload Technology image Files",
target: cons!ImagesFolder_Constant,
value: ri!Image.Image,
/*error("Unable to Store Uploaded Image")*/
saveInto: a!save(ri!Image.Image, save!value)
)
}
)
}
)
)

  Discussion posts and replies are publicly visible

  • Hi Anusha,

    Please try and use a submit button for document upload. Every uploaded document first gets added to temporary repository with temp document id.
    My advise is:
    Create a submit button > Load the same page upon submission > It shall store the documents in desired folder and share the document ids. > On load call and API to save data into database.
    Hope it helps!
  • Hi anushas,

    Here is the sample code :
    a!fileUploadField(
    label: "Upload Certificates",
    target: cons!TEST_FILE_UPLOAD,
    maxSelections: 10,
    value: ri!Files.fileId,
    saveInto: {
    ri!Files.fileId,
    a!save(
    ri!files.userName,
    concat(
    user(
    loggedInUser(),
    "firstName"
    ),
    " ",
    user(
    loggedInUser(),
    "lastName"
    )
    )
    )
    Thanks,
    ravalik
  • 0
    Certified Lead Developer
    Hi,

    Can you try the casting to the data(Example below) and make sure non-null properties of the particular table.
    a!writeToDataStoreEntity(cons!ENTITY,cast('type!{namespace}CDTName', {documentId:index(ri!Image,"documentId","")}),"Success","")
  • Tried some changes with below code:
    a!formLayout(
    label: "Form",
    contents: {
    a!textField(
    label:"Technology Name",
    value: ri!Technologies.Name,
    saveInto: a!save(ri!Technologies.Name, save!value)
    ),
    a!integerField(
    label: "Opportunity Rating",
    value: ri!Technologies.OpportunityRating,
    saveInto: a!save(ri!Technologies.OpportunityRating, save!value)
    ),
    a!integerField(
    label: "Risk Rating",
    value: ri!Technologies.RiskRating,
    saveInto: a!save(ri!Technologies.RiskRating, save!value)
    ),
    a!textField(
    label: "Years To Impact",
    value: ri!Technologies.YearsToImpact,
    saveInto: a!save(ri!Technologies.YearsToImpact, save!value)
    ),
    a!textField(
    label: "Impact",
    value: ri!Technologies.Impact,
    saveInto: a!save(ri!Technologies.Impact, save!value)
    ),
    a!fileUploadField(
    label: "Images",
    value: ri!Technologies.Images,
    saveInto: a!save(ri!Technologies.Images, save!value)
    )
    },
    buttons: a!buttonLayout(
    primaryButtons: {
    a!buttonWidgetSubmit(
    label: "Submit",
    saveInto: a!writeToDataStoreEntity(cons!DataStore_Technologies_Constants,
    ri!Technologies,
    "Success",
    ""),
    submit: true,
    style: "PRIMARY"
    )
    }
    )
    )

    This up clicking submit button is not doing anything and not showing any error. Document not uploaded and content not saved to DB. Anything wrong with the code please?

    Noticed that [Document: 1234] is the format of document info being passed into an array of text column in DB. But, for an Array of Text we need something to be in the format { "1234" } with just document number in strong form, how to achieve this? what is best possible way of storing multiple document numbers as an array in a column for each record?

  • you can have a workaround.as store all/multiple documents in local . then on click of button submit, you can have a sub rule which will extract the required id from array. and while saving into main cdt apply/provide loop with that sub rule. ex, sub rule may be like /*extract(local!Document,"[Document:","]")*/
  • 0
    A Score Level 2
    in reply to anushas0002
    Hi Anushas,
    What is the data type of the ri!Technologies.Images? you can type cast the value into toInteger and then store it to ri!Technologies.Images
  • It is integer array. It is saving now as expected. Thank You
  • Hi Anushas,
    What was the final solution that resulted in "It is saving now as expected."?
  • i had to convert to integer explicitly using toInteger() however, that version of environment is quite old. If your environment is latest, then you don't need to store document id as an integer in CDT/DB instead can have its type as Document directly in the CDT.