locked
N-N Relationship RRS feed

  • Question

  • Hi,

    i have two entities with a N-N relationship. is it possible to create the relationship between 2 record of those entity using the form script?

    Regards

    Nicola

    Monday, April 19, 2010 6:18 PM

Answers

  • try replacing

    authenticationHeader+

    with

    GenerateAuthenticationHeader() +

    and see if this helps..

    Tuesday, April 20, 2010 8:00 AM

All replies

  • If you use CRM Web Service Toolkit, it could be something like this:

    associate = function (relationshipName, entity1Name, entity1Id, entity2Name, entity2Id) {
      var request = [
        "<Request xsi:type='AssociateEntitiesRequest'>",
          "<Moniker1>",
           "<Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>", entity1Id, "</Id>",
           "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>", entity1Name, "</Name>",
          "</Moniker1>",
          "<Moniker2>",
           "<Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>", entity2Id, "</Id>",
           "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>", entity2Name, "</Name>",
          "</Moniker2>",
          "<RelationshipName>", relationshipName, "</RelationshipName>",
        "</Request>"
      ].join("");
    
      var response = CrmServiceToolkit.Execute(request);
    };
    

    I haven't actually tested the code, hopefully it will work at first shot. 

    I hope the parameters make sense to you without more explanation. Let me know if you run into any problem.

    Cheers,


    Daniel Cai | http://danielcai.blogspot.com
    Monday, April 19, 2010 6:48 PM
  • Forgot to include RelationshipName in the execute message body previously, just added. 
    Daniel Cai | http://danielcai.blogspot.com
    Monday, April 19, 2010 7:03 PM
  • Hi Nicola,

    Yes, see following example code for the same..

    just replace FirstEntityId,FirstEntityName,SecondEntityId,SecondEntityname,RELTIONSHIPNAME  as per your requirement in the following script..

    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>" + 
    " <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
    "  <Request xsi:type=\"AssociateEntitiesRequest\">" + 
    "  <Moniker1>" + 
    "   <Id xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">" + FirstEntityId + "</Id>" + 
    "   <Name xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">FirstEntityName</Name>" + 
    "  </Moniker1>" + 
    "  <Moniker2>" + 
    "   <Id xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">" + SecondEntityId + "</Id>" + 
    "   <Name xmlns=\"http://schemas.microsoft.com/crm/2006/CoreTypes\">secondEntityName</Name>" + 
    "  </Moniker2>" + 
    "  <RelationshipName>RELTIONSHIPNAME</RelationshipName>" + 
    "  </Request>" + 
    " </Execute>" + 
    " </soap:Body>" + 
    "</soap:Envelope>" + 
    "";
    
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Execute");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    
    var resultXml = xmlHttpRequest.responseXML;
    //alert(resultXml.xml);
    
    
    Monday, April 19, 2010 7:25 PM
  • hi,

    i'm trying to use the class 'AssociateEntitiesRequest'

    this is my code:

                         // Prepare the SOAP message.
                         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'>"+
                         authenticationHeader+
                         "<soap:Body>"+
                         "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
                         "<Request xsi:type='AssociateEntitiesRequest'>"+
                         "<Moniker1>"+
                         "<Id>"+crmForm.ObjectId+"</Id>"+
                         "<Name>incident</Name>"+
                         "</Moniker1>"+
                         "<Moniker2>"+
                         "<Id>"+RelatedTrackingID+"</Id>"+
                         "<Name>ico_externalcases</Name>"+
                         "</Moniker2>"+
                         "<RelationshipName>new_ico_externalcases_incident</RelationshipName>"+
                         "</Request>"+
                         "</Execute>"+
                         "</soap:Body>"+
                         "</soap:Envelope>";
                         // Prepare the xmlHttpObject and send the request.
                         var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
                         xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
                         xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Execute");
                         xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
                         xHReq.setRequestHeader("Content-Length", xml.length);
                         xHReq.send(xml);
                         // Capture the result
                         var resultXml = xHReq.responseXML;

    the error is "An Unexpected error occurred". is there any problem in my code? (is a script running in the event "on save" of the form)

    Thanks

    Monday, April 19, 2010 9:07 PM
  • Can you turn on CRM trace log, and use CRM Trace Log Viewer to locate the detail message reported by CRM platform? 

    If you are trying to associate the record when you are working on a new record, you may not be able to do so, as crmForm.ObjectId will give you a null

    Cheers,


    Daniel Cai | http://danielcai.blogspot.com
    Tuesday, April 20, 2010 2:43 AM
  • try replacing

    authenticationHeader+

    with

    GenerateAuthenticationHeader() +

    and see if this helps..

    Tuesday, April 20, 2010 8:00 AM