Options

How to assign the same value to multiple records

Hi,
is there a way to update selected records all at once? I would like to edit unrelated records (with different value of primary key) at the same time. Is it achievable by the sheer use of a page?

Best Answer

  • Options
    ShaiHuludShaiHulud Member Posts: 228
    edited 2019-07-26 Answer ✓
    You can use a report to achieve this. Reports have a property ProcessingOnly. It removes the printing layout as well, so you don't need to worry about that.
    You would need to create a function inside the report to retrieve the selected records, something like
    SetTable ( newMyTable : Record "My Table")
    MyTable := newMyTable; //MyTable being a global Record variable of subtype "My Table"
    
    Then you would set up value (value1, value2, etc.) for the fields that you want to modify on the Report Request Page. Users will be able to write them in (don't forget to add TableRelation if needed!) and assign them to the MyTable records in the OnPostReport() trigger:
    OnPostReport()
    IF MyTable.FINDSET THEN BEGIN
      REPEAT
        MyTable.VALIDATE("Field Name 1", value1);
        MyTable.VALIDATE("Field Name 2", value2);
        MyTable.MODIFY(TRUE);
      UNTIL MyTable.NEXT = 0;
    END;
    

    And then, of course, change the Page Action code to call the Report's SetTable function first and then the report itself:
    <Action01> - OnAction()
    CurrPage.SETSELECTIONFILTER(MyTable);
    MyReport.SetTable(MyTable); //MyReport is a local variable of type Report and subtype "My Report"
    MyReport.RUNMODAL;
    

    Alternatively, on your processing report you could set up "My Table" as DataItem, and then instead of the function SetTable, use SETTABLEVIEW before calling the report to set the record. And then you would write the modification code not in OnPostReport, but under "My Table - OnAfterGetRecord()".

Answers

  • Options
    ShaiHuludShaiHulud Member Posts: 228
    Yes, it's doable. On the page you need to create a Page Action (button) and add code that does something along these lines:
    <Action01> - OnAction()
    //This will put the selected (marked/highlighted) records into MyTable record
    CurrPage.SETSELECTIONFILTER(MyTable); //MyTable here is the table which records you're displaying
    MyTable.MODIFYALL("Field Name", value, TRUE); //or FALSE, if you don't want MODIFY to run with TRUE parameter, default (if omitted) is FALSE
    

    Please note that this will NOT validate the fields! In order to validate them (run code on their OnValidate triggers), you need to loop through the records, so your code becomes something like this:
    <Action01> - OnAction()
    CurrPage.SETSELECTIONFILTER(MyTable); //MyTable here is the table which records you're displaying
    IF MyTable.FINDSET THEN BEGIN
      REPEAT
        MyTable.VALIDATE("Field Name", value);
        MyTable.MODIFY(TRUE); //or FALSE);
      UNTIL MyTable.NEXT = 0;
    END;
    
  • Options
    OrangePieOrangePie Member Posts: 11
    Thanks for the quick answer. It's very helpful, though it requires the programmer to assign values in code. Is it possible to put the user in charge of modifications? I have the idea that an action would open a new page and the user would assign some values to certain fields. After accepting it all of previously selected records would have the new values. Does it sound doable?
  • Options
    ResolusResolus Member Posts: 40
    It certainly is possible for a developer to create such pages. But you'd need to know in advance what fields you want to be changed so they can be added.

    Is it wise? depends what fields you want the user to change like that. (if a user is distracted and makes a typo or selects too many records)
  • Options
    OrangePieOrangePie Member Posts: 11
    So how should I do it? I'm at a loss to know what to do. I want the page to act as a form without connecting it to any table but it's not possible, is it. If you've any idea I'd be much grateful, it's been quite a conundrum for me to solve. I'm not asking for a ready solution, just some steps I could follow or suggestions since at the moment I've tried everything I could think of.
  • Options
    ShaiHuludShaiHulud Member Posts: 228
    edited 2019-07-26 Answer ✓
    You can use a report to achieve this. Reports have a property ProcessingOnly. It removes the printing layout as well, so you don't need to worry about that.
    You would need to create a function inside the report to retrieve the selected records, something like
    SetTable ( newMyTable : Record "My Table")
    MyTable := newMyTable; //MyTable being a global Record variable of subtype "My Table"
    
    Then you would set up value (value1, value2, etc.) for the fields that you want to modify on the Report Request Page. Users will be able to write them in (don't forget to add TableRelation if needed!) and assign them to the MyTable records in the OnPostReport() trigger:
    OnPostReport()
    IF MyTable.FINDSET THEN BEGIN
      REPEAT
        MyTable.VALIDATE("Field Name 1", value1);
        MyTable.VALIDATE("Field Name 2", value2);
        MyTable.MODIFY(TRUE);
      UNTIL MyTable.NEXT = 0;
    END;
    

    And then, of course, change the Page Action code to call the Report's SetTable function first and then the report itself:
    <Action01> - OnAction()
    CurrPage.SETSELECTIONFILTER(MyTable);
    MyReport.SetTable(MyTable); //MyReport is a local variable of type Report and subtype "My Report"
    MyReport.RUNMODAL;
    

    Alternatively, on your processing report you could set up "My Table" as DataItem, and then instead of the function SetTable, use SETTABLEVIEW before calling the report to set the record. And then you would write the modification code not in OnPostReport, but under "My Table - OnAfterGetRecord()".
Sign In or Register to comment.