Skip to main content

Notifications

Power Apps - Microsoft Dataverse
Answered

Generate field mappings and autopopulate field

(0) ShareShare
ReportReport
Posted on by 820

Hi

I believe i can achieve my goal with entity mappings but i think i don't get it till the very end.

I have a table Works that stores Category,Work name, Work price

I have a table Tasks that stores tasks and this table i have a lookup to Category and to Work name to table Works and a Task Price that is a decimal number

What i want to achieve is when creating new task i select work name(lookup from Works) it would automaticaly populate Work price from table Works to this newly creating record to table Taks to field Task price

Task price is Decimal number.

I tried mapping Work price and Task price in Table Works but this does not autopopulate

What i am doing wrong?

Categories:
  • julijazas Profile Picture
    julijazas 820 on at
    Re: Generate field mappings and autopopulate field

    @Mira_Ghaly 

    Your proposed sollution works! i had mistaken logical name and put id within "" after i fixed all works fine!

  • Gowri Halan Profile Picture
    Gowri Halan 262 on at
    Re: Generate field mappings and autopopulate field

    @jja You would have got this error when you clear off the field first and then select another value.

    Add null check example (if (formContext.getAttribute("new_darbai")) !== null OR undefined)

  • julijazas Profile Picture
    julijazas 820 on at
    Re: Generate field mappings and autopopulate field

    @Mira_Ghaly 

    Hi

    This is my generated code and adjustments:

    f
    function GetWorksPrice(executionContext)
    {
    var formContext = executionContext.getFormContext();
    //This gets the ID you will need to pass the API
    var Works = formContext.getAttribute("new_darbai").getValue();//Work name attribute logical name in Form
    var Id = Works [0].id;

    Xrm.WebApi.online.retrieveRecord("new_darbai", "Id", "?$select=new_kaina").then(
        function success(result) {
            console.log(result);
            // Columns
            var new_darbaiid = result["new_darbaiid"]; // Guid
            var new_kaina = result["new_kaina"]; // Decimal
            var new_kaina_formatted = result["new_kaina@OData.Community.Display.V1.FormattedValue"];
            formContext.getAttribute("new_kaina").setValue(lookup);
        },
        function(error) {
            console.log(error.message);
        }
    );
    }
    On Change of field Work name in my Form i get error:
    Cannot read properties of null (reading 'getValue')
     
    What might be the issue?
  • Gowri Halan Profile Picture
    Gowri Halan 262 on at
    Re: Generate field mappings and autopopulate field

    @jja  You are looking to prepopulate based on work category you choose which can be done only via JS you have steps already provided. 

     

    If you wish to bring the data from Work Entity down to task. First you can map the fields from Work entity to Order entity (you may need to create new fields in order entity). Then map these fields down to Task entity. 

    Please note that Data type should match between the entities to map. 

  • julijazas Profile Picture
    julijazas 820 on at
    Re: Generate field mappings and autopopulate field

    @dpoggemann 

    Now i understand why my mapping doesnt work as i want. It works only from parent entity and i am calling the map from another entity.

    Between Works and Tasks I have Order Entity. I create new Order that gets it's number. Then in Orders I have a tab with Subgrid to Tasks. When i hit on it and hit new task it copies me the order number. Then i select Work category, Work task and i expect Price to be prepopulated but it will not because as i understand i am calling this mapping not from the Works entity so this approach will not work.

    @Mira_Ghaly i will test your approacj and give a feedback

  • Verified answer
    Mira Ghaly Profile Picture
    Mira Ghaly 11,409 on at
    Re: Generate field mappings and autopopulate field

    @jja 

     

    1. You can need to use the XRM tool Box - Data verse Rest API Builder to create a web api request to get the data from the works like price for example, Example below we need to retrieve Mobile from account so your api request should look like below:

    Mira_Ghaly_0-1661807636844.png

    2. Select Request Type= Retrieve Single

    Mira_Ghaly_1-1661807721844.png

    3. Choose the Table like works in your case and the fields like Price

    Mira_Ghaly_2-1661807882216.png

    4. Then click the XRMweb.api

    5. You will be presented with the below javascript:

    Xrm.WebApi.online.retrieveRecord("schemanameofworkstable", "GUID of WORKS Place HOlder", "?$select=epd_abn").then(
    	function success(result) {
    		console.log(result);
    		// Columns
    		var price= result["ShcemaNameofPrice"]; // Text
    	},
    	function(error) {
    		console.log(error.message);
    	}
    );

    5. Next step is to get the GUID of the works field to pass to the above function in place of the GUID of WORKS Place HOlder

     So to get the ID it should be like below:

    function GetWorksPrice(executionContext)
    {
    var formContext = executionContext.getFormContext();
    //This gets the ID you will need to pass the API
    var Works = formContext.getAttribute("PLACE Holder for your field name").getValue();
    var Id = Works [0].id;
    
    Xrm.WebApi.online.retrieveRecord("schemanameofworkstable", Id , "?$select=epd_abn").then(
    	function success(result) {
    		console.log(result);
    		// Columns
    		var price= result["ShcemaNameofPrice"]; // Text
    	},
    	function(error) {
    		console.log(error.message);
    	}
    );
    
    }

    6. Next you need to set the price field as below:

    function GetWorksPrice(executionContext)
    {
    var formContext = executionContext.getFormContext();
    //This gets the ID you will need to pass the API
    var Works = formContext.getAttribute("PLACE Holder for your field name").getValue();
    var Id = Works [0].id;
    
    Xrm.WebApi.online.retrieveRecord("schemanameofworkstable", Id , "?$select=epd_abn").then(
    	function success(result) {
    		console.log(result);
    		// Columns
    		var price= result["ShcemaNameofPrice"]; 
    //Set the field on the task
    formContext.getAttribute("Place holder for price field name").setValue(lookup);
    
    	},
    	function(error) {
    		console.log(error.message);
    	}
    );
    
    }

     

     

    7. Then Go to your Task form in classic mode -> Choose the field Works -> Go to Events Tab and first add your JS file

    Mira_Ghaly_3-1661808659612.png

    2. Then Click Add Event

    Mira_Ghaly_4-1661808689624.png

    Choose your library and type in your JS Name and tick the pass execution Context

    Mira_Ghaly_5-1661808756435.png

     

    Hope this helps you solve your problem!

     

     

     

     

  • Drew Poggemann Profile Picture
    Drew Poggemann 9,273 on at
    Re: Generate field mappings and autopopulate field

    Hi @jja ,

     

    Not other than what I put in the link above and this means you would have a tab on your Works table that would be a subgrid of the Tasks table (or the subgrid could be on the same tab) and you would click New Task from this subgrid from the Works table.  This would automatically fill in the Works lookup (since you created from the subgrid) and then the mappings (from link above) would map the price into the field.  

  • Drew Poggemann Profile Picture
    Drew Poggemann 9,273 on at
    Re: Generate field mappings and autopopulate field

    Hi @jja ,

     

    I usually don't work with JavaScript side so hopefully someone else can help you with that.  If you are creating the rows from the parent Works table you can setup mappings that will set this field (see here).  This will not work if you are not creating from the parent entity and is only initial mapping.

     

  • julijazas Profile Picture
    julijazas 820 on at
    Re: Generate field mappings and autopopulate field

    @dpoggemann 

    I know how to create a real time workflow but it will update the price field value only when i hit save button and i want the price to be prepopulated whenever i select work name in Tasks Form when crating new Task. So it is like real time price that is populated directly into the form.

    I imagine there should be a evenhandler OnChange set on Form on field Workname. So whenever the lookup field Workname is changed it should execute query to  Works table searh for Workname retrieve that work price and set this price to Price field on the form

  • Drew Poggemann Profile Picture
    Drew Poggemann 9,273 on at
    Re: Generate field mappings and autopopulate field

    Hi @jja ,

     

    If I understand the model (depicted below), you should be able to accomplish...

    dpoggemann_0-1661798314258.png

     

    Please try the following:

    1. On the Tasks table, set the Work as a required column when saving the record
    2. Setup real-time workflow so when you Create the record, or update the Works relationship on the Task table that it will do an "Update" to the Tasks table. 
      1. The update should set the field value for the Task Price to the Work Price value from the Works table, since you have a 1:N relationship you will be able access that parent lookup value and set as the Task Price value in the real time workflow.

    Some things to think about:

    1. The model above will not update any Tasks that are related to the Works record if the Work Price is modified.  If this is something you would want then setup a Power Automate Flow that will trigger on the Works record being updated (specifically the Work Price field) and have it loop through all the Tasks records that are related to that specific Works record and update the Task Price.

     

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

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

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,475

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,767

Leaderboard

Featured topics