Wenn Kontakt ausgewählt -> Übergabe von Firma an Feld

7. Dezember 2011 20:39

Hallo CRM Community,

bin neu in der CRM Welt.

Habe eine Frage und weiß nicht wo mein Fehler ist.
Folgende Situation. (bin im coden eingerostet :-( ....)

Wenn im Feld Kontakt ein Wert ausgewählt wird soll im Feld Firma automatisch die passende Firma zum Kontakt aufgerufen werden.
Es soll über ein Script gelöst werden.
Ich habe hier zwar was umgeschrieben aber irgendwie funzt es nicht.
Kann mir jemand weiterhelfen?

Hier der Code:

///<reference path="../ABCD.Library/ABCD.Library.Fetch.js" />
///<reference path="../ABCD.Library/ABCD.Library.GUI.js" />

//Create Namespace ABCD
if (typeof (ABCD) == "undefined") {
//Define Object to be used as Namespace
ABCD = {};
}

//Create Namespace ABCD.Library
if (typeof (ABCD.Library) == "undefined") {
//Define Object to be used as Namespace
ABCD.Library = {};
}

// =========================================================
// ABCD.Account
// =========================================================
ABCD.Account = {

//ShortCut zu ABCD Libraries
elg: ABCD.Library.GUI,
elf: ABCD.Library.Fetch,
elc: ABCD.Library.Const,

//----------------------------------------------------------------
// OnLoad
//----------------------------------------------------------------
OnLoad: function () {

if (crmForm.FormType == ABCD.Library.Const.FORM_TYPE_CREATE
|| crmForm.FormType == ABCD.Library.Const.FORM_TYPE_UPDATE) {
ABCD.Account.ReadAccount();
ABCD.Account.InitScheduledEnd();

ABCD.Account.FilterLookup();

if (Xrm.Page.getControl("to") != null)
crmForm.all.to.attachEvent("onchange", ABCD.Account.FilterLookup);
}

ABCD.Account.ShowHideSlxUser();

},

//----------------------------------------------------------------
// Kontakt OnChange
//----------------------------------------------------------------
OnBezugChange: function () {
ABCD.Account.ReadAccount();
},


//----------------------------------------------------------------
// Zugehörige Firma auslesen
//----------------------------------------------------------------
ReadAccount: function () {

// Kontakt ermitteln
var contactid = Xrm.Page.getAttribute("responsiblecontactid");
var contactidLookup = contactid.getValue();
if (contactidLookup != null && contactidLookup[0] != null && contactidLookup[0].entityType == "contact") {
var contactidValue = contactidLookup[0].id;

var entitySetDefinition = "(guid'" + contactidValue + "')";

var serverUrl = Xrm.Page.context.getServerUrl();
var crmObject = ABCD.Library.Fetch.EntityByREST(serverUrl, 'Contact', entitySetDefinition);

// Funktion auslesen
if (crmObject != null && crmObject.ParentCustomerId != null) {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = crmObject.ParentCustomerId.Id;
lookupValue[0].name = crmObject.ParentCustomerId.Name;
lookupValue[0].entityType = 'account';

// Prüfen, ob diese Firma bereits hinterlegt ist
var accountidLookup = Xrm.Page.getAttribute('customerid').getValue();

if (accountidLookup == null
|| accountidLookup[0] == null
|| accountidLookup[0].id.toUpperCase() != "{" + crmObject.ParentCustomerId.Id.toUpperCase() + "}") {

Xrm.Page.getAttribute('customerid').setValue(lookupValue);
crmForm.all.customerid.ForceSubmit = true;
}
}
}
}
},