Create/Update Entity records using XrmServiceToolKit.Soap

If you want to create or update a record using the XrmServiceToolKit Soap and don’t know how to do it for all the field types, here is some code that might help you.

//Create Entity
var entity = new XrmServiceToolkit.Soap.BusinessEntity("EntityName");
//String
entity.attributes["fieldName"] = Xrm.Page.getAttribute("fieldName").getValue();

//Lookup
entity.attributes['fieldName'] = { id : Xrm.Page.getAttribute('fieldName').getValue()[0].id, logicalName : Xrm.Page.getAttribute('fieldName').getValue()[0].entityType, type : 'EntityReference' };

//Bool
entity.attributes["fieldName"] = Xrm.Page.getAttribute("fieldName").getValue();

//OptionSetValue
entity.attributes["fieldName"] = { value: Xrm.Page.getAttribute("fieldName").getValue(), type: "OptionSetValue" };

//Currency
entity.attributes["fieldName"] = { Value: Xrm.Page.data.entity.attributes.get("fieldName").getValue() };

//Decimal
entity.attributes["fieldName"] = { Value: parseFloat(eval(Xrm.Page.data.entity.attributes.get("fieldName").getValue())) };

entity.attributes["fieldName"] = { Value: 2, type: "int"}; 

entity.attributes["fieldName"] = { Value: 1.5617, type: "double" }; 

var quoteId;
quoteId = XrmServiceToolkit.Soap.Create(entity);

//Update Entity
var updateEntity = new XrmServiceToolkit.Soap.BusinessEntity("EntityName", entityGuid);
//Lookup to update
updateEntity.attributes["fieldName"] = { id : entityGuid, logicalName : 'entityName', type : 'EntityReference' };
//OptionSet
updateEntity.attributes["fieldName"] = { value : Value, type : 'OptionSetValue' };
//Currency
updateEntity.attributes["fieldName"] = { value : Value, type : 'Money' };
//Boolean
updateEntity.attributes["fieldName"] = { value : true/false, type: 'boolean' };
//String
updateEntity.attributes["fieldName"] = "Value";

updateEntity.attributes["fieldName"] = { Value: 2, type: "int"}; 

updateEntity.attributes["fieldName"] = { Value: 1.5617, type: "double" }; 

</pre>
<pre>var updateResponse = XrmServiceToolkit.Soap.Update(updateEntity ); 
Advertisements

14 thoughts on “Create/Update Entity records using XrmServiceToolKit.Soap

  1. Thank you! I could not find an example of dealing with a boolean field (aka “Two Options”) until I found this post.

    • I commented too soon. At least this was a starting after a bit of guessing and testing, I found out that updating a boolean field requires the parameter “type: ‘boolean'” at the end. So the entire line looks like:
      updateEntity.attributes[“fieldName”] = { value : true, type: ‘boolean’ };

      • Hi, I don’t know what version of CRM you tried, but I used like that in CRM 2013 SP1/2015 online and worked.

        But I will update the post with that info.

        Thank you.

      • I’m using CRM 2015 on-prem. Does it hurt to have the type parameter specified in 2013 / 2015 online? Another department in my company will be using CRM online for a different application so it would be good to know in case this line of code ever crops up.

      • I think it will work just fine, maybe my version was wrong and it never gave me an error (at the time I was trying on trial versions with lots of tests).

        Have tou tried with: updateEntity.attributes[“fieldName”] = true;
        instead of
        updateEntity.attributes[“fieldName”] = { value : true, type: ‘boolean’ };
        Maybe both will work.

      • I’m fairly certain I tried ‘updateEntity.attributes[“fieldName”] = true;’ first and it didn’t work which is what led me to search the internet and come here.

  2. Hi,
    Let say I have a look up field and on html it is a drop down, user can deselect previously selected item by selecting “– Please select –“, in such how should I update data ..??

    I tried
    1. 0
    2. null
    3. “00000000-0000-0000-0000-000000000000”

    all the way I failed.
    Please share your idea here,

    Thanks and regards,
    Rk_Hirpara

    • Hi, if you want to update the lookup with an empty value, you should pass null. Have you tried like this:     Xrm.Page.getAttribute(“field1”).setValue(null);
      Normaly it is set to null like that.

      • Miguel,
        Thanks for the contribution, but this is not what can work, as I have some stand alone HTML page i.e web resource.
        So I need solution in terms of following context

        testEntity.attributes[“test_attr”] = { id: “??????”, logicalName: ‘test_ref’, type: ‘EntityReference’ };

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s