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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Trying to retrieve SalesLine.SalesId

(0) ShareShare
ReportReport
Posted on by 15

Hello,

I would like to retrieve the current salesLine.SalesId in order to use it in the code of a configurator. Is it possible ? I didn't find a way to directly have it.

For example : if i'm using the configurator in the SalesLine with the SalesId : c067991, i would like to attribute that SalesId to a variable declare in the code of my configurator.

Thank you in advance

I have the same question (0)
  • Suggested answer
    Kenneth Ariel Chaves Herrera Profile Picture
    on at

    Hi AymericKer, I hope you are doing good.

    There are many where you can do it but the most common way is using SDK in C# language.

    I use the following to create a connection to my enviorement and then to CRUD different entities.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Crm.Sdk.Messages;
    using System.Net;
    using System.ServiceModel;
    using Microsoft.Xrm.Tooling.Connector;
    using Microsoft.Xrm.Sdk.Query;
    
    //THIS CODE IS USING SDK TO CONNECT TO DYNAMICS WITH C#
    
    namespace DynamicsLinkToVS
    {
        class Program
        {
            static void Main(string[] args)
            {
                IOrganizationService oServiceProxy;
                try
                {
                    Console.WriteLine("Setting up Dynamics 365 connection");//This line create the Dynamics 365 Connection:
                    CrmServiceClient oMSCRMConn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient("AuthType=Office365;"  
                    "Username=test.com;Password=test;URL=test.com");
                    //Now Create the IOrganizationService
                    oServiceProxy = (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient != null ?
                    (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient :
                    (IOrganizationService)oMSCRMConn.OrganizationServiceProxy;
                    Console.WriteLine("Validating Connection");
                    if (oServiceProxy != null)
                    {
                        //Get the current User Id
                        Guid userId = ((WhoAmIResponse)oServiceProxy.Execute(new WhoAmIRequest())).UserId;
                        if (userId != Guid.Empty)
                        {
                            Console.WriteLine("Connection Successful");
                            //Creating a new entity
                            var mycontact = new Entity("contact");
                            mycontact.Attributes["firstname"] = "test";
                            //Create a new contact and returns an Id because create function returns an Id
                            Guid recordId = oMSCRMConn.Create(mycontact);
                            //Create a new Entity as contact
                            Entity aux = new Entity("contact");
                            //Save the Id in the Id of aux
                            aux.Id = recordId;
                            //Update something
                            aux.Attributes["lastname"] = "test";
                            //Changes are applied to the enviorement
                            oMSCRMConn.Update(aux);
                            //Get record
                            Entity contact = oMSCRMConn.Retrieve("contact", recordId, new ColumnSet("firstname", "lastname"));
                            Console.WriteLine(contact.Attributes["lastname"]);
                            //Delete record created
                            oMSCRMConn.Delete("contact", recordId);
                            //Get all contacts
                            QueryExpression query = new QueryExpression("contact");
                            query.ColumnSet = new ColumnSet("firstname", "lastname");
                            EntityCollection ec = oMSCRMConn.RetrieveMultiple(query);
                            for (int i = 0; i < ec.Entities.Count; i  )
                            {
                                if (ec.Entities[i].Attributes.ContainsKey("firstname"))
                                {
                                    Console.WriteLine(ec.Entities[i].Attributes["firstname"]);
                                }
    
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Connection failed");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: "   ex.ToString());
                }
                Console.ReadKey();
            }
        }
    }

    You can use that as a reference.

    I this answers your question please check it as verified to help others finding useful information.

    Here additional info that can be valuable:

    QueryExpression Class (Microsoft.Xrm.Sdk.Query) | Microsoft Docs

    Welcome to this amazing community.

  • Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    AymericKer, what do you mean by a configurator?

    Kenneth Chaves, how is your answer related to the question? It seems to me that it's not only unrelated to retrieving SalesId, but also unrelated to Dynamics AX at all.

  • AymericKer Profile Picture
    15 on at

    I mean a product model, and when someone uses this model, i want it to retrieve the SalesId of the current salesLine and then write the information in a new table.  I also tried with recId, but i couldn't find a way to retrieve it either because it doesn't exist until the configuration of the item is done.

  • Suggested answer
    Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    You can find the current sales line in sourceLine variable of PcExecuteVariantConfiguration class. But don't forget that the order line isn't saved yet and it may never be saved (e.g. because the use leaves the SalesTable form without saving). If you store information about the sales line already when configuring the product, you may end up with inconsistent data. The fact that the line isn't saved also prevents you from referring to its RecId.

    Could you save the information when saving the sales line? You could do that by overriding insert() method of SalesLine.

  • AymericKer Profile Picture
    15 on at

    I'm not sure that's possible in my case, because informations that i'm sending to the table are not related to the salesLine and are only related to choices of users when using the product model.

    How can i call the sourceLine variable directly in my code ? And if i call it, wil it return to me the salesLines.SalesId directly ?

    In my case it's not a problem that the salesLine isn't saved, because we are only going to read the table for the data when the SalesLine is saved so we could filter the data afterwards.

    Thank you for your answer.

  • Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    If you modify the class, you can refer to sourceLine variable simply by its name. It does contain all fields; just don't forget to cast it to the right type. For example:

    SalesLine salesLine = sourceLine as SalesLine;
    if (salesLine)
    {
    	info(salesLine.SalesId);
    }

  • AymericKer Profile Picture
    15 on at

    i don't really understand, i have to modify the class in order to have the sourceLine ? i did find a method that return sourceLine.

    public Common parmSourceLine(Common _sourceLine = sourceLine)

    {

       sourceLine = _sourceLine;

       return sourceLine;

    }

    Can't i just call that method ? I'm sorry i'm new to AX i don't understand really fast.

    Thanks

  • Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    It depends on where you want to put your logic. If you don't want to put it into PcExecuteVariantConfiguration, then you indeed don't have to modify this class, but then you must find another place.

  • AymericKer Profile Picture
    15 on at

    I don't understand what i have to change, in or out of the PCExecuteVariantConfiguration class, can you explain it to me please ? Because i thought i only had to call the object method and give the value (the current salesId)to an object but i can't do it.

    Also i tried to applied your code to the class, but got an syntax error. Do you know why ?

    thank you 

  • Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    Does it mean that you don't know how to use insert a record to a table? I thought that's what you want to do, but inserting a record isn't the same thing as "to give the value (the current salesId)to an object". Also, could you please tell us more about your problem than just "i can't do it"?

    If you doesn't know how to insert a record, we'll need to address it first. Here is a simple example:

    MyTable myTable;
    
    myTable.SalesId = 'abc123';
    myTable.insert();

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans