Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Set lookpup field value if another field have a especified value Through javascript funtion

Posted on by 15

hi ! i need autocomplete a lookup type field in my form if another field have a especified value.

if field "Nivel 2 " = Beneficios Externos

field "SLA" = 24 hs B2C

else " SLA" = 48 hs B2C

pastedimage1647356204906v1.png

this is my code:


// GET Lookup
function RetrieveSLAToCaseB2C(executionContext) {

     var formContext = executionContext.getFormContext();
     var nivel2 = formContext.getAttribute("axnr1_nivel_2").getValue()
// Access the field on the form
     var fieldValue = formContext.getAttribute("slaid").getValue();
// Verify it does exist on the form
     if (nivel2 != null) {
// To get the Id, Name and Entity Name (account/contact)
       if (nivel2 != "Beneficios Externos") {
          formContext.getAttribute(fieldValue).setValue("24 hs B2C");
       }else{
          formContext.getAttribute(fieldValue).setValue("48 hs B2C");
            }
       }
}

  • Verified answer
    Pulkit_Srivastava Profile Picture
    Pulkit_Srivastava 170 on at
    RE: Set lookpup field value if another field have a especified value Through javascript funtion

    Hi,

    You have to first fetch the SLA as this is a lookup field and then set the value in the "slaid".

    the code should look like

    // GET Lookup
    function RetrieveSLAToCaseB2C(executionContext) {
    
        var formContext = executionContext.getFormContext();
        var nivel2 = formContext.getAttribute("axnr1_nivel_2").getValue()
        // Access the field on the form
        var fieldValue = formContext.getAttribute("slaid").getValue();
        // Verify it does exist on the form
        if (nivel2 != null) {
            // To get the Id, Name and Entity Name (account/contact)
            if (nivel2[0].name != "Beneficios Externos") {
                var twentyFourHoursB2CSla = Get24HoursB2CSLA();
                if (twentyFourHoursB2CSla !== null && typeof (twentyFourHoursB2CSlaId) !== "undefined") {
                    var twentyFourHoursB2CSlalookupValue = new Array();
                    twentyFourHoursB2CSlalookupValue[0] = new Object();
                    twentyFourHoursB2CSlalookupValue[0].id = twentyFourHoursB2CSla.slaid;
                    twentyFourHoursB2CSlalookupValue[0].name = twentyFourHoursB2CSla.name;
                    twentyFourHoursB2CSlalookupValue[0].entityType = "sla";
                }
                formContext.getAttribute("slaid").setValue(twentyFourHoursB2CSlalookupValue);
            } else {
                var fortyEightHoursB2CSla = Get48HoursB2CSLA();
                if (fortyEightHoursB2CSla !== null && typeof (fortyEightHoursB2CSla) !== "undefined") {
                    var twentyFourHoursB2CSlalookupValue = new Array();
                    twentyFourHoursB2CSlalookupValue[0] = new Object();
                    twentyFourHoursB2CSlalookupValue[0].id = fortyEightHoursB2CSla.slaid;
                    twentyFourHoursB2CSlalookupValue[0].name = fortyEightHoursB2CSla.name;
                    twentyFourHoursB2CSlalookupValue[0].entityType = "sla";
                }
                formContext.getAttribute("slaid").setValue("48 hs B2C");
            }
        }
    }
    
    /*  
     *  This function retrieves the
     *  24 Hour SLA  
     */
    function Get24HoursB2CSLA() {
        try {
            var result;
            Xrm.WebApi.online.retrieveMultipleRecords("sla", "?$filter=name eq '24 hs B2C'").then(
                function success(results) {
                    console.log(results);
                    for (var i = 0; i < results.entities.length; i  ) {
                        result = results.entities[i];
                    }
                },
                function (error) {
                    console.log(error.message);
                }
            );
            return result;
        } catch (ex) {
            Xrm.Navigation.openAlertDialog(ex.message);
        }
    }
    
    /*
     *  This function retrieves the
     *  48 Hour SLA
     */
    function Get48HoursB2CSLA() {
        try {
            var result;
            Xrm.WebApi.online.retrieveMultipleRecords("sla", "?$filter=name eq '48 hs B2C'").then(
                function success(results) {
                    console.log(results);
                    for (var i = 0; i < results.entities.length; i  ) {
                        result = results.entities[i];
                    }
                },
                function (error) {
                    console.log(error.message);
                }
            );
            return result;
        } catch (ex) {
            Xrm.Navigation.openAlertDialog(ex.message);
        }
    }

    Please mark as ans if  works out for you

    Thanks

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Set lookpup field value if another field have a especified value Through javascript funtion

    Hi,

    You are on the right way but some parts of the code are not correct. Here I matched your code.

    // GET Lookup
    function RetrieveSLAToCaseB2C(executionContext) {

        var formContext = executionContext.getFormContext();
        var nivel2 = formContext.getAttribute("axnr1_nivel_2").getValue();
        var nameNivel2=nivel2[0].name;

        if (nameNivel2 != null) {
    // To get the Id, Name and Entity Name (account/contact)
          if (nameNivel2 != "Beneficios Externos") {
             formContext.getAttribute("slaid").setValue("24 hs B2C");
          }else{
             formContext.getAttribute("slaid").setValue("48 hs B2C");
               }
          }
    }

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,986 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans