Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript to set "name" field for custom entity

Posted on by 4,926

Hi,

I have a custom entity, where I have to write a combination of two fields into the default field "name".

Most of the records I Import via Scribe and there I know how to set the name field.

But users could also create records themselves and in this case the Name field should be popolated automatically on save.

The Name field should be populated with the text value of a lookup field to entity contact and with the selected option of an option set.

I tried to write JS for that (I never wrote a single line of JS code before and I assume my code is a hughe mess ;)) and Register the according function "on save" for the relevant form.

Unfortunately something seems to be not working, since my form doesn't allow me to save any more. It remains open and no error is being returned.

Any advise would be highly appreciated ;)

thx Thomas

function SetName () {
// Kunde aus Erreichbarkeit
var Kunde

var lookupFieldObject = Xrm.Page.data.entity.attributes.get('tt_contact');
    if (lookupFieldObject.getValue() != null) {
        Kunde= lookupFieldObject.getValue()[0].name;
    }

// Optionset Erreichbarkeitstyp
var Typ = Xrm.Page.getAttribute("tt_typ_erreichbarkeit").getSelectedOption().text;


// Name setzen
Xrm.Page.getAttribute("name").setValue(Kunde + " - " + Typ)
}

*This post is locked for comments

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Javascript to set "name" field for custom entity

    OK, I figured it out now.

    thx to all for your Support:

    function SetName () {

    // Kunde aus Erreichbarkeit

    var kunde ="";

    var contact = Xrm.Page.getAttribute('tt_contact');

    if (contact != null) {

    var contactValue = contact.getValue();

    if (contactValue != null) {

    kunde = contactValue[0].name;

       }

    }

    // Optionset Erreichbarkeitstyp

    var typ = Xrm.Page.getAttribute('tt_typ_erreichbarkeit').getText();

    // Name setzen

    Xrm.Page.getAttribute('tt_name').setValue(kunde + " - " + typ)

    }

    br Thomas

  • Verified answer
    RE: Javascript to set "name" field for custom entity

    your variable "Kunde" is not initialized like "var Kunde ="". If the lookup is not containing data --> Kunde will not be set and will be "undefined". you should just initialize your Kunde-Varible like described and everything will be good ;)

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Javascript to set "name" field for custom entity

    "name" Feld ist Text oder Lookup Field?

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Javascript to set "name" field for custom entity

    Danke.

    Liefert bei mir leider auch undefined zurück :(

    Lg Thomas

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Javascript to set "name" field for custom entity

    Hi Thomas use this code. I try on CRM 2015 it is working.

    function SetName () {
    // Kunde aus Erreichbarkeit
    var Kunde;
    
    var lookupFieldObject = Xrm.Page.data.entity.attributes.get('tt_contact');
        if (lookupFieldObject.getValue() != null) {
            Kunde= lookupFieldObject.getValue()[0].name;
    		//alert(Kunde);
        }
    
    // Optionset Erreichbarkeitstyp
    var Typ = Xrm.Page.getAttribute("tt_typ_erreichbarkeit").getSelectedOption().text;
    //alert(Typ);
    
    // Name setzen
    Xrm.Page.getAttribute("name").setValue(Kunde + " - " + Typ);
    }

    Is the field "name" text or lookup field?

    PC: bitte ignore die Zeilennummer :)
    Grüss

    Happy CRM

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Javascript to set "name" field for custom entity

    Hi,

    ok now at least the save works and the text of the Option set is correctly returned.

    But instead of the contact Name "undefined" is being displayed.

    Any ideas?

    br Thomas

    undefined.png

  • Suggested answer
    RE: Javascript to set "name" field for custom entity

    you could try to use Xrm.Page.getAttribute("tt_typ_erreichbarkeit").getText().

    And I would always add ; behind each Statement --> var Kunde = "";

    you could also hit F12 in browser to see if there is an error in your JavaScript.

    and one last Thing: I would always use Xrm.Page.getAttribute("") instead of Xrm.Page.data.entity.attributes.get

    hope this helps a bit.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans