Archive

Posts Tagged ‘Associate’

Auditing User’s Team membership in CRM

January 20, 2014 1 comment

Recently we got a requirement to track Team membership of a User (i.e., Log when a User gets Add/Remove from a Team).

We checked whether the OOB Audit feature would fulfill this requirement.

Audit feature does logs an entry every time we add/remove a User from Team but as separate events.

Add User to a Team – Audit

  • An entry with Event name ‘Associate Entities’ gets created in Audit History
  • On double click on the entry you get below window with details of Team that User added to.
Audit Log-Add User to Team

Audit Log-Add User to Team

Remove User from a Team – Audit

  • An entry with Event name ‘Disassociate Entities’ gets created in Audit History
  • On double click on the entry you get below window with details of Team that User removed from.
Audit Log- Remove User from Team

Audit Log- Remove User from Team

 

More Info

  • Audit feature provide details of User’s Team membership, but not in a detailed manner.
  • Also ‘Associate Entities & Disassociate Entities‘ event’s will get logged in ‘Audit History’ every time you perform Associate\Disassociate operations with your N:N related entities.
  • If the details provided in Audit does not give you more details, You can go for a Plugin to log more details when User gets Add/Remove from a Team.

🙂

Associate Campaign with related items using Jscript – CRM 2011

We can associate 2 entity records having N:N relationship using “AssociateRequest” SDK message. (How to associate)

But you get below error when you try to associate a Campaign record with Campaign related items such as Marketing List, Product, or Salesliterature using “AssociateRequest” SDK message.

Associate is not supported for CampaignItem Platform

To associate a Campaign, we have an exclusive SDK message “AddItemCampaignRequest”.

Below is the Jscript to associate Campaign with related records

function associateCampaignItem (campaignId, associateRecordId, associateRecordSchemaName) {

var requestMain = “”

requestMain += “<s:Envelope xmlns:s=\”http://schemas.xmlsoap.org/soap/envelope/\”>”;

requestMain += ” <s:Body>”;

requestMain += ” <Execute xmlns=\”http://schemas.microsoft.com/xrm/2011/Contracts/Services\” xmlns:i=\”http://www.w3.org/2001/XMLSchema-instance\”>”;

requestMain += ” <request i:type=\”b:AddItemCampaignRequest\” xmlns:a=\”http://schemas.microsoft.com/xrm/2011/Contracts\” xmlns:b=\”http://schemas.microsoft.com/crm/2011/Contracts\”>”;

requestMain += ” <a:Parameters xmlns:c=\”http://schemas.datacontract.org/2004/07/System.Collections.Generic\”>”;

requestMain += ” <a:KeyValuePairOfstringanyType>”;

requestMain += ” <c:key>CampaignId</c:key>”;

requestMain += ” <c:value i:type=\”d:guid\” xmlns:d=\”http://schemas.microsoft.com/2003/10/Serialization/\”>” + campaignId + “</c:value>”;

requestMain += ” </a:KeyValuePairOfstringanyType>”;

requestMain += ” <a:KeyValuePairOfstringanyType>”;

requestMain += ” <c:key>EntityId</c:key>”;

requestMain += ” <c:value i:type=\”d:guid\” xmlns:d=\”http://schemas.microsoft.com/2003/10/Serialization/\”>” + associateRecordId + “</c:value>”;

requestMain += ” </a:KeyValuePairOfstringanyType>”;

requestMain += ” <a:KeyValuePairOfstringanyType>”;

requestMain += ” <c:key>EntityName</c:key>”;

requestMain += ” <c:value i:type=\”d:string\” xmlns:d=\”http://www.w3.org/2001/XMLSchema\”>” + associateRecordSchemaName + “</c:value>”;

requestMain += ” </a:KeyValuePairOfstringanyType>”;

requestMain += ” </a:Parameters>”;

requestMain += ” <a:RequestId i:nil=\”true\” />”;

requestMain += ” <a:RequestName>AddItemCampaign</a:RequestName>”;

requestMain += ” </request>”;

requestMain += ” </Execute>”;

requestMain += ” </s:Body>”;

requestMain += “</s:Envelope>”;

var req = new XMLHttpRequest();

 

var OrgServicePath = “/XRMServices/2011/Organization.svc/web”;

var serverUrl = Xrm.Page.context.getServerUrl() + OrgServicePath;

req.open(“POST”, serverUrl, true)

// Responses will return XML. It isn’t possible to return JSON.

req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);

req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute&#8221;);

var successCallback = null;

var errorCallback = null;

req.onreadystatechange = function () { AddItemCampaignResponse(req); };

req.send(requestMain);

}

function AddItemCampaignResponse(req) {

if (req.readyState == 4) {

if (req.status == 200) {

alert(“Success”);

}

else {

alert(“Error – ” + req.responseXML);

}

}

}

How do I use

  • Lets try to associate ‘Campaign’ with ‘Marketing List’ item using above script
  • Pass the required arguments as specified below

    var campaignId = “”; //Campaign GUID

var marketinglistId = “”; //Marketinglist GUID

var schemaName = “”; // Marketinglist entity Schema Name

associateCampaignItem(campaignId, marketinglistId, schemaName);

Below is the C# code to form the ‘AddItemCampaignRequest’

var request = new AddItemCampaignRequest
{
    CampaignId = campaignId,
    EntityId = marketinglistId,
    EntityName = {Marketinglist entity Schema Name}
};

🙂

Associate/Disassociate plugin messages in CRM

April 17, 2013 15 comments

In CRM, the Associate or Disassociate event happens

  • If you have a N:N relationship between two entities and when you try to associate or disassociate records either from Associated view or Sub grid.
Entity Associate View

Entity Associate View

In Plugins, the Associate & Disassociate messages behave little different than other messages.

  • When you register a plugin on Associate message, you have to leave “Primary and Secondary” entities as ‘none’.
Associate Plugin Step

Associate Plugin Step

  • Since we don’t provide entity names, the registered Plug-in step triggers on all “Associate” operations, so we have to check few conditions to let the “Association” trigger happen only between intended entities.

You can use the below code template for Associate or Disassociate plugins

            EntityReference targetEntity = null;

            string relationshipName = string.Empty;

            EntityReferenceCollection relatedEntities = null;

            EntityReference relatedEntity = null;

            if (context.MessageName == “Associate”) {

                // Get the “Relationship” Key from context

                if (context.InputParameters.Contains(“Relationship”)) {

                    relationshipName = context.InputParameters[“Relationship”].ToString();

                }                                   

                // Check the “Relationship Name” with your intended one

                if (relationshipName != “{YOUR RELATION NAME}”) {

                    return;

                } 

                // Get Entity 1 reference from “Target” Key from context

                if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is EntityReference)  {

                    targetEntity = (EntityReference)context.InputParameters[“Target”];

                }                      

                // Get Entity 2 reference from ” RelatedEntities” Key from context

                if (context.InputParameters.Contains(“RelatedEntities”) && context.InputParameters[“RelatedEntities”] is EntityReferenceCollection) {

                    relatedEntities = context.InputParameters[“RelatedEntities”] as EntityReferenceCollection;

                    relatedEntity = relatedEntities[0];

                }

            }

🙂

Associating entities with n:n relationship using Jscript

March 29, 2012 1 comment

Hi,

Below is the Jscript to associate two entity records, which are having (N:N) relationship.

function AssociateEntities(relationshipSchemaName, entity1Name, entity1Id, entity2Name, entity2Id) {

var xmlSoapBody = “<Execute xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>&#8221;

+ ”  <Request xsi:type=’AssociateEntitiesRequest’>”

+ ”    <Moniker1>”

+ ”      <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity1Name + “</Name>”

+ ”      <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity1Id + “</Id>”

+ ”    </Moniker1>”

+ ”    <Moniker2>”

+ ”      <Name xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity2Name + “</Name>”

+ ”      <Id xmlns=’http://schemas.microsoft.com/crm/2006/CoreTypes’>&#8221; + entity2Id + “</Id>”

+ ”    </Moniker2>”

+ ”    <RelationshipName>” + relationshipSchemaName + “</RelationshipName>”

+ ”  </Request>”

+ “</Execute>”;

ExecuteSOAP(“/MSCRMServices/2007/CrmService.asmx”, xmlSoapBody, “http://schemas.microsoft.com/crm/2007/WebServices/Execute&#8221;, false);

}

 function ExecuteSOAP(serviceUrl, xmlSoapBody, soapActionHeader) {

var xmlReq = “<?xml version=’1.0′ encoding=’utf-8′?>”

+ “<soap:Envelope xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/'&#8221;

+ ”  xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'&#8221;

+ ”  xmlns:xsd=’http://www.w3.org/2001/XMLSchema’>&#8221;

+ GenerateAuthenticationHeader()

+ ”  <soap:Body>”

+ xmlSoapBody

+ ”  </soap:Body>”

+ “</soap:Envelope>”;

var httpObj = new ActiveXObject(“Msxml2.XMLHTTP”);

httpObj.open(“POST”, serviceUrl, false);

httpObj.setRequestHeader(“SOAPAction”, soapActionHeader);

httpObj.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

httpObj.setRequestHeader(“Content-Length”, xmlReq.length);

httpObj.send(xmlReq);

var resultXml = httpObj.responseXML;

var errorCount = resultXml.selectNodes(“//error”).length;

if (errorCount != 0) {

var msg = resultXml.selectSingleNode(“//description”).nodeTypedValue;

alert(“Error while association; ” + msg);

return null;

} else {

alert(“Success!!!”);

return resultXml;

}

}

How do I call these functions :-

  • Let’s assume, you have N:N relationship between entityA & entity with relationship name (“new_entityAentityB”)

    var relationshipName = ” new_entityAentityB “;

var entityA_SchemaName = “new_entitya”;

var entityB_SchemaName = “new_entityb”;

var entiyA_Id = “{}”; //GUID of entityA record

var entiyB_Id = “{}”; //GUID of entityB record

AssociateEntities(relationshipName, entityA_SchemaName, entiyA_Id, entityB_SchemaName, entiyB_Id);

Note :- If you copy above functions, please replace double quote & single quote symbols with keyboard symbols.

Hope it helps 🙂