0
votes

I'm using dynamics crm 2016, my mission is to copy a record in client side (no plugins) so using onload event I'm calling a function that fetching an object with all my attributes(to be copy to..), I can fill simply all fields in my form except from lookup field. I have in my data from my fetch 3 values to use: name: the data shown in the field' value: the guid of the entity reference and a number of type.. To populate a lookup field I need the name of the entity the id and type. ***THE code needs to be generic Two questions rise up:

1.Is the type value usable to populate the entity type field if yes then how?
2.Can I populate the name attribute in the new field as is from the old field?

Example of the data that I get :

Attributes
:
name
:
"this is the name"
type
:
"10026"  -- Can I get the entity name (Account,Contact etc...) according to this val?
__proto__
:
Object
Value
:
"{6BddfDC-8B10-47F4-BC49-A537d2E43E}"
2
I understand that I need to query the metadata to get the entity name according to entityType, so If someone has a simple Idea.Damkulul

2 Answers

0
votes
  1. Not sure from where you are getting the entity type code aka objecttypecode (10026), you may need to change the source to store entity type name (logical name). Since the object array used to store lookup value expects guid, display name & type like [{ id: recordid, name: recordName, entityType: entityName }]. Otherwise like you commented you need to pull from Metadata or store some key:value pairs as enums.

  2. You can get all the needed properties from old field & set to new field like below.

-

var lookupObjValue = Xrm.Page.getAttribute(old_lookupSchemaName).getValue();

var lookupEntityType = lookupObjValue[0].entityType, //To get EntityName
lookupRecordGuid = lookupObjValue[0].id, // To get record GUID
lookupRecordName = lookupObjValue[0].name; //To get record Name 

Xrm.Page.getAttribute(new_lookupSchemaName).setValue([{ id: lookupRecordGuid, name: lookupRecordName, entityType: lookupEntityType }]);
0
votes

If your requirement is no code (plugins) to me it seems a bit of a cheat to use JavaScript to achieve the same.

You should investigate how much of this could be done using a CRM Workflow, making a solution that would be supportable by a functional support person rather than a technical.

You could then optionally trigger the workflow using JavaScript