cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CAP: req.error inside bound action not highlighting target in Fiori Elements Object Page

0 Kudos
201

Hi guys,

I have a bound action in my sap cap project where I check for mandatory fields and throw errors highlighting the targets in the UI, in a Fiori Elements Object page. 

        if (mandatoryFields.length) {
            for (const field of mandatoryFields) {
                if (!item[field.mandatoryField]) {
                    req.error({
                            message: `The field ${field.description} is mandatory`,
                            target: `in/${field.mandatoryField}`,
                            code: 400,
                        });
                    }
            };
        };

 My problem here is that it is showing the error in the UI in a MessageBox but not highlighting the targets. I have the exact same code inside a this.on("SAVE") action and it works there, but somehow it doesn't inside a bound action.

The only solution I found is returning the req.error, but then it only shows it one by one and feels very clumsy.

I would appreciate any help with this issue. Thanks!

 

0 Kudos

Finally I came to conclusion that you cannot highlight errors when youre not in edit mode.
So I have to create this workaround:

const isActiveEntity = req.params[0].IsActiveEntity;

if (mandatoryFields.length) {
            if (!isActiveEntity) {
                for (const field of mandatoryFields) {
                    if (!item[field.mandatoryField]) {
                        req.error({
                            message: `The field ${field.description} is mandatory`,
                            target: `in/${field.mandatoryField}`,
                            code: 400,
                        });
                    }
                };
            } else {
                let mandatoryErrors = "";
                for (const field of mandatoryFields) {
                    if (!item[field.mandatoryField]) {
                        mandatoryErrors += `The field ${field.description} is mandatory` + "\n";
                    };
                };
                req.error({
                    message: mandatoryErrors,
                    code: 400
                });
            }
        };

 This works perfectly but cant highlight the targets unfortunately.

Accepted Solutions (0)

Answers (1)

Answers (1)

MattBrightman
Explorer
0 Kudos

 

A few thoughts:

1. Are you 100% sure that the req.error calls occurs? i.e. Your logic isn't preventing it from getting invoked? 

2. Does this syntax work? 

req.error(400,`The field ${field.description} is mandatory`, `in/${field.mandatoryField}`)

 

 

0 Kudos

Thanks for replying Matt.

Nevermind the original question, it was doing it all good. But it seems it only highlight errors when you are in edit mode in the Object Page, and Im triggering the action  when Im not editing, so for that reason I thought it was not working.