Copy fields to a form

supp85supp85 Member Posts: 76
Hello,

can anyone help me please, I have created a command button and within the onPush trigger i have inserted the following code to copy across certain fields from the sales order to another form (build schedule) however it doesn't seem to be working it creates a new record within the new form but all the fields are blank:

SalesHeader.SETRANGE("Document Type", "Document Type"::Order);
SalesHeader.SETRANGE("No.", SalesLine."Document No.");
SalesLine.SETRANGE(SalesLine.Type, SalesLine.Type::ITEM);

IF (SalesLine.FIND('-')) AND (SalesLine."To Build" = TRUE) THEN BEGIN
BuildSchHead.INIT;
SalesHeader."No." := BuildSchHead."Sales Order No.";
SalesHeader."Bill-to Name" := BuildSchHead."Customer Name";
REPEAT
BuildSchLine."Item No." := SalesLine."No.";
BuildSchLine.Description := SalesLine.Description;
BuildSchLine.Qty := SalesLine.Quantity;
BuildSchHead.INSERT(TRUE);
UNTIL BuildSchLine.NEXT = 0;
END;

MESSAGE('Build Schedule %1 has been created',BuildSchHead."No.");

Any ideas please?

Comments

  • ssinglassingla Member Posts: 2,973
    Put BuildSchHead.INSERT before END command.
    CA Sandeep Singla
    http://ssdynamics.co.in
  • supp85supp85 Member Posts: 76
    Thank you for your reply Sandeep, but it didn't work.
  • garakgarak Member Posts: 3,263
    any ideas? Yes, we change your source ;-)

    This is your old source:
    SalesHeader.SETRANGE("Document Type", "Document Type"::Order);
    SalesHeader.SETRANGE("No.", SalesLine."Document No.");
    SalesLine.SETRANGE(SalesLine.Type, SalesLine.Type::ITEM);
    
    IF (SalesLine.FIND('-')) AND (SalesLine."To Build" = TRUE) THEN BEGIN
    BuildSchHead.INIT;
    SalesHeader."No." := BuildSchHead."Sales Order No.";
    SalesHeader."Bill-to Name" := BuildSchHead."Customer Name";
    REPEAT
    BuildSchLine."Item No." := SalesLine."No.";
    BuildSchLine.Description := SalesLine.Description;
    BuildSchLine.Qty := SalesLine.Quantity;
    BuildSchHead.INSERT(TRUE);
    UNTIL BuildSchLine.NEXT = 0;
    END;
    

    This is the new source (i hope the Button is on the Sales Order Form and not in the subform):
    SalesLine.reset;
    SalesLine.setrange("Document Type",Rec."Document Type");
    SalesLine.setrange("Document no.",Rec."No.");
    SalesLine.SETRANGE(Type,SalesLine.Type::ITEM);
    SalesLine.SETfilter("No.",'<>''''');
    SalesLine.setrange("To Build",true);
    if SalesLine.findset then begin
    
      if not BuildSchHead.get(PRIMARY KEY VALUES) then begin
        BuildSchHead.INIT;
        BuildSchHead."Sales Order No." := Rec."No.";
        BuildSchHead."Customer Name" := "Bill-to Name";
        //here the other field in BuildSchHead table
        BuildSchHead.insert;
      end; //what is, if the Rec exist? a modify or what?
    
      repeat
        if not BuildSchLine.get(PRIMARY KEY VALUES) then begin
          BuildSchLine.init;
          BuildSchLine."Primary key Fields" := "Primary KEY VALUES"
          BuildSchLine."Item No." := SalesLine."No.";
          BuildSchLine.Description := SalesLine.Description;
          BuildSchLine.Qty := SalesLine.Quantity;
          BuildSchLine.INSERT(TRUE); //the true only if you have soure in the Insert Trigger
        end; //what is, if the Rec exist? a modify or what?
      until SalesLine.next = 0;
    end;
    

    Regards
    Do you make it right, it works too!
  • supp85supp85 Member Posts: 76
    Thank you very much Garak that worked great I took your Modify comments on board and put code in to cater for existing records. Thanks again
  • garakgarak Member Posts: 3,263
    no problem and welcome
    Do you make it right, it works too!
  • supp85supp85 Member Posts: 76
    Hello again,

    I seem to be having problems with getting the record to modify of it exists, the code i have added is as follows;
    IF SalesLine."To Build" = FALSE THEN
      ERROR('Unable to create Build Schedule please ensure all fields are correctly entered')
    
    IF SalesLine.FINDSET THEN BEGIN
      IF NOT BuildSchHead.GET("No.") THEN BEGIN
        BuildSchHead.INIT;
        BuildSchHead."Sales Order No." := Rec."No.";
        BuildSchHead."Customer Name" := "Bill-to Name";
        BuildSchHead.INSERT(TRUE);
      END ELSE
         IF NOT BuildSchHead.INSERT THEN                                            
           BuildSchHead."Customer Name" := "Bill-to Name";
           BuildSchHead.MODIFY(TRUE);
    
      REPEAT
        IF NOT BuildSchLine.GET('Build No.','Item No.') THEN BEGIN
          BuildSchLine.INIT;
          BuildSchLine."Build No." := BuildSchHead."No.";
          BuildSchLine."Item No." := SalesLine."No.";
          BuildSchLine.Description := SalesLine.Description;
          BuildSchLine.Qty := SalesLine.Quantity;
          BuildSchLine.INSERT(TRUE);
        END ELSE
          IF NOT BuildSchLine.INSERT THEN
             BuildSchLine."Item No." := SalesLine."No.";
             BuildSchLine.Description := SalesLine.Description;
             BuildSchLine.Qty := SalesLine.Quantity;
             BuildSchLine.MODIFY(TRUE);
      UNTIL SalesLine.NEXT = 0;
    END;
    
    MESSAGE('Build Schedule %1 has been created',BuildSchHead."No.");
    
    I have also tried IF NOT INSERT THEN MODIFY, but that does not work either?
  • BeliasBelias Member Posts: 2,998
    IF NOT BuildSchLine.INSERT THEN    //////////////////SHOULD THERE BE A "BEGIN" HERE?
             BuildSchLine."Item No." := SalesLine."No.";
             BuildSchLine.Description := SalesLine.Description;
             BuildSchLine.Qty := SalesLine.Quantity;
             BuildSchLine.MODIFY(TRUE);
    

    see the comment, i don't know if this is the problem, as i didn't read carefully your code, but it is potentially a serious bug, if i'm right
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • supp85supp85 Member Posts: 76
    Thank you for your reply, i have tired it but still not working
  • BeliasBelias Member Posts: 2,998
    there's another error like the one i posted before in your code...have you already corrected it?
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • supp85supp85 Member Posts: 76
    Yes that has been corrected.
  • BeliasBelias Member Posts: 2,998
    :shock: what the hell is this?!?!
    IF NOT BuildSchLine.GET('Build No.','Item No.') THEN BEGIN
    
    you are doing a GET with 'Build No.' and 'Item No.' strings. i mean, these strings AS IS
    have you really got a line that have "build no." = 'build no." and "item no." = 'item no.'?! :shock:
    i don't think so, maybe you want to do
    IF NOT BuildSchLine.GET("Build No.","Item No.") THEN BEGIN
    
    anyway, i don't think this will make your application work correctly, there are some other problems in the code.
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • BeliasBelias Member Posts: 2,998
    SalesLine.reset;
    SalesLine.setrange("Document Type",Rec."Document Type");
    SalesLine.setrange("Document no.",Rec."No.");
    SalesLine.SETRANGE(Type,SalesLine.Type::ITEM);
    SalesLine.SETfilter("No.",'<>''''');
    SalesLine.setrange("To Build",true);
    IF SalesLine.FINDSET THEN BEGIN
      IF NOT BuildSchHead.GET("No.") THEN BEGIN
        BuildSchHead.INIT;
        BuildSchHead."Sales Order No." := Rec."No.";
        BuildSchHead."Customer Name" := "Bill-to Name";
        BuildSchHead.INSERT(TRUE);
      END ELSE begin
    //    IF NOT BuildSchHead.INSERT THEN begin        //USELESS, as you already tested that the record exists
        BuildSchHead."Customer Name" := "Bill-to Name";
        BuildSchHead.MODIFY(TRUE);
      end;
    
      REPEAT
        IF NOT BuildSchLine.GET(BuildSchHead."No.",SalesLine."No.") THEN BEGIN 
          BuildSchLine.INIT; 
          BuildSchLine."Build No." := BuildSchHead."No.";
          BuildSchLine."Item No." := SalesLine."No.";
          BuildSchLine.Description := SalesLine.Description;
          BuildSchLine.Qty := SalesLine.Quantity;
          BuildSchLine.INSERT(TRUE);
        END ELSE begin
    //      IF NOT BuildSchLine.INSERT THEN   //USELESS, see above
          BuildSchLine."Item No." := SalesLine."No.";
          BuildSchLine.Description := SalesLine.Description;
          BuildSchLine.Qty := SalesLine.Quantity;
          BuildSchLine.MODIFY(TRUE);
        end;
      UNTIL SalesLine.NEXT = 0;
    END;
    
    MESSAGE('Build Schedule %1 has been created',BuildSchHead."No.");
    

    This should be correct, (what is the "rec"?) you should check the GET function in the online help :wink:
    hope to be helpful
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • supp85supp85 Member Posts: 76
    Thank you for your reply Belias however its still not working
  • BeliasBelias Member Posts: 2,998
    What is the "rec"? :-k in which dataitem are you writng this code?
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • supp85supp85 Member Posts: 76
    Rec is sales header, this code has been written within a command button
Sign In or Register to comment.