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 :
Microsoft Dynamics CRM (Archived)

Javascript to set "name" field for custom entity

(0) ShareShare
ReportReport
Posted on by 4,930

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

I have the same question (0)
  • Suggested answer
    Tobias Koller Profile Picture
    on at

    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.

  • tpeschat Profile Picture
    4,930 on at

    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

  • necsa Profile Picture
    3,455 on at

    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
    4,930 on at

    Danke.

    Liefert bei mir leider auch undefined zurück :(

    Lg Thomas

  • necsa Profile Picture
    3,455 on at

    "name" Feld ist Text oder Lookup Field?

  • Verified answer
    Tobias Koller Profile Picture
    on at

    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 ;)

  • tpeschat Profile Picture
    4,930 on at

    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

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans