Skip to main content

Notifications

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!

  • Community Member Profile Picture
    on at
    RE: What's best practice for accessing the current record / fields that runs a Codeunit?

    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);

  • Community Member Profile Picture
    on at
    RE: What's best practice for accessing the current record / fields that runs a Codeunit?

    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?

  • Verified answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    13,695 Moderator on at
    RE: What's best practice for accessing the current record / fields that runs a Codeunit?

    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
    RE: What's best practice for accessing the current record / fields that runs a Codeunit?

    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.

  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    13,695 Moderator on at
    RE: What's best practice for accessing the current record / fields that runs a Codeunit?

    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;

    }

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans