locked
Using a form script to update a custom entity value with Web Services RRS feed

  • Question

  • I am stuck on how to update a custom entity with Web Services in CRM 4.0.  I am working from the great example of how to call web services with JavaScript here: http://blog.customereffective.com/blog/2008/03/custom-auto-inc.html

    I have the first three parts of the solution working...

    1. I read the current setting ID, name and value and parse the returned values into variables.

    ENTITY: new_customsetting

    SCHEMA:  new_customsettingid (ID), new_settingname (Text), new_settingvalue (Text)

    2. I validate the new_settingvalue is not in use in my Account table.  If it is in use, I increment until it finds a unique value.

    3. I stamp the new (assumed unique) value into the current Account record.

    4. I'm stuck when trying to write the new counter value back into my new_customsetting entity.  I am creating the XML and using previously declared variables but getting a server side error as follows.  I've been unable to find any clear documentation on any changes from CRM 3 to CRM 4.  The code snippet is from Will's CRM 3 example with the updated ns values and the SOAP header... but I'm not sure what else has changed.

    Any pointers in the right direction are appreciated.

    Trace snippet:

    >SOAP Request:
    
    
    
    Address:http://crm/mscrmservices/2007/crmservice.asmx
    
    
    
    URL:2007/crmservice.asmx
    
    
    
    HttpHeader:SOAPAction: http://schemas.microsoft.com/crm/2007/WebServices/Execute
    
    
    
    SOAP Xml:
    
    
    
    <?xml version="1.0" encoding="utf-8"?>
    
    
    
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    
    
    
    <soap:Header>
    
    
    
    <CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
    
    
    
    <AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">0</AuthenticationType>
    
    
    
    <OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">MBMSI</OrganizationName>
    
    
    
    <CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>
    
    
    
    </CrmAuthenticationToken>
    
    
    
    </soap:Header>
    
    
    
    <soap:Body>
    
    
    
    <Request xsi:type="UpdateRequest" xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
    
    
    
    <Target xsi:type="TargetUpdatenew_customsetting">
    
    
    
    <new_customsetting>
    
    
    
    <new_customsettingid>{BB397CDD-5E59-DF11-B4A9-00155D0B4801}</new_customsettingid>
    
    
    
    <new_settingvalue>997935</new_settingvalue>
    
    
    
    </new_customsetting>
    
    
    
    </Target>
    
    
    
    </Request>
    
    
    
    </soap:Body>
    
    
    
    </soap:Envelope>
    
    
    
    

    Trace error:

    Crm Exception: Message: request, ErrorCode: -2147220989, InnerException: System.ArgumentNullException: Value cannot be null.
    
    
    
    
    
    
    
    Parameter name: request
    
    
    
    

    Code snippet:

    xmlPost = '<?xml version="1.0" encoding="utf-8"?>'+
    
    
    
    '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" '+
    
    
    
    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+
    
    
    
    '<soap:Header>'+
    
    
    
    '<CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">'+
    
    
    
    '<AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">0</AuthenticationType>'+
    
    
    
    '<OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">MBMSI</OrganizationName>'+
    
    
    
    '<CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>'+
    
    
    
    '</CrmAuthenticationToken>'+
    
    
    
    '</soap:Header>'+
    
    
    
    '<soap:Body>'+
    
    
    
    '<Request xsi:type="UpdateRequest" xmlns="http://schemas.microsoft.com/crm/2007/WebServices">'+
    
    
    
    '<Target xsi:type="TargetUpdatenew_customsetting">'+
    
    
    
    '<new_customsetting>'+
    
    
    
    '<new_customsettingid>' + participantid_guid + '</new_customsettingid>'+
    
    
    
    '<new_settingvalue>' + counter + '</new_settingvalue>'+
    
    
    
    '</new_customsetting>'+
    
    
    
    '</Target>'+
    
    
    
    '</Request>'+
    
    
    
    '</soap:Body>'+
    
    
    
    '</soap:Envelope>';
    
    
    
    xmlhttp.open("POST", serverUrl, false); 
    
    
    
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
    
    
    
    xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
    
    
    
    //debugger;
    
    
    
    xmlhttp.send(xmlPost);
    
    
    
    alert('It should be written now...');
    
    
    
    
    Thanks for the help.
    Monday, May 10, 2010 4:42 AM

Answers

  • Hi Paul,

    Please try updating the record using CrmServcie's update method instead of UpdateRequest class.

       var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    " <soap:Header>" +
    "  <CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    "   <AuthenticationType xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" +
    "   <OrganizationName xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">MBMSI</OrganizationName>" +
    "   <CallerId xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" +
    "  </CrmAuthenticationToken>" +
    " </soap:Header>" +
    " <soap:Body>" +
    "  <Update xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    "   <entity xsi:type=\"new_customsetting\">" +
    "    <new_customsettingid>"+ participantid_guid +"</new_customsettingid>" +
    "    <new_settingvalue>"+counter+"</new_settingvalue>" +
    "   </entity>" +
    "  </Update>" +
    " </soap:Body>" +
    "</soap:Envelope>" +
    "";
    


    Regards,

    Nishant Rana

     

     

     


    http://nishantrana.wordpress.com
    • Proposed as answer by Edwin2win Tuesday, May 11, 2010 4:58 AM
    • Marked as answer by Jim Glass Jr Tuesday, May 11, 2010 4:05 PM
    Monday, May 10, 2010 6:08 AM

All replies

  • Hi Paul

    i think you should use GetAuthenticationHeader() method above instead of manually hard coding the organization name and other Id's for authentication in soap header. That could be the reason of breaking of the above code on CRM 4. see the example below.

    var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    GenerateAuthenticationHeader() +
    " <soap:Body>" +
    " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
    " <q1:EntityName>account</q1:EntityName>" +
    " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
    " <q1:Attributes>" +
    " <q1:Attribute>name</q1:Attribute>" +
    " <q1:Attribute>accountid</q1:Attribute>" +
    " </q1:Attributes>" +
    " </q1:ColumnSet>" +
    " <q1:Distinct>false</q1:Distinct>" +
    " <q1:PageInfo>" +
    " <q1:PageNumber>1</q1:PageNumber>" +
    " <q1:Count>1</q1:Count>" +
    " </q1:PageInfo>" +
    " </query>" +
    " </RetrieveMultiple>" +
    " </soap:Body>" +
    "</soap:Envelope>" +
    "";

    http://www.stunnware.com/crm2/topic.aspx?id=jswebservice2

    http://jianwang.blogspot.com/2008/12/crm-40-get-attribute-value-from-entitys.html

     

     


    Muhammad Ali Khan
    My MS CRM blog
    Monday, May 10, 2010 5:37 AM
  • Hi Paul,

    Please try updating the record using CrmServcie's update method instead of UpdateRequest class.

       var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    " <soap:Header>" +
    "  <CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    "   <AuthenticationType xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" +
    "   <OrganizationName xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">MBMSI</OrganizationName>" +
    "   <CallerId xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" +
    "  </CrmAuthenticationToken>" +
    " </soap:Header>" +
    " <soap:Body>" +
    "  <Update xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    "   <entity xsi:type=\"new_customsetting\">" +
    "    <new_customsettingid>"+ participantid_guid +"</new_customsettingid>" +
    "    <new_settingvalue>"+counter+"</new_settingvalue>" +
    "   </entity>" +
    "  </Update>" +
    " </soap:Body>" +
    "</soap:Envelope>" +
    "";
    


    Regards,

    Nishant Rana

     

     

     


    http://nishantrana.wordpress.com
    • Proposed as answer by Edwin2win Tuesday, May 11, 2010 4:58 AM
    • Marked as answer by Jim Glass Jr Tuesday, May 11, 2010 4:05 PM
    Monday, May 10, 2010 6:08 AM
  • Nishant - thank you.  By updating from the Execute Method to the Update Method, all is now working as expected and it keeps the syntax simpler and easier to follow.

    Again, many thanks. PB

    Monday, May 10, 2010 8:04 PM