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 :
Small and medium business | Business Central, N...
Answered

Custom action to create new contact

(0) ShareShare
ReportReport
Posted on by

I am trying to create a new contact based on the records in another table through an action.

when I run the action the records in my table will create a new contact with the name and id mentioned , now new contact are created but data is not transfering ie the name and id is validating.

 please guide me were i went wrong ,Thanks in Advance


procedure Createcontact()
    var
        contact: Record Contact;
        radr: Record Radr;
    begin
        IF radr.RADRID <> 0 THEN BEGIN
            radr.Get(rec.RADRID);
            IF contact.FindSet() then
                repeat
                    contact.Init();
                    contact.Validate(RADRID, radr.RADRID);
                    contact.Validate("Name 2", radr.NAME2);
                    contact.Insert(true);
                until radr.Next = 0;
        end
    end;

I have the same question (0)
  • Verified answer
    YUN ZHU Profile Picture
    95,595 Super User 2025 Season 2 on at

    Hi, radr.get will only have one data, why add a loop here?

    pastedimage1672275346623v1.png

    I update the code, because I do not know the structure of the table Radr, no detailed test. You can try to see if it works.

        procedure Createcontact()
        var
            contact: Record Contact;
            radr: Record Radr;
        begin
            if radr.Get(rec.RADRID) then begin
                contact.Reset();
                contact.SetRange(RADRID, radr.RADRID);
                if contact.IsEmpty then begin
                    contact.Init();
                    contact.Validate(RADRID, radr.RADRID);
                    contact.Validate("Name 2", radr.NAME2);
                    contact.Insert(true);
                end;
            end
        end;

    Thanks.

    ZHU

  • Community Member Profile Picture
    on at

    Thanks yzhums  , Iam using  loop because i have 100 records  now its working for a single record , but how can i insert multiple records ie how to create 100 contact with different radrid when i tried it gives me error -->The record in table Contact already exists. Identification fields and values: No.='xxx115',how can i create 100 contacts through single action

  • Community Member Profile Picture
    on at

    procedure Createcontact()
        var
            contact: Record Contact;
            radr: Record Radr;
        begin
            if radr.RADRID <> 0 then
                contact.Reset();
            if radr.FindSet() then
                repeat
                    contact.Init();
                    contact.Validate(Name, radr.NAME1);
                    contact.Validate(Radrid, radr.RADRID);
                    contact.Insert(true);
                until radr.Next = 0;
        END;

  • Suggested answer
    YUN ZHU Profile Picture
    95,595 Super User 2025 Season 2 on at

    Hi, I don't know what your purpose is, but it's really not recommended.

    The following code is based on all Radr to create contacts. f the RADRID is already duplicated, skip creating it. You can modify this part of the code to achieve your purpose.

        procedure Createcontact()
        var
            contact: Record Contact;
            radr: Record Radr;
        begin
            radr.Reset();
            if radr.FindSet() then
                repeat
                    contact.Reset();
                    contact.SetRange(RADRID, radr.RADRID);
                    if contact.IsEmpty then begin
                        contact.Init();
                        contact.Validate(RADRID, radr.RADRID);
                        contact.Validate("Name 2", radr.NAME2);
                        contact.Insert(true);
                    end;
                until radr.Next() = 0;
        end;

    Hope this helps.

    Thanks

    ZHU

  • Community Member Profile Picture
    on at

    Thanks for the response

    Actually iam doing this because i have imported data into the radr table and now i have create new contacts from this data ,but still no luck with last code, Thanks

  • Suggested answer
    DAnny3211 Profile Picture
    11,397 on at

    hi

    your correct loop is:

    radr.Reset();

           if radr.FindSet() then

               repeat

                   contact.Reset();

                   contact.SetRange(RADRID, radr.RADRID);

                   if contact.findfirst then begin

                       contact.Init();

                       contact.Validate(RADRID, radr.RADRID);

                       contact.Validate("Name 2", radr.NAME2);

                       contact.Insert(true);

                   end;

               until radr.Next() = 0;

    DAniele

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at
    [quote user="sainudheen"]

    Thanks for the response

    Actually iam doing this because i have imported data into the radr table and now i have create new contacts from this data ,but still no luck with last code, Thanks

    [/quote]

    The "No." field is the primary key of the contact table so you need to assign that field a unique value for each Contact you are creating in your code.

    Contact."No" := YourUniqueContactNo;

    You can either pick that from a no, series or find another way to assign that number. But without it only the last contact you create will be visible in the contact table.

  • Community Member Profile Picture
    on at

    Okay, Thanks for the guidance now the contact is created,

    now iam facing a weird issue , iam trying to add an condition and on the findfirst it gives me an error on run time,any idea why this issue is coming

    the field radrid in the table refs is not a primary key so iam using this method but iam not able to get the record

    pastedimage1672334242509v1.png

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    To catch this error you should use the return value of the findfirst statement.

    So do if refs.findfirst then.........

    Then you will avoid the runtime error.

    Your problem is that the setrange you are doing does not match with any record in the table.

    You might also want to do a refs.reset(); before you do your refs.setrange. That will clear any filters you might have set earlier in your code that you do not want to use anymore.

  • Community Member Profile Picture
    on at

    Thanks for the Information,  I have modified the code like this no there is no any error and nothing is happening with this code,can you please guide where i went wrong thanks,

     procedure Refscontact()
        var
            contact: Record Contact;
            radr: Record Radr;
            refs: Record REFS;
        begin
            refs.Reset();
            refs.SetRange(RADRID, rec.RADRID);
            if refs.FindFirst() then begin
                radr.Get(rec.RADRID);
                if refs.RADRID = ' ' then Message('blank');
                IF radr.RADRID <> REFS.RADRID then begin
                    radr.Reset();//working
                    if radr.FindSet() then
                        repeat
                            contact.Reset();
                               if contact.findfirst then begin
                                contact.Init();
                                contact.Validate(RADRID, radr.RADRID);
                                contact.validate(Name, radr.NAME1);
                                contact.Insert(true);
                            end;
                        until radr.Next() = 0;
                end;

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 > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,010

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,270 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,085 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans