Hi ,
I'm trying to set a default value for a lookup field. I keep on seeing "fieldid" in the suggested script.
How will I know the field id of a certain field?
*This post is locked for comments
Hi ,
I'm trying to set a default value for a lookup field. I keep on seeing "fieldid" in the suggested script.
How will I know the field id of a certain field?
*This post is locked for comments
I use the following function to set the value of a lookup field (usually in a globals file):
function setLookupField(fieldName, lookupId, lookupName, entityName) {
var lookupData = new Array();
var lookupItem = new Object();
lookupItem.id = lookupId;
lookupItem.name = lookupName;
lookupItem.entityType = entityName;
lookupData[0] = lookupItem;
Xrm.Page.getAttribute(fieldName).setValue(lookupData);
}
You then just call the function where you need it:
setLookupField("FIELD_LOGICAL_NAME", "GUID_OF_LOOKUP", "TEXT_THAT_GOES_IN_LOOKUP", "NAME_OF_ENTITY");
Example:
setLookupField("new_FieldId", "4AB26754-3F2F-4B1D-9EC7-F8932331567A", "Sample Project", "account");
Hope this helps.
Hi Arci,
if you solved the problem thanks to an answer, please mark it as verified to encourage the community to provide more and more a better support. Thank you. Francesco
If you found the answer helpful, please mark as Verified
Join my network on LinkedIn Follow me on Twitter
Thank You & Best Regards
Francesco Picchi
Microsoft Dynamics CRM Consultant, Bologna, ITALY
Independent Contractor
You can try
var name = Xrm.Page.getAttribute('<FIELD_NAME>').getValue()[0].name;
(you can also use "id": Xrm.Page.getAttribute('<FIELD_NAME>').getValue()[0].id;)
Hi Arci,
You need to go to Setting >> Customization >> Customize The System >> your entity >> fields >> This will list out the field along with their schema name. Look for you field and then use the schema name in your code.
For example, if you want to know the field id of Primary Contact of Account entity then go to Setting >> Customization >> Customize The System >> Account >> Primary Contact >> the schema name for Primary Contact is primarycontactid.
So to get this lookup, you need to write
Xrm.Page.getAttribute("new_fieldid")
Hope this helps
Hi Arci,
you could use the following methods to deal with lookup:
// GET LOOKUP var lookup = Xrm.Page.getAttribute('XXXXXX').getValue(); if (lookup != null) { var name = lookup[0].name; var id = lookup[0].id; var type = lookup[0].entityType; } // SET LOOKUP var lookup = [{}]; lookup[0].id = recorid; lookup[0].name = recordname; lookup[0].entityType = entityname; Xrm.Page.getAttribute('XXXXXX').setValue(lookup);
Please let us know.
If you found the answer helpful, please mark as Verified
Thank You & Best Regards
Francesco Picchi
Microsoft Dynamics CRM Consultant, Bologna, ITALY
Independent Contractor
Hi,
You are supposed to use
Xrm.Page.getAttribute("new_fieldid").setValue(value);
to set a value
new_fieldid is your lookup field name (so you'll need to change it to match your field name)
value is the value you are going to set there. There are lots of examples, here is just one of those:
www.mscrmconsultant.com/.../get-and-set-lookup-value-using.html
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,280 Super User 2024 Season 2
Martin Dráb 230,235 Most Valuable Professional
nmaenpaa 101,156