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 :
Finance | Project Operations, Human Resources, ...
Answered

In x++ Import Update

(0) ShareShare
ReportReport
Posted on by 1,215

Hi guys,

i have a update two record in PositionID and Description 

"Record 000001Employee Num 000001 doesn't exist"

"Record 000001Employee Num 000002 doesn't exist:

below my code.

private void importPositionDescription()
    {
        int                             rowNo, counter, inserted;
        HcmPosition                     hcmPosition;
        HcmPositionDetail               hcmPositionDetail;
        HcmPositionId                   positionId;
        OfficeOpenXml.ExcelRange        columnVal;
        OfficeOpenXml.ExcelRange        colValNum;
        rowNo=2;

        if (dialog.run() && dialog.closedOk())
        {
            FileUpload fileUploadControl     = dialog.formRun().control(dialog.formRun().controlId("Upload"));
            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
        
            if (fileUploadResult != null && fileUploadResult.getUploadStatus())
            {
                stream = fileUploadResult.openResult();
            
                using (ExcelPackage Package = new ExcelPackage(stream))
                {
                    Package.Load(stream);
                    ExcelWorksheet  worksheet   = package.get_Workbook().get_Worksheets().get_Item(1);

                    OfficeOpenXml.ExcelRange    range       = worksheet.Cells;

                    while (range.get_Item(rowNo,1).Value)
                    {

                        setPrefix(strFmt("@MPL425",range.get_Item(rowNo,1).Value));

                        try
                        {
                            positionId = range.get_Item(rowNo,1).Value;

                           /* hcmPosition = HcmPosition::find(hcmPositionDetail.Position);
                            if(hcmPosition)
                            {
                                ttsbegin;
                                hcmPosition.selectForUpdate(true);
                                hcmPosition.PositionId = range.get_Item(rowNo,2).Value;
                                hcmPosition.doUpdate();
                                ttscommit;
                            }*/
                            hcmPositionDetail = hcmPositionDetail::findByPosition(hcmPosition.RecId);
                            if(hcmPositionDetail)
                            {
                                ttsbegin;
                                hcmPositionDetail.selectForUpdate(true);
                                hcmPositionDetail.Description_AR = range.get_Item(rowNo,2).Value; 
                                hcmPositionDetail.doUpdate();
                                ttscommit;
                            }
                            
                            else
                            {
                                warning(strFmt("@MPL800",positionId));
                            }
                            rowNo  ;
                            counter  ;
                        }
                        catch (Exception::Error)
                        {

                            counter  ;
                            rowNo  ;
                            continue;
                        }

                    }
                }
            }
        }

        info(strFmt("@MPL426",counter));
    }

what did i mistake.

Thanks.

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    The first mistake is mixing all the code in a single method. Why would you (and we) have to deal with all the stuff about file upload, Excel etc. if the actual problem is about working with AX tables?

    You also forgot to mention where your code fails.

    It seems that the relevant code is this:

    HcmPosition hcmPosition;
    
    /* hcmPosition = HcmPosition::find(hcmPositionDetail.Position);
    if(hcmPosition)
    {
    	ttsbegin;
    	hcmPosition.selectForUpdate(true);
    	hcmPosition.PositionId = range.get_Item(rowNo,2).Value;
    	hcmPosition.doUpdate();
    	ttscommit;
    }*/
    
    if (hcmPositionDetail)
    {
    	ttsbegin;
    	hcmPositionDetail.selectForUpdate(true);
    	hcmPositionDetail.Description_AR = range.get_Item(rowNo,2).Value; 
    	hcmPositionDetail.doUpdate();
    	ttscommit;
    }
    else
    {
    	warning(strFmt("@MPL800",positionId));
    }

    Now it's easy to see a problem - you commented out the code populating hcmPosition, therefore it'll always be empty.

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    Another bug is that you're trying to update empty records. If you want to update an error (and not create a new one), you must first read it from a table.

  • Riyas ahamed F Profile Picture
    1,215 on at

    Thanks for your replay martin,

    actually PositionID Primary key so we can't update , now to to rewrite above the logic to

    in my excel sheet

    PositionID   Ar Description

    000001,      Description

    000001,      Name

    getting error "Record 000001Employee Num 000001 doesn't exist"

    "Record 000001Employee Num 000002 doesn't exist:

  • Riyas ahamed F Profile Picture
    1,215 on at

    How to overcome that issue.

  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    You are not updating the primary key, you are updating Description_AR field.

    You use the primary key to find the record that you attempt to update. Your error message means that there is no such record. For example, you don't have a record for PositionId 000001, but you try to update such record.

  • Riyas ahamed F Profile Picture
    1,215 on at

    Thanks for your replay Nikolaos Mäenpää,

    please give us a Example  how to Position ID "000001" against update to Description.

  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    You must find/select the record by calling HcmPosition::findByPosition. Then you need to check if you found something. And finally you can update it.

  • Verified answer
    Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    It would be something like this:

    HcmPosition hcmPosition = HcmPosition::findByPosition('abcd');
    if(hcmPosition)
    {
    	ttsbegin;
    	HcmPositionDetail hcmPositionDetail = HcmPositionDetail::findByPosition(
    	                                                            hcmPosition.RecId,
    	                                                            DateTimeUtil::utcNow(),
    	                                                            DateTimeUtil::utcNow(),
    	                                                            true);
    	if (hcmPositionDetail)
    	{
    		hcmPosition.Description_AR = 'new description';
    		hcmPosition.update();
    	}
    	ttscommit;
    }

    It might be a bit more complex, depending on your needs, because it's a date-effective table.

    Also note that you could simply export the Excel file through "Position details" entity.

  • Verified answer
    mhdshb1 Profile Picture
    1,250 on at

    Hi Ahmad,

    as mentioned above an example can be 

    HcmPosition position = HcmPosition::findByPosition(positionId,true);
    if(position.recId!=0)
    {
    //update
    }

    Regards,

    M

  • mhdshb1 Profile Picture
    1,250 on at

    @Martin i guess the find method takes recId as parameter.

    The right method is findbyPosition.

    Regards,

    M

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 456 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans