locked
Error on setting relationship between entities through javascript in CRM 2011. RRS feed

  • Question

  • Hi,

    I am getting error using CRM javascript SDK exampple to set relationship between two entity.

    I have attache image for the same.

    When i select any Contact from "Contact Test" Lookup (Currently Administrator is there),

    at that time there should create a relationship between this current form entity (in my case it is Training Custom Entity)

    and this Contact Entity, there is already M:M relationship between this entity (Training) and Contact Entity. 

    My js code is as below.

    function AssociateContactToTraining()
    {
        debugger;
        var TrainingId = Xrm.Page.data.entity.getId();;
        LookupFieldObject = Xrm.Page.data.entity.attributes.get("new_contacttestid");
        if (LookupFieldObject.getValue() != null) 
        {
            var ContactId = LookupFieldObject.getValue()[0].id;
            var ContactName = LookupFieldObject.getValue()[0].entityType;
        }
        
        try
        {
        SDK.REST.associateRecords(TrainingId,"Training","new_trainingcontactMtoM",ContactId,ContactName,
                                                       function () 
                                                       {
                                                           alert("Association successful.");
                                                           window.location.reload(true); 
                                                           return false;
                                                       },errorHandler);
        }
        catch(e)
        {
            alert("Error : " + e);
        }
    }

    function errorHandler()
    {
        alert("error");
    }

    But i am getting error.

    Please let me know if i am missing here something or wrong somewhere.


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Saturday, August 3, 2013 5:26 AM

Answers

  • Hi,

    Can you please post the error that you are getting? Please make sure that you are using Schema name properly. 

    I think when we read the Entity type through JavaScript it returns the Name while in Odata we have to provide Schema name. Please check ContactName variable (It should have value Contact not contact)

    Hope this helps!

    Thanks!

    • Marked as answer by DynamicsCRM31 Monday, August 5, 2013 7:31 AM
    Saturday, August 3, 2013 6:48 AM
  • Hi,

    Finally i got success. :-)

    I changed my code to 

    SDK.REST.associateRecords(TrainingId,"new_training","new_training_contactMtoM",ContactId,"Contact",successCallBack,errorHandler);

    Thanks Kalim.


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by DynamicsCRM31 Monday, August 5, 2013 7:30 AM
    Monday, August 5, 2013 7:30 AM

All replies

  • Hi,

    Can you please post the error that you are getting? Please make sure that you are using Schema name properly. 

    I think when we read the Entity type through JavaScript it returns the Name while in Odata we have to provide Schema name. Please check ContactName variable (It should have value Contact not contact)

    Hope this helps!

    Thanks!

    • Marked as answer by DynamicsCRM31 Monday, August 5, 2013 7:31 AM
    Saturday, August 3, 2013 6:48 AM
  • Ya. my schema name is correct.

    and i think schema name for Contact Entity is same as Contact, correct ?



    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Saturday, August 3, 2013 11:59 AM
  • Hi Milan,

    Actually there are two names to each attribute and entity i.e. Logical Name and Schema Name. Actually they are the same except that the name is the schema name but into lower case and when using the attribute names through plugins or web services or late bound queries We should use the name not the schema name. In case of ODATA or Early bound Queries we use Schema Name.

    Yes,schema name for Contact Entity is Contact, But you are reading it through below given JavaScript that returns “contact”. It is case sensitive.<o:p></o:p>

    var ContactName = LookupFieldObject.getValue()[0].entityType

    I am not able to post image as it is not allaowing me to do that.  You can refer below link to get the understanding on Schema name and Logical name.

    http://social.microsoft.com/Forums/en-US/e08b6c06-1f8f-4be6-9833-0de54fa71acc/crm-2011-atributes-name-schemaname-and-displayname

    It should be as given below.

    SDK.REST.associateRecords(TrainingId,

            "new_Training",

            "RelationshipName",

            ContactId,

            "Contact",

            function ()
    {
    alert("Association successful.");
    window.location.reload(true);
    return false;
    },errorHandler);

    Please use proper schema name for both entity and attribute name. I think your Training entity is custom entity. You have specified the name as "Training". Please provide proper schema name with prefix.

    I didnt find any other issue in the code. Sometime JavaScript throws an error when we try to associate same object twice.  If it doesn't solve the issue please post the error that you are getting.

    Hope this helps!


    Saturday, August 3, 2013 4:10 PM
  • Thanks Kalim,

    I have changes all as per mentioned.

    Noe my function look like 

    SDK.REST.associateRecords(TrainingId,"new_Training","new_training_contactMtoM",ContactId,"Contact",successCallBack,errorHandler);

    I am getting error now as below.

    Error 404 : Not Found: Resource Not Found for the segment 'new_TrainingSet'.


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Monday, August 5, 2013 5:16 AM
  • I am sure that error is related to name only. As I understand from the error the request URL in not properly constructed. Could you please try to access the below URL through IExplorer.

    [Your Organization Root URL]/XRMServices/2011/OrganizationData.svc/new_TrainingSet

    You can find the proper URL through Developer Resource.

    Setting>>Customization>>Developer Resource>> click on Organization Data service link. try to find the new_TrainingSet.

    Hope this helps!

    Monday, August 5, 2013 6:47 AM
  • Hi,

    Finally i got success. :-)

    I changed my code to 

    SDK.REST.associateRecords(TrainingId,"new_training","new_training_contactMtoM",ContactId,"Contact",successCallBack,errorHandler);

    Thanks Kalim.


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by DynamicsCRM31 Monday, August 5, 2013 7:30 AM
    Monday, August 5, 2013 7:30 AM