Today’s Learning | Dynamics 365 Web API


Photo by Roman Synkevych on Unsplash

Today’s Learning 📚

Today I focus on managing to learn and understand the Dynamics 365 Web API.

The problem I was trying to solve was.

Parent entity or table (new reference by Microsoft) and when a look up field was populated we need to pull some properties into equivalent fields in the parent table from that Lookup record we just associated.

Ok, that said in technical jargon, what it basically need is:

We need to copy field from the record in the look up field into field in parent record when load() or save() events.

👀 Why you ask?

In Dynamics 365 in order to make some calculations in form all field need to present in that form.

Now the Web API

Microsoft Dynamics 365 is for me one of the best alongside Salesforce one of the best documentation out there, simple and with examples really cool.

Pseudocode runtime in order was:

💢 Use formContext getting form context

💢 Load LookUp field using Xrm.Page.getAttibute(“FIELDNAME”);

💢 if Statements checking if LookUp field contains data

💢 Use the super power of retrieveRecord()

💢 ⤴️ Method take parameters as Xrm.WebApi.retrieveRecord (“Entity”, ID_from_Response, “?$select=PropertyinTargetLookUp” )

💢 We then .then( Success + Error functions )

SUCCESS ✅

I then grabbed the Success Parsed/returned parameter using dotNotation getting value from LookUp entity.

💻 let response = response.sirocco_consconsumption;

💢 Using setValue() method parse the parameters value into the field loaded locally in the parent.

💻 let VARIABLE = formContext.getAttribute(FIELDNAME).SetValue(ParsedParameters)

Error ❌

I show pop up with error handler message as below:

💢 Xrm.Navigation.openAlertDialog({ text: error.message });

function retrieveCons(executionContext) {
formContext = executionContext.getFormContext();

let lookupObjB = Xrm.Page.getAttribute("FieldName"); //Check for Lookup Object

    if (lookupObjB != null) {

        let lookupObjValue = lookupObjB.getValue();//Check for Lookup Value
        if (lookupObjValue != null) {

            let lookupRecordGuidB = lookupObjValue[0].id;


        Xrm.WebApi.retrieveRecord("ENTITY_NAME",lookupRecordGuidB,"?$select=LookUpPropertyFieldName").then(
            
            (result) => {
                //set the value to local field in Parent Record
                let Consumption = result.LookUpPropertyFieldName;
            
                
                //Troubleshootinh in Console 👀
                console.log("Retrieved cons values: Consumption: " + Consumption);    
                console.log("Retrieved cons values: speed: " + speed);    
                let consUnderway = formContext.getAttribute("FieldNameParentPopulated").setValue(Consumption);         
                debugger; 
            },
            (error) => {
                Xrm.Navigation.openAlertDialog({ text: error.message });
            }
        );
                
        }

    }
}
4 views0 comments