Skip to main content
Post a question

Notifications

Community site session details

Community site session details

Session Id : 8y9K5teQYE043mZqvgU0K5
Small and medium business | Business Central, N...
Suggested answer

Cannot process all sales lines to create job task & planning lines

Like (7) ShareShare
ReportReport
Posted on 28 Mar 2025 14:13:38 by 152
Hello , I am trying to creating new project from sales order and taking the old job no. from item's card in the sales lines and then take that old job no.'s task lines and planning lines to create a new project's task and planning lines from action , but it is only taking the last sales line which has item with old job no. field with value in it consideration, I don't understand how to process all the sales line , I have used loop also , still it's not working , plz help me with this one...
 
    Procedure CreateJobsfromSalesOrder(Rec: Record "Sales Header")
    var
        oldjob: Record Job;
        oldjobtask: Record "Job Task";
        oldjobplanning: Record "Job Planning Line";
        salesline: Record "Sales Line";
        newjob: Record Job;
        newjobtask: Record "Job Task";
        newjobplanningline: Record "Job Planning Line";
        lineNo: Integer;
        defaultdim: Record "Default Dimension";
        jobtaskdim: Record "Job Task Dimension";
        dimsetentry: Record "Dimension Set Entry";
        Jobexists: Boolean;
        rCustomer: Record Customer;
        ritem: Record Item;
        JobSetup: Record "Jobs Setup";
        NoSeriesMgt: Codeunit NoSeriesManagement;
        DuplicateNumber: Text;
    begin
        // Check if Sales Order has a customer
        if Rec."Sell-to Customer No." = '' then
            Error('Sales Order must have a customer.');
 
        // Check if a job with this number already exists
        newJob.Reset();
        newJob.SetRange("No.", Rec."No.");
        JobExists := newJob.FindFirst();
 
        if JobExists then begin
            // Show warning message that project already exists
            Message('Warning: Project %1 already exists.', Rec."No.");
            if not Confirm('Do you still want to create a new project?', false) then
                exit;
 
            // Create a new job with a different number
            newJob.Init();
            //newJob."No." := Format(Rec."No." + '-1'); // Append '-1' to the job number
            newJob."No." := GenerateDuplicateJobNumber(Rec."No."); // Call the new procedure
            newJob.Validate("Bill-to Customer No.", Rec."Bill-to Customer No.");
            //newJob.Validate(Description, 'Project for Sales Order ' + Rec."No." + ' (Duplicate)');
            DuplicateNumber := CopyStr(newJob."No.", StrLen(Rec."No.") + 2); // Extract the part after "Sales Order No.-"
            newJob.Validate(Description, 'Project for Sales Order ' + Rec."No." + ' (Duplicate ' + DuplicateNumber + ')');
            newJob.Insert(true);
 
            // Copy dimensions from Sales Order to Job
            CopyDimensionsToJob(newJob, Rec);
        end else begin
            // Create a new job
            newJob.Init();
            newJob."No. Series" := '';
            newJob."No." := Rec."No."; // Use Sales Order number as Job number
            newJob.Validate("Bill-to Customer No.", Rec."Bill-to Customer No.");
            newJob.Validate(Description, 'Project for Sales Order ' + Rec."No.");
            newJob.Insert(true);
 
            // Copy dimensions from Sales Order to Job
            CopyDimensionsToJob(newJob, Rec);
        end;
 
        // Process all sales lines to create job tasks and planning lines
        SalesLine.Reset();
        SalesLine.SetRange("Document Type", SalesLine."Document Type"::Order);
        SalesLine.SetRange("Document No.", Rec."No.");
        SalesLine.SetFilter(Type, '<>%1', SalesLine.Type::" ");
        SalesLine.SetFilter(Quantity, '<>0');
 
        if SalesLine.FindSet() then begin
            repeat
                // Check if the item has a project template
                if SalesLine.Type = SalesLine.Type::Item then begin
                    ritem.Reset();
                    if ritem.Get(SalesLine."No.") then begin
                        if ritem."old job no." <> '' then begin
                            oldjob.Reset();
                            if oldjob.Get(ritem."old job no.") then begin
                                // Copy job tasks and planning lines from template
                                CopyJobTasks(oldjob, newJob);
 
                                // Copy dimensions to job tasks
                                CopyDimensionsToJobTasks(newJob, Rec);
 
                                //to create job planning lines
                                CopyJobPlanningLines(oldjob, newJob);
                            end;
                        end;
                    end;
                end;
            until SalesLine.Next() = 0;
            // Show confirmation message
            Message('Project %1 has been created for S/O %2.', newJob."No.", Rec."No.");
        end else
            Message('No valid sales lines found to create project tasks.');
    end;
  • Suggested answer
    Jainam M. Kothari Profile Picture
    3,170 on 31 Mar 2025 at 04:41:41
    Cannot process all sales lines to create job task & planning lines
    Hello,
     
    You can replicate the transaction in UAT or Sandbox Environment and Debug from there and can find the issue.
  • Suggested answer
    YUN ZHU Profile Picture
    79,743 Super User 2025 Season 1 on 30 Mar 2025 at 23:53:28
    Cannot process all sales lines to create job task & planning lines
    You can debug this code to see where it loops only once or the standard method only creates the last one.
     
    Hope this can give you some hints.
    Thanks.
    ZHU
  • Suggested answer
    Ramesh Kumar Profile Picture
    1,991 on 29 Mar 2025 at 18:46:42
    Cannot process all sales lines to create job task & planning lines
    Hi,
     
    Please perform a change test in UAT to see if you can replicate the issue. Also, check whether the project is already archived. If it's not, kindly unarchive all the versions and try again.
     
     
    Thanks
    Ramesh
     
    If this was helpful, please check the "Does this answer your question?" box and mark it as verified.
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    12,111 Super User 2025 Season 1 on 29 Mar 2025 at 05:49:02
    Cannot process all sales lines to create job task & planning lines

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,333 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,262 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete