Announcement

Collapse
No announcement yet.

Setting a field value based on the Sequence

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting a field value based on the Sequence

    Setting a field value based on the Sequence
    Introduction
    You can set a field's value based on the current sequence value of a record by using the Data Entry Screen's onLoad method. To keep incorrect values from being inserted, use both the onLoad and onUpdate methods. This process can become tricky, but the following example works well.

    Information that is needed:
    1. Current Sequence value
    2. Total number of sequences that currently exist for the given page (barcode)
    3. Maximum number of sequences allowed
    4. Current command that has been issued to update the record

    Behind the scenes, the system finds most of this information in the recordInformationList. To access this, you need to create an object identifier within the method code (onLoad and onUpdate) that references the screens information list and then the particular attributes are queried as follows:

    Code:
    ** Get record information object **;
    dcl object f info;
    f=_self_.frameid;
    f._getNumVar('recordInfo',info);
    
    ** declare local variables for current sequence, max sequence and current total **;
    dcl num curseq = info.currentSeqValue;
    dcl num currentTotalSeq = listlen(info.recordSequenceList);
    dcl num maxSequence = 3;
    
    ** Get the current Command issued **;
    dcl char mycmd = word(1);
    The onLoad method will populate the required fields based on the Sequence value, however, some error checking should be done to limit incorrect information from being updated.
    • Check that the maximum number of expected sequences will not be exceeded by the user inserting a new record.
    • Check that the user has not been able to add a sequence greater than that allowed.
    • Update the required field based on the current sequence value.
    • Check for a mismatch in values based on the sequence value and warn the user. This could happen if the user deletes one of the sequenced records that is not the last.


    In the onUpdate method, error checking will also be done to limit what the user can update or add.
    • On the last allowed sequence value, only allow the user to update the record by using the UPDATE (F9) command. If they attempt to insert or add a sequence, issue a message and error a field so they cannot continue.
    • Check that the maximum number of expected sequences is not exceeded by the user inserting a record.


    Example:
    The following example method code will update and error check the text field VAR1. In this example, a max of 3 sequences are allowed and default values for each sequence are 1 = ONE, 2 = TWO and 3 = THREE.

    OnLoad Method

    Code:
    /** onLoad Method **/
    %include objfile;
    
    _onLoad: method _self_:u:object;
    link createobj;
    ************************;
    ** Add user code here **;
    ************************;
    
    ** Get identifier for record information object **;
    dcl object f info;
    f=_self_.frameid;
    f._getNumVar('recordInfo',info);
    
    ** declare local variables for current sequence, max sequence and current total **;
    dcl num curseq = info.currentSeqValue;
    dcl num maxSequence = 3;
    dcl num currentTotalSeq = listlen(info.recordSequenceList);
    
    ** Get the current Command issued **;
    dcl char mycmd = word(1);
    
    
    ** Make sure that the max seq has not been exceeded by inserting a record **;
    if mycmd='INSERTSEQ' and currentTotalSeq>=maxSequence then do;
        rcc=messagebox({'You cannot insert a sequence.','Maximum sequence value will be exceeded.'}); rcc=rcc;
        ** Return to previous sequence **;
        call execcmd('SEQUENCELEFT');
        return;
    end;
    ** Check if user added a sequence greater that allowed and go back to previous seq **;
    if curseq > maxSequence then do;
        rcc=messagebox({'You cannot add a sequence value greater than max.'}); rcc=rcc;
        ** Return to previous sequence **;
        call execcmd('SEQUENCELEFT');
        return;
    end;
    
    ** if above checks passed **;
    ** Populate field based on current seq value only if not already completed **;
    if var1.text = '' then do;
        select curseq;
            when(1) var1.text='ONE';
            when(2) var1.text='TWO';
            when(3) var1.text='THREE';
            otherwise;
        end;
    end;
    
    ** Otherwise check that current value does matches what it should for the sequence **;
    else if var1.text > '' then do;
        select curseq;
            when(1) do;
                if var1.text ^= 'ONE' then do;
                    rcc=messagebox({'Value for FIELDNAME does not match expected value of ONE.'}); rcc=rcc;
                end;
            end;
            when(2) do;
                if var1.text ^= 'TWO' then do;
                    rcc=messagebox({'Value for FIELDNAME does not match expected value of TWO.'}); rcc=rcc;
                end;
            end;
            when(3) do;
                if var1.text ^= 'THREE' then do;
                    rcc=messagebox({'Value for FIELDNAME does not match expected value of THREE.'}); rcc=rcc;
                end;
            end;
            otherwise;
        end;
    end;
    
    
    endmethod;
    OnUpdate Method

    Code:
    /** onUpdate Method **/
    
    %include objfile;
    
    _onUpdate: method _self_:u:object;
    link createobj;
    ************************;
    ** Add user code here **;
    ************************;
    
    ** Get record information object **;
    dcl object f info;
    f=_self_.frameid;
    f._getNumVar('recordInfo',info);
    
    ** declare local variables for current sequence, max sequence and current total **;
    dcl num curseq = info.currentSeqValue;
    dcl num maxSequence = 3;
    dcl num currentTotalSeq = listlen(info.recordSequenceList);
    
    ** Get the current Command issued **;
    dcl char mycmd = word(1);
    
    ** Don't allow insert of sequence if on maximum value **;
    if mycmd ^="UPDATE" then do;
        if curseq = maxSequence then do;
            rcc=messagebox({'Current sequence value is the max.', 'Please use F9 to Update record instead of F4.'}); rcc=rcc;
            ** Error back on a field so that record cannot be updated **;
            var1._errorOn();
            return;
        end;
    end;
    
    ** protect insertion of a sequence in the middle if the maximum has already been reached. **;
    if ((int(curseq)^=curseq) and currentTotalSeq >=maxSequence) or
        (curseq > maxSequence) then do;
            rcc=messagebox({'This record cannot be inserted because','the maximum allowed will be exceeded'});
            rcc=rcc;
            ** Error back on a field so that record cannot be updated **;
            var1._errorOn();
            return;
    end;
    
    
    endmethod;
    Attached Files
    Last edited by Ronni Rubenstein; 04-01-2009, 10:05 AM.
Working...
X