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

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).

Related

update netsuite parent field via suitescript in view mode

I have scripts that react off of, for example, a client Recalc client event. For example, on my form I have a subtab that users may add or remove items from. Based on actions on this subtab (housing a child record of the parent) I would like a field on the parent to update (say to show a total from the children records).
As I was saying, these events seem to work fine if in edit mode but they do not work correctly in view mode. (even in view mode these child records have a "Delete" option at the end of each row in the subtab. This was provided by netsuite by default.
I wondered if anyone had any tips to best allow this parent field to update real time while in updating the subtab rows with the form in view mode.
Thanks.
You can make a custom field on the parent (header) whose value is determined by saved search. For instance, make a saved search that totals the line values by transaction. Be sure to make it filter by transaction in the Available Filters tab. Make the search public so everyone can use it.
Create the custom field that sources the total from the saved search. Make sure to uncheck the "Store Value" checkbox, as you don't want to store the data, you want to reference the search results. You do this on the Validation and Defaulting tab. You'll see a field for Saved Search there. Choose the search you created above.
As you remove/add/change lines on the transaction, the field updates accordingly. In essence, you don't need a single line of code to make this work - it's all in how you create the search and the custom field that references it.
I have a similar situation posted here.
The NetSuite team answered me by email, and it happens you can't really achieve this on the view mode: some API methods are not available. Their suggestion to my case (and I think it applies to yours too) was really to force a refresh on the whole page.
Of course, you can always achieve this accessing the DOM elements directly, but this isn't a best practice, as your code can stop working if these elements change on a version update.
I had the same problem, I'm not able to restrict on view or remove edit button. But, there was one alternative solution with workflows, you can deploy workflow on child record edit mode restrictions, then if the user clicks edit on view then the record will not be available to edit. This concern will apply to custom record as well.

Defaulting parent entity of custom activity

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.

How to Enabled a single field for update for a particular role in CRM 2011

What I need
I have a custom Entity with that with multiple fields. Admin Role has "god" access. All other roles except for one have read only. The one non admin role with update access, should only be able to update a single field.
What I believe to be true
I believe I have three main options to implement this requirement:
Enable Update Access to the role for that entity then write Javascript to disable all fields on the form for that role, except for the one that I want that role to be able to edit
Enable Update Access to the role for that entity then create a new form that disables all fields on the form for that role, except for the one that I want that role to be able to edit.
Enable Update Access to the role for that entity then turn on field security for each field, disabling access using the field security, for each field except for the one I want them to edit.
What's the Best Practice?
What options should I choose?
If I go with options 1 or 2, will the user be able to edit the field on the bulk edit form?
From a user perspective, I think it's confusing when a form opens up with things enabled, then they get locked down. Plus someone could possibly get data in there before the fields get locked. I'd say you'd have to combine this with a plugin to prevent changing fields you don't want changed.
I like this option better, although again, the field can be unlocked if someone knows what they're doing, so a plugin to double check would be nice.
This would avoid having to double check in a plugin, but you also have to rely on the admin correctly setting up security for new fields going forward. If that's not a concern, this might be best.
Bulk edit is a global privilege, so they'd have bulk edit for all entities. Also, the bulk edit form does not load scripts, so that knocks out option 1. I'd say if it's just this one field, I might leave the privilege locked down and provide my own Bulk edit button on the grid that would show a custom page that just has that one field on it, then handle the updates though script.
2 is most likely best, or as an alternative put the fields in the header or footer rather than as read-only fields on the form.
This also means the fields won't be available to bulk edit, but other methods such as data import or workflows would let users get round this if they know how and have rights to do such things.
3 Field Security is the most robust and works for all scenarios
Possible option 4: create another entity to contain those fields and apply different security to that entity. If created as a child, show the record in a grid on the form with the values included in the view. If it is a parent then you could use methods such as showing the values via an HTML webresource page included on the form.

With SharePoint/InfoPath: Is it possible to switch default views, and then save this setting with a button?

So lets say that I have two views, one that is the default and another that can be triggered with a button. Is it possible to switch views, then have the view that you switched to become the default view? So that if the form were opened again it would still be on the view you switched to?
If not, is there a way to have a part of a form read-only to a certain group in SharePoint and editable to another group?
Or even better, could I have an email sent out to with different views to different people?
Thanks!
No, it's not possible to set the default view with a button/code. You'd want use a different approach (something similar to the State Machine Pattern)
Create a "State" field, that represents the actual state of the form (usually the same as the views). So when the button is pressed, it sets the State-field to "View2" and switch the view to View2. In the form load rules (Data - Form Load) you create a new Rule that changes the view based on the value State-field.
Yes, setting different section of the form to read-only for specific groups is also possible, however it requires custom code. For each section create a new field (like "Section1Enabled"). Then create a new conditional formatting rule that disables Section1 if Section1Enabled is false. In your form's load event, you add code that that decides whether the current user is in the specific group or not and based on that, you set the value of Section1Enabled.
You can do with SharePoint's UserGroups.asmx or with the SharePoint Server Object Model (google should help you out with that).

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.

Resources