Defaulting parent entity of custom activity - dynamics-crm-2011

This should be a simple thing, but as I am quickly finding out with CRM 2011, simple things almost never are...
Anyway, I have a custom activity type, a "Trip". A Trip represents a single visit by a service technician to a client's site. Trips are always created in connection with a Case (Incident) and never with any other entity type, so the entity exists as a custom activity that does not "Display In Activity Menus". That allows me to drop a subgrid of Trips into the primary Case view, making it the only place these things can be created.
The problem is that when I try to create a new Trip from the Case form (by clicking the subgrid and choosing "Add New Trip" from the ribbon), the "Regarding" field isn't populated with the Case that I was on when I clicked "Add". Any of the "built-in" activity types will default their corresponding field with no problem, so it would seem to me that I can do the same here. The field's supposed to be read-only once this defaulting works, but just so I can set up the reference when creating new Trips, I've made it editable, and of course when you pop up the lookup, you can assign this Trip to anything (which is why I want the field read-only in the first place).
I need the form to default the parent Case when the Trip form loads. It has to be there when it's first created, because if it isn't there on the first save, then some plugin code that sets other default values (namely data relating to other Trips that may or may not exist for the same Case) won't work properly.
I read about relationship field mapping, where you can default the values of various fields based on fields of the parent entity, but the relationship between the Incident and my Trip is not listed as mappable from either side and I can't figure out how to make it mappable. I also know it's possible to set default values using JavaScript, but if there's a less "custom" way to set this particular behavior up I would love to know how.

As an epitaph based on Mike's comment, it does indeed seem that Microsoft doesn't want this done. But, it's still possible to do it. The basic steps are:
Download the Ribbon Workbench solution and import it into CRM.
Create a new solution containing the entity or entities you want to be able to customize.
Create a JavaScript file containing functions that will open the form you want and pass the information you need as a parameter. There are two ways to specify the parameters; you can define custom parameters on the form, or you can use Microsoft's undocumented parameters for the Regarding field:
function OpenNewTripFromCase()
{
var entityId = Xrm.Page.data.entity.getId();
var entityTypeCode= Xrm.Page.context.getQueryStringParameters().etc;
var entityDisplayName = Xrm.Page.getAttribute("title").getValue();
var params = {};
params["pId"] = entityId;
params["pType"] = entityTypeCode;
params["pName"] = entityDisplayName;
Xrm.Utility.openEntityForm("cst_trip", null, params);
}
Import this JavaScript file as a Web Resource, and add it to the Solution so it will be available to Ribbon Workbench.
Open up Ribbon Workbench and open the solution you created with your entity and resources. Click the Solution Elements tab, and find the "Commands" item. Right-click and select "Add New". Now expand the list, find and click on your new command. In the Properties pane, change the name of the command to be more descriptive (it's recommended you only change the third "Command#" term), then click the magnifying glass button to the right of the Actions field.
In the window that pops up, click "Add", then choose "JavaScript Function Action". A new item will be added to the action list. Click it, then in the properties pane to the right, find the library containing your function, then type in the function name. I didn't get any sort of IntelliSense here, so be careful typing in the function name. Add parameters if you need to (you shouldn't need to with the code above), then click OK
Now click the custom activity entity in the Entities list of Ribbon Workbench, then in the dropdown to the upper right of the ribbon layout, select the SubGrid ribbon. Create a button in this Ribbon, give it the icon and text you want, and in the Properties pane, set the Command to the command you just created.
Publish the customized Solution, and you should, if you did everything correctly, have a new button that will do the same thing as the "Add New" button when you've selected a sub-grid, but will additionally populate the Regarding field.

Related

Xpages using 'flags' for different doc. structure on same xpage

My scenario:
The xpage ( document content ) has a complex structure. I have a 1st button which opens this xpage, having in the postNewDocument:
Contr.replaceItemValue("txt_codformularmain","01") // I use this field as a flag.
Now, there is another button which compose the same xpage ... but in this case, I do want the xpage to be different, in other words to have some additional / showed fields.
I could add another code at the postNewDocument, like this:
Contr.replaceItemValue("txt_codformularmain2","01")
Later on, in some views I will filter the listed documents by this 2 fields: some views will be listing the documents where txt_codformularmain=01, other views for txt_codformularmain2=01, and so on.
All the fields are on the same form element. ( I could create easily 2 different xpages, with 2 document contents, but in the form Properties at onOpen and onWebAccess settings, I can add only one xpage )
But, I think it isn't the best approach for this. Because in both cases, postNewDocument takes place, and both fields are having the value 01.
In classic lotus notes programming, I had used only one field ( flag ) and for every different button, inside the button's code I just changed the field value: 01, 02, and so on, before the newDocument had been composed.
Should I use another/ a better approach?
I might go for creating a new form with the fields needed there, and creating a new XPage binded to that form...
dominoDocument.isNewNote() will tell you if it's a new document or not. You can compute visibility based on that.
If it's based on a field value, then again set the visibility based on what the value should be for the panel to be visible - instead of getComponent().getValue() use dominoDocument.getItemValueString() assuming it's a Text field for best practice. In the onblur event of the control that's bound to the field you're checking against, trigger a partial refresh. You will need to amend the default partial refresh settings if there is validation on the page.
Once you're comfortable with that technique, start thinking about using a dataContext to hold whether or not an area should be visible, and referencing that dataContext variable in the rendered property. The benefits of this are not only performance, but youo can give a better variable name to the dataContext to make it clearer when supporting the application what the logic is behind it being visible. For someone supporting the Notes Client app, for example, it's unclear without hunting what state "01" or "02" means. Descriptors like "newDoc", "published", etc have an advantage there.
Another method of optimisation would be to use a Dynamic Content control.
But it sounds like your XPage is going to be complex enough without adding more complexity at this stage.

Change the name when Customizing CRM 2011

I customize the campaign form and i face the some problem
1.How to customize the name "GENERAL".i need the name instead of GENERAL .Where to change?or which property should i change?
2.cant delete Lock symbol fields if we try , its saying "It is allowed only for business units".How to unlock and How to delete this?
As Guido suggests, double you can edit the name of the tab by opening up the form in the form editor, double clicking on the tab, and changing the label.
As far as the locked field, you'll have to mark the field as not required, in the entity customization, publish your changes, then you can delete the field from the form.
The label for any tab can be changed by the form editor, if you want a dynamic value you can set using supported customization as this example:
var firstTab = Xrm.Page.ui.tabs.get(0);
firstTab.setLabel("new tab name");
If a field is locked, it means that is business required (in your case is the name of the campaign that must be filled), you can always hide the field and pre-fill the value using javascript code inside onload, onchange or onsave events.
Perhaps someone else can suggest a better (i.e. supported) solution but until then - why not find out the id of those elements and edit them directly from JS in onload? Let me repeat, though, that such an operation is unsupported.

How should I implement `OnLoad` event handler when creating an activity using a contact?

I've created my own solution with a custom entity of type activity. I'd like to show a message whenever a new instance of it is created using an existing contact but not allow the user to create one, if only attempting to do that without going via contact.
Basically, my aim is that it won't be impossible to just create that activity (the form will be hidden directly at any attempt except for one way only). The user will have to go to contacts (or leads etc.) and in there add and create an instance of the custom activity entity. That way, I can assure that the field "regarding" will be filled out already.
I'm guessing that I need to detect somehow that the opening of the form is a creation attempt. How can I do that?
Also, as it is now, the user can't create a contact-less activity of the custom type because it doesn't appear on the menu with other activities. I must have disabled it somehow but I have no idea how. Anybody who has one?
You could do this a bunch of ways but the easiest would probably be to:
Make the regarding field read only.
Make the regarding field mandatory.
That way if a user opens a create new form they wont be able to set the regarding and because its mandatory they wont be able to save the record. When they open via an existing contact the regarding field will be mapped automatically. That said in this case just making it mandatory my be enough.
(As a side JavaScript can be used to identify the current form state, but I'm not sure how useful that is here).
In terms of where custom activities appear, by default mine show in a number of locations, for example:
CRM > Workplace > Activities > Ribbon > Other Activities > XXX.
CRM > Workplace > Activities > View Selector > XXX.
They don't show under the left hand navigation of the workplace because they are grouped under 'Activities'. I'm pretty sure these are all the default settings.
You can exercise greater control by editing the sitemap, where you can put pretty much anything, anywhere.
In addition to Mr Wood, I'd like to show you some code. It works as supposed to but I'm not sure if it's optimal.
var foo = function () {
var whatIsGoingOn = Xrm.Page.ui.getFormType();
if (whatIsGoingOn === 1)
alert("Let there be an entity!");
else
alert("Not a creation...");
}
The other states' (deletion, update etc.) numeric values are listed here.
Answering the second part of your question:
When you create a custom activity you can choose whether to have it appear in 'normal' Activity menus or not by checking the box at the top right of the entity form. This is a once-only choice as far as I know and can't be changed later.
For your setup, I would suggest NOT checking this box, so it does not appear in the activity menus to avoid users even being tempted to do it that way.
Instead, add an explicit relationship to the activity N:1 to Contact, and another N:1 to Lead. Use this relationship to add your activity to the left navigation of Contact and Lead forms, or add a grid for them (depends on how you want to use this and if you need to filter the view to something other than the default "Associated View").
When a user navigates to this section they will see if any previous activities of this type exist, and be able to add a new one. BUT this means that the child record is a child via this relationship, not using "regarding", so use a script on the form for the activity so that if Contact is filled in, it is copied to Regarding, and if Lead is filled in then that is copied. If neither, then use an alert or other means to warn the use that something is wrong (see comment earlier). If you want Regarding to be read-only but filled in by script, you will need to make sure to use the force the value to be saved:
Xrm.Page.getAttribute("regardingobjectid").setSubmitMode("always");
You must have the lookups for Contact and Lead on the form to be able to use them in your scripts, but you can make them not "visible by default" so they are there but not seen by the user (and taking up no space).

SharePoint 2010 - Customizing the rendering and behavior of a List field

In my SharePoint List, I have an "Employee" column that is a User type field. I would like to add some custom Business Logic to the processing of this field.
Currently, when the user adds a row, I check to see if the user is an Employee or a Manager and then change the behavior on this column accordingly. I do this by statically rendering the field in my custom "ListForm Rendering Template", just before my custom ListFieldIterator. I simply use a standard SharePoint FormField (and FormLabel) control. In the markup of the FormField control, I specify the FieldName (Employee) and an event handler for the Load event. In this Load event, I will check to see if the current user is an Employee or Manager (using two different SharePoint groups). If the user is an Employee I set the value of the field to the current user (this part works perfectly). I also want to change the field so it can't be modified. I thought I might be able to just change the ControlMode on the field (in the code of the OnLoad Event Handler) to Display, but for some reason this has no effect. The field still renders with the full, people picker editor. Am I not changing the fields control mode soon enough? Or is this simply not the correct approach? The other logic I want to put in is if the user is a Manager, I would like to allow that user to select the person from a list (SharePoint group) of Employees. It may be easier to just use the people picker and limit the selectable users to that group. (I think I can do this with the SelectionGroup property.) Although, it would be better if I could just provide a dropdownlist of users, which I could possibly do with a hidden dropdownlist that I would show and event handlers that I could use (handle event selectedindexchanged) to pull the value selected and populate the (now hidden) Employee (user) field. Does this approach make sense? Assuming all that will work, the real difficulty I am having is with changing the ControlMode (rendering) on the field (when the user is an employee) to a label or some kind of read only control, which is how that field renders when viewing the row, which is why I think if I can just trick the control into thinking it is in Display mode then it should work perfectly!
I am still learning SharePoint, but I am very proficient in ASP .Net. This is why I would like to keep my customizations in this Custom Rendering Template, using code behind and leverage my existing skill set as much as properly.
Any thoughts, opinions or advice? Does anyone know why I can't get the column to switch the "Control Mode"?
I do not think that I fully understand your scenario. Some code samples could help.
But anyway it sounds like you want some heavy customizations of the user field. In that case you might want to have a look at creating a custom field with all its advantages and disadvantages. Have a look at MSDN: http://msdn.microsoft.com/en-us/library/gg132914.aspx
Another option might be - in case you do not want to re-use this column in many list definitions - that you can get away with your custom rendering template and create a custom create/edit form where you implement the specific edit behaviour for the field (plain ASP.NET with some SharePoint controls). Here is a nice walk-through on how to grab a custom edit form from SharePoint designer: http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2011/05/12/sharepoint-2010-cookbook-how-to-create-a-customized-list-edit-form-for-development-in-visual-studio-2010.aspx
I hope this helps. Kr., Bernd.

Custom list form using sharepoint object model VS 2008

Is there a way to handle the DropDownSelectedIndexChanged for list item new form.
Let say I have a custom list named Temp having Title, Products (which is a DropDownList) and Color Shade (which is a TextBox).
When I create a list item I want to select a product from DropDownList and on the selected index changed I want to write the
business logic such as fetching the row from Color Shade list and bind the data to TextBox of Color Shade. Now I can say ok so that
a list item will be created. Is it possible using VS 2008 sharepoint object model.
you could add a custom control to your masterpage which resursively traverses your control tree (start from this.Page) to find your dropdown and attaches the event handler (which itself is defined in your custom control). Also set the AutoPostBack property to true on the dropdown once you found it.
In terms of getting a reference do your textbox within your eventhandler, you could also apply the same approach of traversing the control tree.
note, you need to ensure that event handler is attached before eventhandlers are run.
Jaap
I am not sure if I understand what you mean.
An alternative approach, and probably less work, would be to use javascript (I would recommend using the jQuery library) to automatically populate your textbox when the selection of the dropdown changes. Everything would be done clientside.
Jaap
There are some third party components that do this sort of thing as custom column types, but if you're developing from scratch you'll probably find it a lot easier doing a custom form. If you do a fully custom version of newform.aspx, you'll still be using the standard new item menu.
If you try to modify the standard controls you'll run into issues like security errors if the submitted value wasn't in the original list.
If you don't want to write your own saving code, hiding the standard new form with css and modifying the values with javascript from your custom control can work quite well.

Resources