Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to save same form more than one with different firms in crm 2015 using javascript?

(0) ShareShare
ReportReport
Posted on by 260

Hi,

I have a form. In this form i hav a lookup field .In this field i can look for a firm. The other fields of tihs form are text fields.

I can save this form to the database succesfully. According to my scenario; i added a 2 multiline text fields. To these fields i can store more than one id of a firm (comes from another page of the form.) The other multitex field can stroe more than one firm name.

I the other fields are such as bill number, amount etc. I want to save more than one firm in that form if the multitext fields has firm ids. I mean think that the form has 3 ids (guid) in the multitext field.After press the save button of the form  it should be save 3 rows with different firm ids in the database. But the other infos such as bill number, amount etc. should be same?

I wrote a custom library and add to the form OnSave .How can i do this in javascript in crm 2015? Can anyone help me please?

*This post is locked for comments

  • altun8tr Profile Picture
    altun8tr 260 on at
    RE: How to save same form more than one with different firms in crm 2015 using javascript?

    You are correct on this.

    When the user saves the CRM record i am trying to run Javascript that wil stroe the selected firms to the database of couurse. I nothing selected from the html web resource, i selcet one child firm from the lookup field and after press the save button on crm, the record will be saved to the database. The entity name is "new_destek_yerelreklam_detay" . The sql database table name is "new_destek_yerelreklam_detayBase"

    When child firms selected from the checkboxes from the html web resource, i leave the lookup filed empty. And the id's, names and brands of the selected firms are stored in multiline text field on the form.  

    When user presses the save button on crm form. The records should be saved to teh database ın separated rows.

    I mean if 3 child firms selected, 3 rows should be created to the database table that include each of the child firm infos.

    Ä° have nevere use Crm Web API . How can i use it here and where in the code?

    I also tried it using plugin. But on service.Create(entity); sentence it gave me error. When i debug it using registratiıon tool using profile , şit gave me porfile error on servive.Create(entity); sentence. When i did not use the profile and test the form on runtime, it gives me dublicate key , the key already exists erro message on the form. Because of this i wrote the code using javascript.

    How can i achieve this? How can i be successful. How can i do this using javascript?

  • Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: How to save same form more than one with different firms in crm 2015 using javascript?

    Okay, let me see if I understand. The CRM users can select one or more records (firms) from a list in an HTML web resource. You take the GUID for each of the selected records and write each GUID into a text field on the current record, each separated with a comma.

    Am I correct on this so far?

    Now, when the user saves the CRM record, you are trying to run JavaScript that will store each of the selected firms to... where???  What is the entity type that you want to create for each of the selected firms?

    To do this through JavaScript, you will need to use the CRM Web API.  I don't see in your code where you are attempting to create new records in CRM for each firm.

    So, the question is: For each selected firm, what type of CRM record do you want to create?

    One reason to do this in a plug-in, instead of JavaScript, is that you probably only want the new records to be created if the current record that the user is on gets saved successfully. However, in JavaScript in the OnSave function, your code will end up creating the new records even if the current record does not save successfully. Thus, using a "post-operation" plug-in for what you are doing is still the best design.

  • altun8tr Profile Picture
    altun8tr 260 on at
    RE: How to save same form more than one with different firms in crm 2015 using javascript?

    Is there any suggestion about this issue?

  • altun8tr Profile Picture
    altun8tr 260 on at
    RE: How to save same form more than one with different firms in crm 2015 using javascript?

    Hi,

    Thanks for yoyu advise Yadnyesh and Tim.

    I wanted to use a plugin . I wrote it but ı gut profiler erro int the statement service.create();

    Now using javascrpt i got the ids form multiline textbox one by one. When i came to the save pert noth,ing happens. the code loops again and again why i did not understant? Yadnyesh, i got the ids from a html web resource and sotre them in this form to multilinetex field. Anyway her is my javascript code; can anyone help me to save the records to the database, the save part does nto workinfg and the code loops again and again:

    function OnSave() {

       try {

      alert(getCurrentEntity().getId());//it gives me nothing

           var idler = getAttr("new_ids").getValue();//the ids of the child firms comes from the html web resource

           var magazaadlari=getAttr("new_multiline").getValue();//the chid firm names of the child firms comes from the html web resource

    var markalar = getAttr("new_markalar").getValue();//the brand numbers of the child firms comes from the html web resource

      var id = [];

      var magazaadi = [];

       var marka = [];

    //split from commas and stroe them to arrays

     var id= idler.split(',');

      var marka = markalar.split(',');

      var magazaadi= magazaadlari.split(',');

    //They are the form values

    //They should be same for each child firm.

    var tut = getAttr("new_tutar").getValue();

    var faturatarihi = getAttr("new_faturatarihi").getValue();

    var faturano = getAttr("new_faturano").getValue();

              var calismaturu = getAttr("new_calismaturu").getValue();

       var donem = getAttr("new_donem").getValue();

    //the counter

       var count=0;

       count=magazaadi.length;

    //The amount dvided by number of child firms

           tut = tut / (count);

    //counters

    var t=0;

     var i=0;

           if (getAttr("new_ids").getValue() == null) return;//check for empty multiline text field

              //trim the array

               for (i = 0; i < count; i++)

               {

                   id[i] = id[i].trim();

    magazaadi[i] = magazaadi[i].trim();

    marka[i] = marka[i].trim();

               }

    //The lookup field to select a child firm.

    //This should only works

    //When a child firm or child firms were not be selected from the html web resource.

    //Since It is oriinally put in the form.

    //I assign the values about the child firms from the multiline text fields one by one in a loop

       var frmLookupField = getField("new_magaza");

               for(t = 0; t < count; t++)

             {

    getAttr("new_faturatarihi").setValue(faturatarihi);

       getAttr("new_faturano").setValue(faturano);

      getAttr("new_calismaturu").setValue(calismaturu);

    getAttr("new_donem").setValue(donem);

       getAttr("new_tutar").setValue(tut);

              frmLookupField.setValue(setLookupValue("new_magaza", id[t],magazaadi[t],"new_destek_yerelreklam_detay"));

     if((count-1)==t)return;

    //not working

               Xrm.Page.data.entity.save();

               }

       } catch (e) { alert("OnLoad error:" + e.message); }

    }

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    Yadnyesh Kuvalekar 4,102 on at
    RE: How to save same form more than one with different firms in crm 2015 using javascript?

    Hi, Any specific reason to use multiline text to store GUIDs and then try to store multiple firms? Can't you use 1:N relationship? If you have one entity which can contain more than one firms then using 1:N is the correct solution. In this way, you will have to use create child records.

  • Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: How to save same form more than one with different firms in crm 2015 using javascript?

    Muharrem, I believe you were working on this same functionality within a plug-in. Are you now trying to get this to work using JavaScript?  Are you more familiar with C# (or VB.NET) and the CRM Organization Service operations or JavaScript and the Web API?

    If you want to do this in JavaScript, then on save you will get the value of the multi-text field that has the IDs. You will split/parse the values into an array. Next, loop through the array and query CRM (using Web API) to see if the related record already exists. If not, then use the Web API to create the record and associate it to the parent record (then one your user is currently on).

    How you implement this depends on your skills but also on whether using JavaScript and the Web API for this provides some advantage over using a plug-in. In the requirement that you presented, I think it's best to create the related records in a plug-in.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans