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, ...
Suggested Answer

Exception handling

(0) ShareShare
ReportReport
Posted on by 390

Hey guys,

This is my code. I don't know what is wrong with my throw statement even if every input is right yet when I try to insert any data in my form it throws an error saying "Error creating contractor" but when I comment out the throw statement and run the code with and insert a new record it gets successfully added. Please tell me what is wrong in the code. 

class TransformationAssociate
{

[DataEventHandler(tableStr(TransformationAssociates), DataEventType::Inserted)]
public static void TransformationAssociates_onInserted(Common sender, DataEventArgs e)
{

try
{
NumberSequenceReference numberSequenceReference = NumberSeqReference::findReference(extendedTypeNum(DirPartyNumber));
NumberSequenceTable numberSequenceTable = NumberSequenceTable::find(numberSequenceReference.NumberSequenceId);
SysLastValue sysLastValue;



DirPartyEntity d;
TransformationAssociates eda=sender;
HcmContractorEntity contractor;

//1. Create party record
ttsbegin;
d.Name=eda.FirstName+" "+eda.LastName;
if (numberSequenceReference && numberSequenceTable)
{
if (!numberSequenceTable.Manual)
{
NumberSeq numberSeq = NumberSeq::newGetNumFromId(numberSequenceTable.RecId,
true,
true,
sysLastValue,
false);

if (numberSeq)
{
d.PartyNumber = numberSeq.num();
}
}
}


d.PartyType="Person";
d.insert();


//create contractor

contractor.PersonnelNumber = d.PartyNumber;
contractor.EmploymentLegalEntityId ="USMF";
contractor.EmploymentStartDate = eda.StartDate;
//contractor.CalendarId ="Flex";
contractor.FirstName = eda.FirstName;
contractor.KnownAs = eda.FirstName;
contractor.LanguageId = "en-US";
contractor.LastName = eda.LastName;

contractor.NameSequenceDisplayAs = "FirstLast";
contractor.PartyNumber = d.PartyNumber;
contractor.PrimaryContactEmail=eda.Email;
contractor.ProfessionalTitle = "Associate";
contractor.StartDate = eda.StartDate;

contractor.insert();

HcmPosition position;
NumberSequenceReference numberSequenceReferencePosition = NumberSeqReference::findReference(extendedTypeNum(HcmPositionId));
NumberSequenceTable numberSequenceTablePosition = NumberSequenceTable::find(numberSequenceReferencePosition.NumberSequenceId);
HcmPositionId positionId;
if (numberSequenceReferencePosition && numberSequenceTablePosition)
{
if (!numberSequenceTablePosition.Manual)
{
// generate ID if number sequence is not set to Manual.
NumberSeq numberSeqPosition = NumberSeq::newGetNumFromId(
numberSequenceTablePosition.RecId,
true,
true,
sysLastValue,
false);

if (numberSeqPosition)
{
positionId = numberSeqPosition.num();
}
}
}

position.Activation=eda.StartDate;
position.AvailableForAssignment=eda.StartDate;
position.CompensationRegionId="Central";
position.DepartmentNumber="014";
position.Description="Associate";
position.TitleId="Account Manager";
position.WorkerPersonnelNumber=d.PartyNumber;
position.WorkerAssignmentStart=eda.StartDate;

position.insert();


throw Global :: error("Error creating contractor");


ttscommit;


Info("Associate created successfully");
}
catch (exception::Error) {
info("Associate not created ");
}


}


}

I have the same question (0)
  • udaY-ch Profile Picture
    5,128 on at

    hI

    Refactored your code in rich text formatting for easy readability. Please use rich text formatting next time when placing code,

    class TransformationAssociate
    {
        [DataEventHandler(tableStr(TransformationAssociates), DataEventType::Inserted)]
        public static void TransformationAssociates_onInserted(Common sender, DataEventArgs e)
        {
            try
            {
                NumberSequenceReference numberSequenceReference = NumberSeqReference::findReference(extendedTypeNum(DirPartyNumber));
                NumberSequenceTable numberSequenceTable = NumberSequenceTable::find(numberSequenceReference.NumberSequenceId);
                SysLastValue sysLastValue;
                
                DirPartyEntity d;
                TransformationAssociates eda=sender;
                HcmContractorEntity contractor;
                
                //1. Create party record
                ttsbegin;
                d.Name=eda.FirstName " " eda.LastName;
                if (numberSequenceReference && numberSequenceTable)
                {
                    if (!numberSequenceTable.Manual)
                    {
                        NumberSeq numberSeq = NumberSeq::newGetNumFromId(numberSequenceTable.RecId,
                        true,
                        true,
                        sysLastValue,
                        false);
                    
                        if (numberSeq)
                        {
                            d.PartyNumber = numberSeq.num();
                        }
                    }
                }
            
                d.PartyType="Person";
                d.insert();
                
                //create contractor
                contractor.PersonnelNumber = d.PartyNumber;
                contractor.EmploymentLegalEntityId ="USMF";
                contractor.EmploymentStartDate = eda.StartDate;
                //contractor.CalendarId ="Flex";
                contractor.FirstName = eda.FirstName;
                contractor.KnownAs = eda.FirstName;
                contractor.LanguageId = "en-US";
                contractor.LastName = eda.LastName;
                
                contractor.NameSequenceDisplayAs = "FirstLast";
                contractor.PartyNumber = d.PartyNumber;
                contractor.PrimaryContactEmail=eda.Email;
                contractor.ProfessionalTitle = "Associate";
                contractor.StartDate = eda.StartDate;
                
                contractor.insert();
                
                HcmPosition position;
                NumberSequenceReference numberSequenceReferencePosition = NumberSeqReference::findReference(extendedTypeNum(HcmPositionId));
                NumberSequenceTable numberSequenceTablePosition = NumberSequenceTable::find(numberSequenceReferencePosition.NumberSequenceId);
                HcmPositionId positionId;
                if (numberSequenceReferencePosition && numberSequenceTablePosition)
                {
                    if (!numberSequenceTablePosition.Manual)
                    {
                        // generate ID if number sequence is not set to Manual.
                        NumberSeq numberSeqPosition = NumberSeq::newGetNumFromId(
                        numberSequenceTablePosition.RecId,
                        true,
                        true,
                        sysLastValue,
                        false);
                    
                        if (numberSeqPosition)
                        {
                            positionId = numberSeqPosition.num();
                        }
                    }
                }
                
                position.Activation=eda.StartDate;
                position.AvailableForAssignment=eda.StartDate;
                position.CompensationRegionId="Central";
                position.DepartmentNumber="014";
                position.Description="Associate";
                position.TitleId="Account Manager";
                position.WorkerPersonnelNumber=d.PartyNumber;
                position.WorkerAssignmentStart=eda.StartDate;
                
                position.insert();
                
                
                throw Global :: error("Error creating contractor");
                
                
                ttscommit;
                
                
                Info("Associate created successfully");
            }
            catch (exception::Error) 
            {
                info("Associate not created ");
            }
        }
    }
    
    

  • Suggested answer
    udaY-ch Profile Picture
    5,128 on at

    HI

    Right after making insert call, you're using throw statement. why to have throw statement ?

    You haven't assigned position.positionId = positionId;

    Try using the below code, which will insert only validatewrite is true else error will be returned.

    if (position.validateWrite())
    {
        position.insert();
    }
    else
    {
        throw Global :: error("Error creating contractor");
    }

    Uday

  • Suggested answer
    vinitgoyal2005 Profile Picture
    6,332 on at

    Hi,

    You are throwing ERROR in transaction i.e. between ttsbegin and ttscommit. because of error, no changes are committed to database and you don't see any new record.  what is the purpose of that throw, there is no condition for throwing so it will always be executed. you may want to change the position of it and let us know more details about requirements so we can help you.

  • skd Profile Picture
    390 on at

    Hey Vinit,

    Actually I'm entering data into multiple tables and hence I want to make sure that the  data is entering into all tables and correct data is entering. Once the data is perfectly entered into all the tables I want it to give a message that "Associate created successfully" else if anything goes wrong then the error msg should throw.

  • WillWU Profile Picture
    22,361 on at

    Hi skd,

    The code will run the throw Global :: error("Error creating contractor") after the insert() method without any condition, it will throw an exception and rollback the insert statement.

    Read this DOC about how X++ exception handling:

    docs.microsoft.com/.../xpp-exceptions

    Can you get "Associate created successfully" now when throw Global :: error("Error creating contractor") is commented out?

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

    position.insert();
    
    throw Global :: error("Error creating contractor");
    
    ttscommit;
    
    Info("Associate created successfully");
    

    As mentioned by Will WU, your code explicitly says that the error should always be thrown after the insert.

    If you don't want to always throw the error, you have to define and develop some conditional statement and throw it only when your condition is met.

  • Suggested answer
    vinitgoyal2005 Profile Picture
    6,332 on at

    Hi,

    If you want to make sure all data is inserted, you need to use try..catch exception handling.

    Follow ms docs docs.microsoft.com/.../xpp-exceptions

    add your code into Try block and catch the error if any in the Catch block. you can show error here you don't even need to throw it. 

  • vinitgoyal2005 Profile Picture
    6,332 on at

    Hi,

    Were you able to resolve the issue? If not please let us know.

    If yes then please review and mark the answers as verified.

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
Martin Dráb Profile Picture

Martin Dráb 659 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 289 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans