Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

employee name and employee id lookup value selecting first value in case of duplicate names

(0) ShareShare
ReportReport
Posted on by 212

Hi Team,

I have created a custom field for Employee name. I have added a string type field with extended data type of resource view. Suppose i have 2 john cooper with different employee id still system is selecting first value only. please help. Below is my code

class ProjectResource
{
/// <summary>
///
/// </summary>
/// <param name = "_formControl"></param>
/// <param name = "_filterStr"></param>
public void lookup(FormControl _formControl, str _filterStr)
{
super(_formControl, _filterStr);
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(ResourceView), _formControl);
Query query = new Query();
QueryBuildDataSource queryBuildDataSource,queryBuildDataSource1;
QueryBuildRange queryBuildRange;

sysTableLookup.addLookupfield(fieldnum(ResourceView,ResourceId),true);
sysTableLookup.addLookupfield(fieldnum(ResourceView,Name),true);

queryBuildDataSource = query.addDataSource(tablenum(ResourceView));
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}

}

  • Suggested answer
    Rachit Profile Picture
    4,015 User Group Leader on at
    RE: employee name and employee id lookup value selecting first value in case of duplicate names

    Hi TxTxt,

    As Martin suggested, you are using a wrong approach here.

    Have a look at how same thing is modelled in standard application. Most simple example you can find is on sales table >> workersalestaker field.

    You need to add a foreign key relation in your table with HCMWorker table and store the recID of HcmWorkerTable in your table . Then in order to display the name on the form you need to add a reference group control and select the right field group or you can create a new field group.

    Hope this helps.

  • Martin Dráb Profile Picture
    231,768 Most Valuable Professional on at
    RE: employee name and employee id lookup value selecting first value in case of duplicate names

    Please tell us more about the behavior that you want to achieve, because it currently doesn't make sense to me.

    You want to return two values (ID and name), but the purpose of a lookup is selecting a single value. If your lookup is for an ID, you should return an ID. If it's for a name, you should return a name. You said you wanted to return both, but what would you do with them?

    The right approach, IMHO, is using ID, because names aren't unique. Then you can fetch the name for the selected ID.

  • Dynamics365Admin Profile Picture
    212 on at
    RE: employee name and employee id lookup value selecting first value in case of duplicate names

    It didn't work. I want to return name with correct resource id on which i have clicked on

  • Suggested answer
    Martin Dráb Profile Picture
    231,768 Most Valuable Professional on at
    RE: employee name and employee id lookup value selecting first value in case of duplicate names

    I think the bug in the fact that you want to return ResourceId, but you asked to return both ResourceId and Name. This doesn't make sense and the system probably uses the latest one, i.e. Name.

    Fix if by removing the second parameter when adding Name to the lookup:

    // Old code
    sysTableLookup.addLookupfield(fieldNum(ResourceView, Name), true);
    
    // New code
    sysTableLookup.addLookupfield(fieldNum(ResourceView, Name));

  • Dynamics365Admin Profile Picture
    212 on at
    RE: employee name and employee id lookup value selecting first value in case of duplicate names

    Hi Martin,

    Yes you are correct I am referring to Resource id. On clicking on lookup field, it shows the list of resources name and id

    Now we have Resource name - Neha Jain  Resource id 01

    and second  Resource name - Neha Jain   Resource id 02

    Even when I am clicking on Resource id 02,  01 is getting selected and getting saved on table.

    Please help on this

  • Martin Dráb Profile Picture
    231,768 Most Valuable Professional on at
    RE: employee name and employee id lookup value selecting first value in case of duplicate names

    First of all, let me paste your code in the right way (you forgot to use Insert > Code), throw away unsused variables and things like that. This should be much easier to read:

    public void lookup(FormControl _formControl, str _filterStr)
    {
    	super(_formControl, _filterStr);
    	
    	SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(ResourceView), _formControl);
    	sysTableLookup.addLookupfield(fieldNum(ResourceView, ResourceId), true);
    	sysTableLookup.addLookupfield(fieldNum(ResourceView, Name), true);
    
    	Query query = new Query();
    	query.addDataSource(tableNum(ResourceView));
    	sysTableLookup.parmQuery(query);
    	sysTableLookup.performFormLookup();
    }

    You mentioned employees, but I don't see them in your code. Do you mean ResourceView.ResourceId when talking about "employee id"? What exactly do you mean by "system is selecting"?

  • Gunjan Bhattachayya Profile Picture
    35,421 on at
    RE: employee name and employee id lookup value selecting first value in case of duplicate names

    Hi,

    Is this the lookup for Name field or for Resource Id field?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,886 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,768 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans