Validation for sum of percentage field to be not greater than 100

Dear all,

I created a new grid in the projtable form with the LOB field and Proeject id ,percentage in a separate table.Here for each project we add a multiple lob with the percentage value. Here i need to validate sum of percentage field for each project should not be greater than 100 . So while the user adds the percentage for a project with different LOB and if the percentage exeeds beyond 100 then it should not allow the user to save it…Please help me with a sample method and which place should i write exactly. Please let me know if the description is not clear.

Thanks …

Hi,

write your code in validatewrite or validatefield table method(Your newtable).

you should override validateWrite or validateField methods depending on the requirement and check the sum(percentage), throw an error if it exceeds 100. validateWrite will be triggered during the record save and validateField after entering a value in percentage field.

select sum(percentage) from yourTable; (use common field from the projTable and yourTable in the where clause)

Thanks for the respons.Please can you give me a sample method.I wrote the below method but it does not work.

public boolean validateWrite()
{
boolean ret;
CaeProjlob caeprojlob1;
ProjTable projtable1;
ret = super();

if (ret)
{
select sum(percentage) from caeprojlob1 where caeprojlob1.ProjId == projtable1.ProjId;

if (this.Percentage > 100)
{
ret = ret && checkFailed(“Sum of the percentage per project cannot be more than 100%”);
}
}
return ret;
}

It is not selected, use the available projTable buffer in the form, if it is at table level use this.projId

Hi,

I changed to this.projid since its a table.But still it was not working .Please suggest me with some sample method.

Thanks

if (this.Percentage > 100)
{

Instead use caeprojlob1.Percentage (as you have selected the sum by using this buffer)

Hi,

Due to relation CaeProjlob will be having projId.

And one more important thing.Current record is not inserted.System will not consider your current record percentage.You need to add current record percentage with select statement.Please find the below code.Hope it will be helpful.

public boolean validateWrite()
{
boolean ret;
CaeProjlob caeprojlob1;
ProjTable projtable1;

Real total;

ret = super();

if (ret)
{
select sum(percentage) from caeprojlob1 where caeprojlob1.ProjId == this.ProjId;

total = caeprojlob1.Percentage + this.Percentage;

if (total > 100)
{
ret = ret && checkFailed(“Sum of the percentage per project cannot be more than 100%”);
}
}
return ret;
}

Hi ,Thanks a lot for the sample.

If i add 1st line with 99 and 2nd line with 2 is not accepting.Thats correct logic.But after i delete 2nd line and modify the 1st line as 100 , it was throwing validation error instead saving the same.Might be it doesnt allow me to modify.

You should check for the recId value before you add the current line percentage to the sum(percentage)

if (this.RecId)

total = caeprojlob1.Percentage;

else

total = caeprojlob1.Percentage + this.Percentage;