Dynamics CRM: Creating Activities from Activities using Javascript

A request came in to auto-generate an email based on an appointment record. They wanted to transfer certain information to pre-populate an email. Initially, I thought this would be simple, but it turned out to be a little more complex than anticipated as I got further into the weeds on this project. Thankfully after a few iterations, I was able to accomplish the end goal using Javascript and REST.

My first instinct was to use Xrm.Utility.openEntityForm. I had used this before to open existing records and pass updates as well as create new records while pre-populating various information from my source record. While building this out, I was able to pass over the general fields such as ‘subject’ and ‘description’ easily, and I even managed to pass over the regarding object by using custom query string parameters. There was one considerable wall, however.

One of the unique things about activities is that they allow for multiple records of multiple entity types to be populated in one field through the use of activity parties. One of the things that I had looked over was pretty straightforward in the Microsoft library article for setting field values using parameters passed to a form: “You can’t set the values for partylist or regarding lookups.” Well, I had gotten around the regarding lookup by using a custom query string, but there was no way down this path where I could pass multiple attendees on the appointment to the recipient field on the email. What complicates it, even more, is that the number of partylist members can vary from record to record and we would need to have enough querystringparameter elements to hold all of the values.

My second effort was to use the Xrm.Utility.openEntityForm still, but this time just pass the GUID of the appointment and use a REST retrieveRecord function on the newly created email to pull the information from the appointment. This also landed me at the roadblock of not being able to populate the field because an activity party record was not being created along with the email.

The path that eventually led to the completion was to use the REST createRecord function fired from the appointment after collecting all the necessary components into an object. The key component was creating an array within the email object to create the activity party for the email. Being that the number of members could vary by appointment made this a little tricky but I was able to add all of them to the array as well as a sender value. To differentiate the two don’t forget to set the ParticipationTypeMask.

function createEmail() {
	//Create an object to pass into the REST createRecord
	var email = new Object(); 
	//gather data from basic fields and add them to the object 
	var apptDescrip = Xrm.Page.getAttribute("description").getValue();
	var apptSubject = Xrm.Page.getAttribute("subject").getValue();
	email.Description = apptDescrip;
	email.Subject = apptSubject;
	// gather the regarding record and add it to the object
	var regObj = Xrm.Page.getAttribute("regardingobjectid").getValue();
	var apptRegId = regObj[0].id;
	var apptRegEntityType = regObj[0].entityType;
	email.RegardingObjectId = {
		Id: apptRegId,
		LogicalName: apptRegEntityType
	};
	//gather the owner of the appointment to add as the sender of the email
	var apptOwner = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
	//gather the attendees to add as the recipients
	var pMembers = Xrm.Page.getAttribute("requiredattendees").getValue();
	var memLength = pMembers.length; //Colect the dynamic lenght of the party list to append the sender to the end
	var activityParties = new Array();
	var partyObj = new Object();
	partyObj.PartyId = { Id: apptOwner, LogicalName: "systemuser"};
	partyObj.ParticipationTypeMask = { Value: 1 }; //setting "From" member
	activityParties[memLength] = partyObj;
	for (var i = 0; i < pMembers.length; i++) { //process through party members to add tehm to the array
		if (pMembers[i].entityType != null) { //only adding members that are entity records, filtering out values that are email addresses only
			partyObj = new Object();
			partyObj.PartyId = { 
				Id: pMembers[i].id, 
				LogicalName: pMembers[i].entityType 
			};
			partyObj.ParticipationTypeMask = { Value: 2 }; //setting "To" members
			activityParties[i] = partyObj;
		}
	}
	//add activity party array to email object
	email.email_activity_parties = activityParties;
	
	SDK.REST.createRecord(
							email, //object with values to pass to new record
							"Email", //schema name of entity type being created
							EmailCallBack, //success callback function
							function (error) { alert(error.message); } //error callback
	);
}

 


[avatar user=”sflorance” size=”thumbnail” align=”left” /]SCOTT FLORANCE | Business Software Consultant

Scott Florance is one of the CRM Consultants at KTL, and has proven his value as a member of the team since September 2013. Whether implementing a new CRM organization or adding to existing configurations, Scott has engaged clients with a positive and enthusiastic demeanor to help them meet their organizational needs. With six plus years of experience, Scott is familiar with CRM as both a power user and administrator. Scott received his Bachelor’s Degree in Business Administration from the University of Central Florida. He is a Microsoft Certified Technology Specialist for Dynamics CRM, as well as a Certified Scribe Technician.

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email

Related Posts