Get an entityName by GUID, despite of what form is loaded at the moment for use in XrmServiceToolkit.Soap.setState()

255 views Asked by At

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);
1

There are 1 answers

1
AnkUser On

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);
        }
    );