To make fields mandatory on form

Hello Guys,

I have a problem about to make 2 fields to mandatory on form.

I wrote this basic code on dataTable’s active method but did not work:

I want to make if Recodttype=Policy then make mandatory these fiels:AmountDetails,currencycode

if its not(or recordType=Invoice) then do not make mandatory but even it is policy or no it makes mandatory.

Can you guys help me plase?

Public int active()
{
int ret;

;
ret = super();
/////////

if(InsurancePolicyTable.RecordType==RecordType::Policy)
{
InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,AmountDetails)).mandatory(true);
nsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,CurrencyCode)).mandatory(true);

}
else
{

InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,AmountDetails)).mandatory(false);
InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,CurrencyCode)).mandatory(false);
}
/////

dimensionDefaultingController.activated();
element.SetElements();

return ret;
}

What exactly happened? You don’t see any difference at all? Note that I struggle to understand the meaning of “even it is policy or no it makes mandatory”. It sounds to me that it’s always mandatory, but you’re saying that you’re trying to make it mandatory. If it was already mandatory, you would ask how to make it optional.
Did you test that your code is called as expected?
Doesn’t SetElements() contain something that overwrites your setup?

By the way, let me add indentation to your code and simplify it, so it’s easier to follow:

public int active()
{
	int ret = super();

	boolean mandatory = (InsurancePolicyTable.RecordType == RecordType::Policy);

	InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable, AmountDetails)).mandatory(mandatory);
	InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable, CurrencyCode)).mandatory(mandatory);

	dimensionDefaultingController.activated();
	element.SetElements();

	return ret;
}

I’ve also set the font of your question to the normal size.

hi Martin thanks for your respond,its always mandatory yes. if-else did not work it. when i create a new record ,its always mandatory so,when i tried to save the new record for RecordType::Invoice its not making mandatory false.

I want like if recordtype ::policy then make mandatory if its recordtype:invoice then make not mandatory these felds(Amountdetails,currencycode)

when i debug it seems normal but without debug on form,when i create a record it does not work.

if(InsurancePolicyTable.RecordType==RecordType::Policy)
{
InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,AmountDetails)).mandatory(true);
nsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,CurrencyCode)).mandatory(true);

}
else
{

InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,AmountDetails)).mandatory(false);
InsurancePolicyTable_DS.object(fieldNum(InsurancePolicyTable,CurrencyCode)).mandatory(false);

i wrote under super() on databese’s active method? is it wrong?

If it’s always mandatory, isn’t it defined as mandatory at the table? If so, you can’t remove the mandatory flag in a form.

I had done this on the modification of datasource’s field like this and its working.

public void modified()
{
super();

if(Table2.BaseEnum1 == BaseEnum1::Element2)
{

Table2_Field1.mandatory(true);

}
else if(Table2.BaseEnum1 == BaseEnum1::Element1)
{

Table2_Field1.mandatory(false);

}

}

hi martin i figured out the problem and solved i think .thanks for your helps

my code is not wrong but I wrote it on wrong place,but i figured out and solved.thank you so much for your respond

If you mean that you removed it from active(), I don’t think it’ll work correctly.

If you mean that it worked on active but not when you change the value, you should have told us. “It’s not working” can mean million things. If you know have some identical code in active() and modified(), don’t forget to refactor it to remove duplicity.

I see you miss my advice how to paste code so it preserve indentation. Let me explain it once more. Below the text box where you type replies are several drop-down boxes. One of them is Insert, with an option Insert Code. This is what you should use. If you share code with others, you surely want to give it something they can read, otherwise what’s the point?