Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

(0) ShareShare
ReportReport
Posted on by 607

Hi All,

I am quite new to Dynamics 365 BC. I need to know that when a Sales Quote is created manually, a specific Dimension must be set to that Sales Quote automatically. 

Can someone please help me how can i achieve this through AL code? Please help!

Thanks in advance!

  • AXBOY Profile Picture
    607 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    Got it. Thank you Raju!:)

  • Verified answer
    Raju Gudipalli Profile Picture
    185 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    Modify trigger will execute  for all the fields excluding primary key fields

    In your case dimensions will update when you change customer no. field.

    So it is not required to trigger the code for all the fields.

  • AXBOY Profile Picture
    607 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    Thank you so much Raju for the help! Just one last thing. Why should we perform this process on OnAfterValidate event and not on OnModify event?

  • Verified answer
    Raju Gudipalli Profile Picture
    185 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    To set record variable as temporary

    Below is the example

    Dimensionsetentrytemp: Record "Dimension Set Entry" Temporary;

    Else it will insert directly into table.

    Create new codeunit and add this subscriber

    [EventSubscriber(ObjectType::Table, Database::"Sales Header", 'OnaftervalidateEvent', 'Sell-to Customer No.', false, false)]

      local procedure OnafterValidateSellToCustomerNo(var Rec: Record "Sales Header";var xrec :Record "Sales Header")

    Var

    DimensionSetEntry: record ”dimension set entry";

    Dimensionsetentrytemp: Record "Dimension Set Entry" Temporary;

    DimMgmt : codeunit dimensionmanagement;

    ProjectDimVal : code[10];

    DimCode  : code[10];

    Begin

    Dimensionsetentrytemp.deleteall();//This will clear the temptable(table will be empty)

           DimensionSetEntry.Get( xRec."Dimension Set ID", 'Project');

           ProjectDimVal := DimensionSetEntry."Dimension Value Code";

           DimCode := 'Project';

    DimensionSetEntry.reset();       DimensionSetEntry.SetRange("Dimension set id", rec."dimension set id");

    DimensionSetEntry.setfilter("Dimension Code" ,'<>%1', DimCode);

           if DimensionSetEntry.findset() then

               repeat

                DimensionValue.get(DimensionSetEntry."Dimension code",DimensionSetEntry."Dimension value code");

                       DimensionSetEntryTmp.Init();

                       DimensionSetEntryTmp.Validate("Dimension Code", DimensionSetEntry."Dimension Code");

                       DimensionSetEntryTmp.Validate("Dimension Value Code",  DimensionSetEntry."Dimension Value Code");

                       DimensionSetEntryTmp."Dimension Value ID" := DimensionValue."Dimension Value ID";

                  DimensionSetEntryTmp.Insert();

               until DimensionSetEntry.Next = 0;

           DimensionValue.Get('Project', ProjectDimVal);

           DimensionSetEntryTmp.Init();

           DimensionSetEntryTmp.Validate("Dimension Code",DimensionValue."Dimension code" );

           DimensionSetEntryTmp.Validate("Dimension Value Code", ProjectDimVal);

           DimensionSetEntryTmp."Dimension Value ID" := DimensionValue."Dimension Value ID";

           DimensionSetEntryTmp.Insert();

           Rec."Dimension Set ID" := DimMgmt.GetDimensionSetID(DimensionSetEntryTmp);

           Rec.Modify();

    End;

  • AXBOY Profile Picture
    607 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    Hi Raju, i dident your temp table part .Can you pls explain that in detail if possible?

  • Verified answer
    Raju Gudipalli Profile Picture
    185 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    I see that you are looping with default dimensions if default dimension table have value for project code then it will insert a record there and another project dimension after the loop.

    Deleteall dimensionsetentrytemp at the starting

    After that insted direct insert into temp table check the condition on temptable if it is not having record with dimension code.

    Hope this will help you.

  • AXBOY Profile Picture
    607 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    Hi Sir,

    Thank you sir. It worked. But like mentioned my new requirement is :

    After creating a Sales Quote against a Customer A, the standard manually populates all the Dimensions of the customer to the Sales Quote automatically.

    Now when i change the Customer to Customer B: I have done the customization that i am fetching dimensions  which is attached to the customer B, and for a particular Dimension Code , im modifying the Dimension Code Value . And using dimension management codeunit, im able to find any existing or creating new DimSetId. And Finally populating the new DImensions on the Sales Quote. Till here i have succesfully reached.

    But when i am changing the Customer B to back to A , im getting this error :

    pastedimage1576053778783v1.png

    My Code is : 

    tableextension 50001 WfsSalesHeaderExt extends "Sales Header"
    {
    trigger OnModify()
        var
            myInt: Integer;
        begin
            OldSetID := xRec."Dimension Set ID";
            NewCustomer := Rec."Sell-to Customer No.";
            //DimensionSetEntry.SetFilter("Dimension Set ID", 'OldSetID');
            // DimensionSetEntry.SetFilter("Dimension Code", 'Project');
            DimensionSetEntry.Get(OldSetID, 'Project');
            ProjectDimVal := DimensionSetEntry."Dimension Value Code";
            DimCode := 'Project';
            DefaultDimVal.SetRange("No.", NewCustomer);
            if (DefaultDimVal.FindFirst()) then begin
                repeat
                    DimensionValue.Get(DefaultDimVal."Dimension Code", DefaultDimVal."Dimension Value Code");
                    if (DefaultDimVal."Dimension Code" <> DimCodethen begin
                        DimensionSetEntryTmp.Init();
                        DimensionSetEntryTmp.Validate("Dimension Code", DefaultDimVal."Dimension Code");
                        DimensionSetEntryTmp.Validate("Dimension Value Code", DefaultDimVal."Dimension Value Code");
                        DimensionSetEntryTmp."Dimension Value ID" := DimensionValue."Dimension Value ID";
                        DimensionSetEntryTmp.Insert();
                    end;
                until DefaultDimVal.Next = 0;
            end;

            DimensionValue.Get('Project', ProjectDimVal);
            DimensionSetEntryTmp.Init();
            DimensionSetEntryTmp.Validate("Dimension Code", DimensionValue."Dimension Code");
            DimensionSetEntryTmp.Validate("Dimension Value Code", ProjectDimVal);
            DimensionSetEntryTmp."Dimension Value ID" := DimensionValue."Dimension Value ID";
            // DimensionSetEntryTmp."Dimension Set ID" := -1;
            DimensionSetEntryTmp.Insert();

            DimSetID := DimensionSetEntry.GetDimensionSetID(DimensionSetEntryTmp);

            Rec."Dimension Set ID" := DimSetID;
            Rec.Modify();
        end;
    }
    I coudnt find OnValidate() trigger in this extension obj. as you said in message.
    Pls help me to resolve me with the above error screenshot!
    Thanks in Advance.
  • Verified answer
    Suresh Kulla Profile Picture
    47,789 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    I think we already discussed this on the messaging about the solution, you just need to us the dimension management codeunit to pass the temporary dimension set entries and then use the dimensions management codeunit to get the dimension set id and that function will take care of whether to create a new entry or not.

    Please let me know if you have any other questions.

  • AXBOY Profile Picture
    607 on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    Hi Abhinav, thanks for the reply , before updating the dimensions on the Sales Quote against a Customer , i want to check if the customer has any dimensions with it.

    Case 1 : if it has, then i want to further check if it has  Dimension Code = Project, if yes then i want to update the Dimension Value for that Project Code and then populate all the Dimension Code(s) on the Sales Quote created.

    Cast 1 : If that Customer doesnt have any Dimension Code = Project, then insert a Dimension Code = Project with a dimension Value, and then populate that Dimension Code = Project on the Sales Quote Created.

    Note : Dimension Code = Project , has already been Created in the Master table(Dimension Value)

    Please Help!

  • Suggested answer
    Abhinav Mehra Profile Picture
    on at
    RE: Though AL code how to automatically setup a specific dimension on a Sales Quote as soon as that Sales Quote is manually created?

    1. Make 1 new codeunit for Sales Header Event Subscriber

    2. Call event there as per below

    [EventSubscriber(ObjectType::Table, Database::"Sales Header", 'OnafterinsertEvent', '', false, false)]

       local procedure OnValidateSellToCustomerNoAfterInit(var Rec: Record "Sales Header")

       begin

           If Customer.Get(Rec."Sell-to Customer No.") then begin

               DimensionValue.Reset();

               DimensionValue.SetRange("Dimension Code",Customer."Global Dimension 1 Code");

               DimensionValue.SetRange(Code,UserId);

               If DimensionValue.FindFirst() then

               Rec."Shortcut Dimension 1 Code":= DimensionValue.Code;

               Rec.Modify();

           end;

         end;

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 Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

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

#1
YUN ZHU Profile Picture

YUN ZHU 628 Super User 2025 Season 1

#2
Mansi Soni Profile Picture

Mansi Soni 495

#3
Sagar Dangar, MCP Profile Picture

Sagar Dangar, MCP 395

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans