web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

What's best practice for accessing the current record / fields that runs a Codeunit?

(0) ShareShare
ReportReport
Posted on by

I would like to create a Job record while on an Opportunity, and would like to copy some of the fields from the Opportunity record to the new Job record.

In the example below, I have created a new Codeunit - however, as you can tell, the description field will be blank.

codeunit 50100 OpportunityToJob
{
    trigger OnRun()
    begin
        newJob.INIT;
        newJob.Description := '';
        newJob.INSERT;
    end;

    var
        newJob: Record Job;
}

Now on the Opportunity card, I have created an action to run my Codeunt.

pageextension 50113 UpdateOpportunityCard extends "Opportunity Card"
{
    actions
    {
        addlast(reporting)
        {
            action(CreateQuotingJob)
            {
                Caption = 'Create Job';
                ApplicationArea = All;
                Image = Job;
                trigger OnAction();

                begin
                    OpportunityToJob.Run();
                end;
            }
        }
    }

    var
        OpportunityToJob: Codeunit OpportunityToJob;
}

My question is, how can I set the description field of the new Job to the Opportunities description value when running my codeunit? Is it possible to pass the entire record in as a parameter of OpportunityToJob?

Thanks!

I have the same question (0)
  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,306 Super User 2026 Season 1 on at

    You can create a new procedure with parameter inside that codeunit. Example below.

    codeunit 50100 OpportunityToJob

    {

       procedure createNewJob(Opportunity:Record "Opportunity")

       var

           newJob: Record Job;

       begin

           newJob.INIT;

           newJob.Description := Opportunity.Description;

           newJob.INSERT;

       end;

    }

    pageextension 50113 UpdateOpportunityCard extends "Opportunity Card"

    {

       actions

       {

           addlast(reporting)

           {

               action(CreateQuotingJob)

               {

                   Caption = 'Create Job';

                   ApplicationArea = All;

                   Image = Job;

                   trigger OnAction();

                   begin

                       OpportunityToJob.createNewJob(Opportunity);

                   end;

               }

           }

       }

       var

           OpportunityToJob: Codeunit OpportunityToJob;

    }

  • Community Member Profile Picture
    on at

    Thanks TeddyH  - that's getting me a lot closer. It doesn't seem to like OpportunityToJob.createNewJob(Opportunity) as the Opportunity parameter hasn't been declared anywhere?

    I'm also wondering how I can get access to the Job.No value after INSERT is ran, so I might set a RelatedJob field on the opportunity.

  • Verified answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,306 Super User 2026 Season 1 on at

    Sorry. It should be Rec because you are calling it from the Opportunity Card. You can setup Job as part of the parameter.

    codeunit 50100 OpportunityToJob

    {

      procedure createNewJob(Opportunity:Record "Opportunity"; var newJob: Record Job)

      begin

          newJob.INIT;

          newJob.Description := Opportunity.Description;

          newJob.INSERT;

      end;

    }

    pageextension 50113 UpdateOpportunityCard extends "Opportunity Card"

    {

      actions

      {

          addlast(reporting)

          {

              action(CreateQuotingJob)

              {

                  Caption = 'Create Job';

                  ApplicationArea = All;

                  Image = Job;

                  trigger OnAction();

                  var

                        OpportunityToJob: Codeunit OpportunityToJob;

                        NewJob: Record Job;

                  begin

                      OpportunityToJob.createNewJob(Rec,NewJob);

                  end;

              }

          }

      }

    }

  • Community Member Profile Picture
    on at

    Thanks! One last question - the new Job record always uses '' as it's No. value - I assumed it would use the next value in the Job No. Series. How do I set the new Job record to use the next number in series?

  • Community Member Profile Picture
    on at

    For anyone struggling as much as I was, I've solved the auto-numbering with

    NoSeriesMgt: Codeunit NoSeriesManagement;

    and

    newJob."No." := NoSeriesMgt.GetNextNo('J-JOB', 0D, true);

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,216 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,471 Super User 2026 Season 1

#3
AndrewThomas81 Profile Picture

AndrewThomas81 1,369

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans