0

I'm stuck on the XrmServiceToolkit.Soap.setState() function.

Prerequisites
I'm on studying MS CRM. One of my tasks is to activate and deactivate a record (this time it's a native "contact" record). I've decided to build my solution in a following way (according to other tasks):

  1. On the OnLoad event of my custom entity's form I create a new contact using XrmServiceToolkit (task condition), then update one field in newly created contact. This actually doesn't make sense in production to create something on this event, but nevertheless, is done this way only for my convenience:
function Practice3()
{
    let entityToUpdate = CreateRecordWithToolkit("VP_Created by","Xrm Service Toolkit","4F52999B-0E63-EA11-8125-00155D06F203");
    alert("Switching to new record!");
    Xrm.Utility.openEntityForm("contact", entityToUpdate);
    UpdateFieldsWithXrmServiceToolkit(entityToUpdate);
    DeactivateAndActivateContact(entityToUpdate);
}

// I don't post the CreateRecordWithToolkit(fname, lname, accountId) and UpdateFieldsWithXrmServiceToolkit(targetEntity) functions 'cos all is Ok there
// and entityToUpdate GUID is passed properly
  1. Between creation and posting updates I send a command to load that newly created form. It does'n activated it at once, but updates are inserted correctly.
  2. Then I pass the same GUID to my next function DeactivateAndActivateContact(targetEntity) and here I stuck.

Question
Can anyone explain or give a hint on how to use the XrmServiceToolkit.Soap.setState() function? I can't get the "entityName" parameter - what is this? How can I get it with having entity's GUID? I was trying to use it like this:

function  DeactivateAndActivateContact(targetEntity)
{
    let entityName = GetObjectAttribute(targetEntity, "contact", "name");
    XrmServiceToolkit.Soap.setState(entityName,targetEntity, "0", "0",false);
    Xrm.Page.data.refresh(true).then(null, null);
}

but getting an undefined error.

Is there any way to get an entityName by GUID? 'cos I'm getting the name of current form.
Error is raised when stepping in to this line:

XrmServiceToolkit.Soap.setState(entityName,targetEntity, "0", "0",false);
Vitaliy Prushak
  • 1,057
  • 8
  • 13
  • question, Are you using Dynamics 365 and not CRM 2011 or 2013. Reason SOAP request are long deprciated in Dynamics and they are updated with REST API. Let me know your dynamics version and I can add an answer to it. – AnkUser Mar 26 '20 at 13:14
  • It's a Dynamics 365 – Vitaliy Prushak Mar 27 '20 at 20:12

1 Answers1

1

To Deactivate contact here is below code,

Note Rest API call is Asynchronous and You will have to use execution context and formcontext in Dynamics

Also you could change your other SOAP calls with Rest API. Refer to CRM Rest Builder, it's way easy to build calls with it.

var entity = {};
entity.statuscode = 2;
entity.statecode = 1;

    Xrm.WebApi.online.updateRecord("contact", "4A342F12-D248-E211-8669-005056922461", entity).then(
        function success(result) {
            var updatedEntityId = result.id;
formContext.data.refresh(yes).then(successCallback, errorCallback);
        },
        function(error) {
            Xrm.Utility.alertDialog(error.message);
        }
    );
AnkUser
  • 5,421
  • 2
  • 9
  • 25
  • The matter is that I do need this for use exactly in XrmServiceToolkit.Soap.setState(). But anyway - thanks for an answer and upvote! – Vitaliy Prushak Mar 28 '20 at 22:02