Thursday, August 5, 2010

CRM 4.0 : To Show the 1:N entity in to an IFRAME

Hi All,
Here is the code to show the other entity 1:N (CRM left nav) in to an IFRAME.


crmForm.all.IFRAME_ReasonsforRejection.src = GetFrameSource("new_new_tripmanagement_new_tripmanagementreje");


function GetFrameSource(tabSet)
{
if (crmForm.ObjectId != null)
{
var oId = crmForm.ObjectId;
var oType = crmForm.ObjectTypeCode;
var security = crmFormSubmit.crmFormSubmitSecurity.value;
return "areas.aspx?oId=" + oId + "&oType=" + oType + "&security=" + security + "&tabSet=" + tabSet;
}
else
{
return "about:blank";
}
}

//Note:

/// To identify the GetFrameSource('XXXXXXXXXXXXXX');

// XX = relationship name(open the entity and identify the relationship which you created)

To Show the N:N Relationship entity in to an IFRAME.


crmForm.all.IFRAME_NatureofBusiness.src = GetFrameSourceNN(new_new_natureofbusiness_account');

//IFRAME_NatureofBusiness = Name of the IFrame.

function GetFrameSourceNN(tabSet)
{
if (crmForm.ObjectId != null)
{
var oId = crmFormSubmit.crmFormSubmitId.value;
var oType =crmFormSubmit.crmFormSubmitObjectType.value;
var security = crmFormSubmit.crmFormSubmitSecurity.value;
return "areas.aspx?oId=" + oId + "&oType=" + oType + "&security=" +
security + "&roleOrd=2" + "&tabSet=area" + tabSet
}
else
{
return "about:blank";
}
}

//Note:

/// To identify the GetFrameSourceNN('XXXXXXXXXXXXXX');

// XX = relationship name(open the entity and identify the relationship which you created).


Cheers....!

CRM 4.0 : Retrieve Values using FetchXML.

Hi All,
Last week i have implemented to retrieve the values from an entity using fetchxml. hope this will be useful for our other development.


function populateTopicSubTopic(premise)
{
var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message.
var xml = ""+
"
authenticationHeader+ ""+
"
<fetch mapping='logical'>"+
"<entity name='new_subtopic'>"+
"<attribute name='new_name'/>"+
"<attribute name='new_subtopicid'/>"+
"<attribute name='new_subtopicsid'/>"+
"<attribute name='new_subtopicsidname'/>"+
"<filter type='and'>"+
"<condition attribute='new_premisetype' operator='eq' value='"+premise+"'/>"+
"</filter>"+
"</entity>"+
"</fetch>
"+
"
"+
"
"+
"
";
// 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/Fetch");
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;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
// Process and display the results.
else
{

// Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;
resultSet.replace('<','<'); resultSet.replace('>','>');

// Create an XML document that you can parse.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
// Load the XML document that has the UnEncoded results.
oXmlDoc.loadXML(resultSet);
// Display the results.
var results = oXmlDoc.getElementsByTagName('result');
if (results.length>0)
{
var tlookupItem=new Object();
tlookupItem.id=results.item(0).selectSingleNode("new_subtopicid").text;
tlookupItem.typename='new_subtopic';
tlookupItem.name=results.item(0).selectSingleNode("new_name").text;
crmForm.all.new_subtopicid.DataValue=new Array(tlookupItem);

var stlookupItem=new Object();
stlookupItem.id=results.item(0).selectSingleNode("new_subtopicsid").text;
stlookupItem.typename='new_topic';

var topicname='';
if (results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[0].name=='name')
{
topicname=results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[0].text;
}
else if(results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[1].name=='name')
{
topicname=results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[1].text;
}
//stlookupItem.name=results.item(0).getElementsByTagName('new_subtopicsid').item(0).attributes[0].text;
stlookupItem.name=topicname;

crmForm.all.new_topicid.DataValue=new Array(stlookupItem);

return true;
}
else return false;
}
return false;
}


Cheers..!

Friday, June 25, 2010

CRM 4.0 SDK- Insert record into many-to-many relationship

Hi All,

Here is the code to insert record in to many-to-many relationship using CRM SDK.

public void CreateRelationshipRecord(ref CrmService service, Guid esurveyid, Guid incidentid)
{
try
{
Moniker moniker1 = new Moniker();
moniker1.Name = "new_e_survey";
moniker1.Id = esurveyid;
Moniker moniker2 = new Moniker();
moniker2.Name = "incident";
moniker2.Id = incidentid;
AssociateEntitiesRequest request = new AssociateEntitiesRequest();
request.Moniker1 = moniker1;
request.Moniker2 = moniker2;
request.RelationshipName = "new_new_e_survey_incident";
service.Execute(request);
}
catch (System.Web.Services.Protocols.SoapException soapex)
{
Response.Write(soapex.Detail.InnerText);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}


Cheers.....

Monday, June 7, 2010

CRM 4.0 -Send Email using CRM SDK

I got asked for some sample code for sending an email from CRM 4.0 by using the SDK, so thought I would post the code.


1. Create an Email template. so that we can modify the email content at anytime.







2. Here is the code to retrieve the email template and send email using CRM SDK.I have created a queue based on our requirment.so all email should go using this queue Email.

public static void sendEmail(ref CrmService service, Guid entityId, Guid customerid, string customertype, string createdate)

{

try

{

Guid mailID = Guid.Empty;

SendEmailRequest req = new SendEmailRequest();




InstantiateTemplateRequest instTemplate = new InstantiateTemplateRequest();

instTemplate.TemplateId = new Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"); //guid of email template

instTemplate.ObjectId = entityId;

instTemplate.ObjectType = EntityName.incident.ToString();



InstantiateTemplateResponse instTemplateResponse = (InstantiateTemplateResponse)service.Execute(instTemplate);

email emailresponse = (email)instTemplateResponse.BusinessEntityCollection.BusinessEntities[0];



Guid queueId = GetQueueGuid(ref service, "Customer Service");



ArrayList alProperty = new ArrayList();



DynamicEntity newEmail = new DynamicEntity();

newEmail.Name = "email";



DynamicEntityArrayProperty deapFrom = new DynamicEntityArrayProperty();

deapFrom.Value = ConvertIntoSingleActivityParty(queueId, EntityName.queue.ToString());

deapFrom.Name = "from";

newEmail.Properties.Add(deapFrom);



DynamicEntityArrayProperty deapTo = new DynamicEntityArrayProperty();

deapTo.Name = "to";

deapTo.Value = ConvertIntoSingleActivityParty(customerid, customertype);

newEmail.Properties.Add(deapTo);



StringProperty strPropDescription = new StringProperty();

strPropDescription.Value = emailresponse.subject;

strPropDescription.Name = "subject";

alProperty.Add(strPropDescription);



StringProperty strPropMessageBody = new StringProperty();

strPropMessageBody.Value = emailresponse.description;

strPropMessageBody.Name = "description";

alProperty.Add(strPropMessageBody);



CrmBooleanProperty ack = new CrmBooleanProperty();

ack.Value = new CrmBoolean(true);

ack.Name = "mda_autoresponse";

alProperty.Add(ack);



Lookup caseid = new Lookup();

caseid.Value = entityId;

caseid.type = EntityName.incident.ToString();



LookupProperty lookupPropRegardingObject = new LookupProperty();

lookupPropRegardingObject.Value = caseid;

lookupPropRegardingObject.Name = "regardingobjectid";

alProperty.Add(lookupPropRegardingObject);



Property[] prop = new Property[alProperty.Count];

alProperty.CopyTo(prop);



newEmail.Properties.AddRange(prop);



TargetCreateDynamic myTarget = new TargetCreateDynamic();

myTarget.Entity = newEmail;

CreateRequest create = new CreateRequest();

create.Target = myTarget;

CreateResponse myResponse = (CreateResponse)service.Execute(create);



SendEmailRequest emailreq = new SendEmailRequest();

emailreq.EmailId = myResponse.id;

emailreq.TrackingToken = "";

emailreq.IssueSend = true;

// Send the e-mail message.

SendEmailResponse res = (SendEmailResponse)service.Execute(emailreq);

}




private static DynamicEntity[] ConvertIntoSingleActivityParty(Guid senderId, string entityname)

{

DynamicEntity[] part = new DynamicEntity[1];

try

{

part[0] = new DynamicEntity();

part[0].Name = "activityparty";



LookupProperty lProp = new LookupProperty();

lProp.Name = "partyid";

Lookup l = new Lookup();

l.type = entityname;

l.Value = senderId;



lProp.Value = l;



Property[] prop = new Property[1];

prop[0] = lProp;



part[0].Properties.AddRange(prop);

return part;

}

catch (System.Web.Services.Protocols.SoapException soapex)

{

throw soapex;

}

catch (Exception ex)

{ throw ex; }

}



Hope this will help you guys to send email from email Template.



Happy Coding.!!!!!!!1

CRM 4.0 Assign CRM record to another user using CRM-SDK

Last week i have assigned a task to assign the record to another user in CRM. using CRM SDK ,we can assign the record to another user.


Program as follows




protected void AssignToUser(ref ICrmService crmService, String targetEntity, String NewOwnerGuid)

{

SecurityPrincipal assignee = new SecurityPrincipal();

assignee.Type = SecurityPrincipalType.User;



assignee.PrincipalId = new Guid(NewOwnerGuid);

TargetOwnedIncident target = new TargetOwnedIncident();



target.EntityId = new Guid(targetEntity);



AssignRequest assign = new AssignRequest();

assign.Assignee = assignee;

assign.Target = target;



AssignResponse assignResponse = (AssignResponse)crmService.Execute(assign);

}