locked
CRM 2013 - Javascript - Open New Record Of A Different Entity RRS feed

  • Question

  • Hi

    In my order form the user can input a address for a ship to address. They have the option to save this address via a decision box, if they click yes i'd like CRM to open a new form displaying the entity customeraddress so that the user can save the new address to CRM, a added extra would be if the user doesn't have to re-input the data it does it for them

    I've tried the following JavaScript code but had a bad error request when the window opened

       var AccountGuid = Xrm.Page.data.entity.getId();
                  var AccountName = Xrm.Page.data.entity.attributes.get("name").getValue();
                  var parameters = {};
                  parameters["title"] = "Save New Address";
                  parameters["casetypecode"] = "3";
                  parameters["customerid"] = AccountGuid;
                  parameters["customeridname"] = AccountName;
                  parameters["customeridtype"] = "account";
    
                  Xrm.Utility.openEntityForm("customeraddress",null,parameters);
    

    Thanks, Shaun


    S.Harrison

    Monday, July 7, 2014 3:21 PM

Answers

  • Hi Shaun,

    The error was due to the fact that the field names you used like title, customerid, customeridname, etc. do not exist in the customer address entity. You probably picked them off from the Order entity. When you're opening an entity form and you're passing parameters, the field names need to be of the entity being opened, i.e. customeraddress in this case. Always check in the customizations for correct field names before passing them.


    Admin QuikView Solution for CRM 2013

    Wednesday, July 9, 2014 8:55 AM

All replies

  • Hi,

    I think the Xrm.Page.data.entity.getId() returns the id of the records with {}. So, setting the "{guid}" could cause issue. Use the JavaScript trim or replace methods to remove the {} characters.


    Persian Dynamics CRM Community | انجمن متخصصان فارسی زبان

    Monday, July 7, 2014 3:52 PM
    Moderator
  • "a added extra would be if the user doesn't have to re-input the data it does it for them"

    Thanks, Shaun


    S.Harrison

    As Payman mentioned, it looks like your including the curly braces. So this should do the trick:

    var AccountGuid = Xrm.Page.data.entity.getId();
                  AccountGuid = AccountGuid.replace('{', '').replace('}', '');
                  var AccountName = Xrm.Page.data.entity.attributes.get("name").getValue();
                  var parameters = {};
                  parameters["title"] = "Save New Address";
                  parameters["casetypecode"] = "3";
                  parameters["customerid"] = AccountGuid;
                  parameters["customeridname"] = AccountName;
                  parameters["customeridtype"] = "account";
    
                  Xrm.Utility.openEntityForm("customeraddress",null,parameters);

    As for the user not having to re-enter data:

    This msdn link explains the use of attribute mappings, a technique you can use to auto-fill the values of a form based on the values of the previous form

    Monday, July 7, 2014 4:07 PM
  • You can also use the "CreateFromId" url format which will automatically respect the attribute mappings.

    example:
    http://SERVER:5555/ORGANIZATION/main.aspx?etc=1084&extraqs=%3f_CreateFromId%3d%257bE9F4AA7E-987A-E111-A842-0050568370A8%257d%26_CreateFromType%3d3%26etc%3d1084&pagetype=entityrecord

    Here is the way stuff breaks down:

    http://SERVER:5555/ORGANIZATION
    main.aspx?etc=1084  //1084=Entity Type to be created
    &extraqs=%3f_CreateFromId%3d%257b8292F618-897D-E111-A842-0050568370A8%257d  //GUID Of Parent Record
    %26_CreateFromType%3d3   //3=Originating Entity Type
    %26etc%3d
    1084&pagetype=entityrecord //1084=Entity Type to be created

    Monday, July 7, 2014 7:05 PM
  • Thanks for all your replies,

    I tried the code with the replace in there, unfortunately same error still shows up

    Thanks, Shaun


    S.Harrison

    Tuesday, July 8, 2014 7:53 AM
  • I just checked to see what AccountGuid returns and it's returning nothing, I'm assuming this is the problem 

    Thanks, Shaun


    S.Harrison

    Tuesday, July 8, 2014 8:36 AM
  • Hi Shaun,

    The form where the script is supposed to be executing, is it a new record form? Or are you opening an existing record form?

    The only way "Xrm.Page.data.entity.getId();" can return blank is if the script is executing on a new record form and the record Guid is not yet available.


    Admin QuikView Solution for CRM 2013

    Tuesday, July 8, 2014 8:54 AM
  • Hi, When I tried it was with a new record, which ideally I'd prefer as at the moment once the record is created the data should all be present therefore I've locked the fields once the account has been created

    Is there a way around this issue or do I have to change stuff around?

    Thanks, Shaun


    S.Harrison

    Tuesday, July 8, 2014 8:59 AM
  • Hi Shaun,

    I have a question here. You say you're trying to add a new Customer Address against an Account from the Order form, right? If that is the case, then why are you using "Xrm.Page.data.entity.getId();" in the script that fires in the Order form? That would try to give you the Order record Id (which is not yet created and would hence give you a blank field).

    I believe you're trying to associate the Customer Address against the Account selected in the Customer field in the Order form. In that case, to get the Account Id, you'll need to use something like

    Xrm.Page.getAttribute("customerid").getValue();

    and then of course extract the Id from it.


    Admin QuikView Solution for CRM 2013

    Tuesday, July 8, 2014 10:10 AM
  • Hi Dynamotion

    Thanks for your help

    I was doing that because that was in the code I found, I didn't realize it would return a blank guid as it wasn't created

    I'll try your way now and get back to you,

    Thanks Again

    Shaun


    S.Harrison

    Tuesday, July 8, 2014 10:35 AM
  • Hi everyone, the guid now doesn't have {} but the error on the pop up screen is still the same

    Thanks, Shaun


    S.Harrison

    Tuesday, July 8, 2014 12:13 PM
  • Hi Shaun,

    Are you using the same logic and code?


    Admin QuikView Solution for CRM 2013

    Tuesday, July 8, 2014 7:28 PM
  • Hi Shaun

    One thing at a time, try this first:

    Xrm.Utility.openEntityForm("customeraddress");

    If that works then you know the problem is with the parameters you are passing, if it doesn't work then you are probably not using the entities logical name.

    Then take out one argument and a time until you track it down.

    Can you post the exact error message?

    Wednesday, July 9, 2014 1:18 AM
  • Hi guys

    Thanks for the replies

    With regards to MichaelBarry's post when i removed the parameters as suggested the form did open as I wanted

    I'll now follow his instructions and insert one parameter at a time to see when the error accures

    With regards to Dynamotion's post the code is as follows

    var custId = Xrm.Page.getAttribute("customerid");
    var CustomerID = "";
     
    if (custId !=null)
     {
       var custIDObject = custId.getValue();
      
       if (custIDObject !=null)
       {
         CustomerID = custIDObject[0].id;
       }
      }
    CustomerID = CustomerID.replace("{","");
    CustomerID =CustomerID .replace("}","");
    
    var AccountName = custIDObject[0].name;
              	
    var parameters = {};
    
    parameters["title"] = "Save New Address";
    parameters["casetypecode"] = "3";
    parameters["customerid"] = CustomerID;
    parameters["customeridname"] = AccountName;
    parameters["customeridtype"] = "account";
              	Xrm.Utility.openEntityForm("customeraddress",null,parameters);
    
    

    Thanks for all your help

    Shaun


    S.Harrison

    Wednesday, July 9, 2014 7:41 AM
  • Okay so I put the following back in

    parameters["title"] = "Save New Address";
              	Xrm.Utility.openEntityForm("customeraddress",null,parameters);

    And the error came back which is as follows

    Error

    An error has occured

    Try this action again, if the problem continues, check the Microsoft Dynamics CRM Community for solutions or contact......

    Thanks, Shaun


    • Thanks for all your help

      Shaun


      S.Harrison

      a few seconds ago
      Avatar of Shaun Harrison
      0 Points
      <form style="margin:0px;padding:0px;border:0px;outline:0px;font-weight:inherit;font-style:inherit;font-family:inherit;"></form>


    • Thanks for all your help

      Shaun


      S.Harrison

      a few seconds ago
      Avatar of Shaun Harrison
      0 Points
      <form style="margin:0px;padding:0px;border:0px;outline:0px;font-weight:inherit;font-style:inherit;font-family:inherit;"></form>


    S.Harrison

    Wednesday, July 9, 2014 7:50 AM
  • Hi Shaun,

    Try this code, did some digging and found out some field names do not exist, so the error.

    var custId = Xrm.Page.getAttribute("customerid");
    var CustomerID = "";
    
    if (custId !=null)
    {
    	var custIDObject = custId.getValue();
    
    	if (custIDObject !=null)
    	{
    		ustomerID = custIDObject[0].id;
    	}
    }
    
    CustomerID = CustomerID.replace("{","");
    CustomerID =CustomerID .replace("}","");
    
    var parameters = {};
    
    parameters["parentid"] = CustomerID;
    parameters["objecttypecode"] = "1";
    Xrm.Utility.openEntityForm("customeraddress",null,parameters);
    

    The field names that I have omitted do not exist in the customeraddress entity.


    Admin QuikView Solution for CRM 2013

    Wednesday, July 9, 2014 8:49 AM
  • Further Update

    Basically any sort of parameter is causing the error message to appear

    Thanks, Shaun


    S.Harrison

    Wednesday, July 9, 2014 8:49 AM
  • Hi Shaun, See my reply above. The code should work!

    Admin QuikView Solution for CRM 2013

    Wednesday, July 9, 2014 8:50 AM
  • Hi 

    Yes the code does work, was my error in naming the fields from the wrong entity?

    Thanks, Shaun


    S.Harrison

    Wednesday, July 9, 2014 8:52 AM
  • Hi Shaun,

    The error was due to the fact that the field names you used like title, customerid, customeridname, etc. do not exist in the customer address entity. You probably picked them off from the Order entity. When you're opening an entity form and you're passing parameters, the field names need to be of the entity being opened, i.e. customeraddress in this case. Always check in the customizations for correct field names before passing them.


    Admin QuikView Solution for CRM 2013

    Wednesday, July 9, 2014 8:55 AM
  • Hi All, been a while since I've been here!
    I'm trying to do something similar to the above, however when I am trying to pass Lookups over to a new record I always seem to get an error, the lines of code is as follows;

    function cloneQuote()
    {
      var id =Xrm.Page.data.entity.getId();
      var shipping = Xrm.Page.getAttribute("new_shippingmethodtext").getValue();
      var freight = Xrm.Page.getAttribute("new_freighttermstext").getValue();
      var payment = Xrm.Page.getAttribute("new_paymentterms").getValue();
      var faxnum = Xrm.Page.getAttribute("new_quotefax").getValue();
    
      var accountID = Xrm.Page.getAttribute("customerid");
      var AccountIDObj= accountID.getValue();
      AccountID= AccountIDObj[0].id;
      AccountName = AccountIDObj[0].name;
    
     
      var parameters = {};
      parameters["customerid"] = "customerid=AccountID&customeridname=AccountName&customeridtype=account";
    
      parameters["new_quotecopyid"] =id;
      parameters["new_shippingmethodtext"] = shipping;
      parameters["new_freighttermstext"] = freight;
      parameters["new_paymentterms"] = payment;
      parameters["new_quotefax"] = faxnum;
      Xrm.Utility.openEntityForm("quote",null,parameters);
    }
    


    S.Harrison

    Friday, March 20, 2015 12:49 PM