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 :
Microsoft Dynamics CRM (Archived)

Create Record Using Javascript Form Custom Button

(0) ShareShare
ReportReport
Posted on by

I use crm 2016 on-premmis. I want to create a record by clicking the custom button using javascript. I create button using ribbon workbench. In the solution, i add jquery-1.9.1.js as a webresources. I write simple code, just to try if the custom button is work or not.

function createAccount() {
  var account = {};
 account.Name = "Tes 001";
 account.Description = "Create Account Test";
 
 //Create the Account
 SDK.JQuery.createRecord(
     account,
     "account",
     function (account) {
      writeMessage("The account named \"" + account.Name + "\" was created with the AccountId : \"" + account.AccountId + "\".");
      writeMessage("Retrieving account with the AccountId: \"" + account.AccountId + "\".");
      retrieveAccount(account.AccountId)
     },
     errorHandler
   );
     
}


I call this function from custom button, but it seem doesn't work. Nothing happen when i click the custom button. Did i'm missing the requirements that make the function dont work ?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    To make your code work you should add a reference to webresources that you use and it's not only jQuery but following as well - msdn.microsoft.com/.../gg309549(v=crm.7).aspx

    To use mentioned jQuery and SDK.jQuery webresources in ribbon you can use following approach - madcomputerist.blogspot.co.uk/.../referencing-multiple-javascript.html

  • Community Member Profile Picture
    on at

    Hi Andrii,

    I have add reference to webresources and add both jquery 1.9.1 and SDK.Jquery.

    I follow this link to add reference webresources:

    https://rajeevpentyala.com/2014/05/25/loading-dependent-jscript-libraries-in-ribbon-buttons-execution-crm-2013/

    This is my code inside customization.xml from the solution folder:

    <RibbonDiffXml>
            <CustomActions>
              <CustomAction Id="new.contact.Button1.Button.CustomAction" Location="Mscrm.HomepageGrid.contact.MainTab.Management.Controls._children" Sequence="60">
                <CommandUIDefinition>
                  <Button Command="new.contact.Command0.Command" Id="new.contact.Button1.Button" LabelText="$LocLabels:new.contact.Button1.Button.LabelText" Sequence="60" TemplateAlias="o2" />
                </CommandUIDefinition>
              </CustomAction>
            </CustomActions>
            <Templates>
              <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
            </Templates>
            <CommandDefinitions>
              <CommandDefinition Id="new.contact.Command0.Command">
                <EnableRules />
                <DisplayRules />
                <Actions>
                  <JavaScriptFunction FunctionName="IsNaN" Library="$webresource:new_jquery1.9.1" />
                  <JavaScriptFunction FunctionName="IsNaN" Library="$webresource:new_JquerySdk" />
                  <JavaScriptFunction FunctionName="IsNaN" Library="$webresource:new_jquery_1.9.1.min" />
                  <JavaScriptFunction FunctionName="createAccount" Library="$webresource:new_createContact" />
                </Actions>
              </CommandDefinition>
            </CommandDefinitions>
            <RuleDefinitions>
              <TabDisplayRules />
              <DisplayRules />
              <EnableRules>
                <EnableRule Id="new.contact.EnableRule0.EnableRule">
                  <SelectionCountRule AppliesTo="SelectedEntity" Minimum="1" />
                </EnableRule>
              </EnableRules>
            </RuleDefinitions>
            <LocLabels>
              <LocLabel Id="new.contact.Button1.Button.LabelText">
                <Titles>
                  <Title description="Button" languagecode="1033" />
                </Titles>
              </LocLabel>
            </LocLabels>
          </RibbonDiffXml>


  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    First - there is no IsNaN function. There is isNaN.

    Second - don't use both jQuery and jQuery.min it has no sense.

  • Community Member Profile Picture
    on at

    I changed the name of functions and remove jquery.min.

    Still not working :(

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    That means that something is wrong with your code. You will have to troubleshoot your code - blogs.msdn.microsoft.com/.../debugging-custom-javascript-code-in-crm-using-browser-developer-tools

  • Community Member Profile Picture
    on at

    I follow this link too, but its not run like before:

    lakshmanindian.wordpress.com/.../update-selected-records-in-home-page-entity-grid-in-crm-2011

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Unfortunately I can't help you more. I suggested everything I know and as usual - there are no miracles, always exists explanation why your code or customization doesn't work the way you wanted.

    May the troubleshooting be with you!

  • Ramakanta Profile Picture
    2,715 on at

    Hi ,

         Why you are not using the new Web API introduced in MCRM2016..?As we know that the Organization Data services will replaced by Web API.....this link may help you..

    himbap.com/blog;

          

  • Verified answer
    Community Member Profile Picture
    on at

    Hii, Ramakanta

    I found an error within my script.  Can't using getServerUrl for CRM 2016. I need to use getClientUrl and my script is working now. But i had a problem when i want to update the record. Take a look at my update function:

    function UpdateContact(accountId, accountName) {
        var serverUrl = Xrm.Page.context.getClientUrl().toString();
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var ODATA_EntityCollection = "/ContactSet";
    
        var objContact = new Object();
        // set the name of Account
        objContact.Id = Xrm.Page.data.entity.getId();
    
        // set the Primary Contact lookup field
        objContact.parentcustomerid= { Id: accountId, LogicalName: "account", Name: accountName };
    
        // Parse the entity object into JSON 
        var jsonEntity = window.JSON.stringify(objContact);
    
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
            data: jsonEntity,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (response) {
                if (response != null && response.d != null) {
                    alert(response.d.ContactId);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            }
        });
    }


    The error is on the Line :

    objContactId.Id =  Xrm.Page.data.entity.getId();

    i think its not return a guid from selected record.

  • Suggested answer
    Goutham A Profile Picture
    2 on at

    HI Fikri,

    In 2016,You can use new CRM web api  instead of this. Its simple and gives high performance.

    lakshmanindian.wordpress.com/.../create-record-using-jscript-in-microsoft-dynamics-crm-2011

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans