AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 31.07.2012, 16:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
Gareth Tucker: Duplicate Record Button using Jscript in Microsoft CRM 2011
Источник: http://gtcrm.wordpress.com/2012/07/3...soft-crm-2011/
==============

As detailed here, Rollup Pack 8 for Microsoft CRM 2011 gave us a couple of shiny new jscript toys:

Xrm.Utility.openEntityForm(name,id,parameters)

and

Xrm.Utility.openWebResource(webResourceName,webResourceData,width, height)

In this post I will show you how to add a “Duplicate Record” button to a CRM form, using the new openEntityForm function.

First, lets add the button.  Download Erik Pool’s ribbon editor from CodePlex.  If you are still editing ribbon xml manually and not using this tool then you are mad.   Run the tool, connect to your CRM organisation and open the ribbon you wish to edit.  I am going to add my button to the Case form, adding to the last Group of buttons on the right hand side.  I click in that Button Group and then click New Button and name my button:



My button appears small by default so I change the Template Alias to Large.  I need an icon for my button and the one I see on the “Copy Selected” button looks good so I can click on that button and copy and paste the image properties from that button over to my new button:



I tidy the Label and Tooltip and then click on the Action tab and enter the name of my jscript library and function, which we will create next:



Finally, I save the change and then check my button appears in CRM:



I want to demonstrate copying a range of data types so I add a few custom fields to my Case form:



Ok, now we can write the jscript function.  There are 3 steps to this:

1. Collect field values from the source record

2. Define the values you want to populate into the destination record

3. Pop the form, passing across the values to be populated

I’ve pasted just the first part below.  Most of this is just me using CRM’s getValue() functon, but you will see I do some null value checks on the lookup fields (to avoid errors) and I also need to extract multiple values from each lookup field.  You will also note for the date field I call another function so that the value returned by the getValue() call gets converted into the MM/DD/YYYY format that the next step requires:

function DuplicateCase() { //get values from the Form var CaseId = Xrm.Page.data.entity.getId(); var CaseTitle = Xrm.Page.data.entity.attributes.get("title").getValue(); if (Xrm.Page.data.entity.attributes.get("customerid").getValue() != null) { var CustomerId = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].id; var CustomerName = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name; var CustomerType = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].entityType; } if (Xrm.Page.data.entity.attributes.get("subjectid").getValue() != null) { var SubjectId = Xrm.Page.data.entity.attributes.get("subjectid").getValue()[0].id; var SubjectName = Xrm.Page.data.entity.attributes.get("subjectid").getValue()[0].name; } var CaseOriginCode = Xrm.Page.data.entity.attributes.get("caseorigincode").getValue(); var CaseTypeCode = Xrm.Page.data.entity.attributes.get("casetypecode").getValue(); var CaseDate = FormatDate("new_dateofincident"); // wants "MM/DD/YYYY" (this might be environment specific though) var CaseUrgent = Xrm.Page.data.entity.attributes.get("new_urgent").getValue(); var CaseClaimAmount = Xrm.Page.data.entity.attributes.get("new_claimamount").getValue(); if (Xrm.Page.data.entity.attributes.get("transactioncurrencyid").getValue() != null) { var CurrencyId = Xrm.Page.data.entity.attributes.get("transactioncurrencyid").getValue()[0].id; var CurrencyName = Xrm.Page.data.entity.attributes.get("transactioncurrencyid").getValue()[0].name; } if (Xrm.Page.data.entity.attributes.get("ownerid").getValue() != null) { var OwnerId = Xrm.Page.data.entity.attributes.get("ownerid").getValue()[0].id; var OwnerName = Xrm.Page.data.entity.attributes.get("ownerid").getValue()[0].name; var OwnerType = Xrm.Page.data.entity.attributes.get("ownerid").getValue()[0].entityType; } var CaseDescription = Xrm.Page.data.entity.attributes.get("description").getValue();
Here’s the second section of the function, here I am basically placing each of the values I extracted during step 1 into a parameter object (testing for and excluding null values as I go):

 

//define default values for new Incident record var parameters = {}; if (CaseTitle != null) { parameters["title"] = CaseTitle + " - COPY"; } if (CustomerId != null && CustomerName != null) { parameters["customerid"] = CustomerId; parameters["customeridname"] = CustomerName; parameters["customeridtype"] = CustomerType; } if (SubjectId != null && SubjectName != null) { parameters["subjectid"] = SubjectId; parameters["subjectidname"] = SubjectName; } if (CaseOriginCode != null) { parameters["caseorigincode"] = CaseOriginCode; } if (CaseTypeCode != null) { parameters["casetypecode"] = CaseTypeCode; } if (CaseDate != null) { parameters["new_dateofincident"] = CaseDate; } if (CaseUrgent != null) { parameters["new_urgent"] = CaseUrgent; } if (CaseClaimAmount != null) { parameters["new_claimamount"] = CaseClaimAmount; } if (CurrencyId != null && CurrencyName != null) { parameters["transactioncurrencyid"] = CurrencyId; parameters["transactioncurrencyidname"] = CurrencyName; } if (OwnerId != null && OwnerName != null) { parameters["ownerid"] = OwnerId; parameters["owneridname"] = OwnerName; parameters["owneridtype"] = OwnerType; } if (CaseDescription != null) { parameters["description"] = CaseDescription; } if (CaseId != null && CaseTitle != null) { parameters["new_parentcase"] = CaseId; parameters["new_parentcasename"] = CaseTitle; }
And the last bit is simply:

//pop incident form with default values Xrm.Utility.openEntityForm("incident", null, parameters); }
Here’s how it looks in action.  Here’s my source record:



And here’s what pops up when I click the button:



To use this yourself in your unique scenarios you will obviously need to edit the getValue() and parameters lines to match your fields.  I’ve covered off the main data types and added a little bit of robustness to help guide you on this.   The openEntityForm utility certainly helps out here.

Here’s the jscript function in full and the missing FormatDate function:

function DuplicateCase() { //get values from the Form var CaseId = Xrm.Page.data.entity.getId(); var CaseTitle = Xrm.Page.data.entity.attributes.get("title").getValue(); if (Xrm.Page.data.entity.attributes.get("customerid").getValue() != null) { var CustomerId = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].id; var CustomerName = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name; var CustomerType = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].entityType; } if (Xrm.Page.data.entity.attributes.get("subjectid").getValue() != null) { var SubjectId = Xrm.Page.data.entity.attributes.get("subjectid").getValue()[0].id; var SubjectName = Xrm.Page.data.entity.attributes.get("subjectid").getValue()[0].name; } var CaseOriginCode = Xrm.Page.data.entity.attributes.get("caseorigincode").getValue(); var CaseTypeCode = Xrm.Page.data.entity.attributes.get("casetypecode").getValue(); var CaseDate = FormatDate("new_dateofincident"); // wants "MM/DD/YYYY" (this might be environment specific though) var CaseUrgent = Xrm.Page.data.entity.attributes.get("new_urgent").getValue(); var CaseClaimAmount = Xrm.Page.data.entity.attributes.get("new_claimamount").getValue(); if (Xrm.Page.data.entity.attributes.get("transactioncurrencyid").getValue() != null) { var CurrencyId = Xrm.Page.data.entity.attributes.get("transactioncurrencyid").getValue()[0].id; var CurrencyName = Xrm.Page.data.entity.attributes.get("transactioncurrencyid").getValue()[0].name; } if (Xrm.Page.data.entity.attributes.get("ownerid").getValue() != null) { var OwnerId = Xrm.Page.data.entity.attributes.get("ownerid").getValue()[0].id; var OwnerName = Xrm.Page.data.entity.attributes.get("ownerid").getValue()[0].name; var OwnerType = Xrm.Page.data.entity.attributes.get("ownerid").getValue()[0].entityType; } var CaseDescription = Xrm.Page.data.entity.attributes.get("description").getValue(); //define default values for new Incident record var parameters = {}; if (CaseTitle != null) { parameters["title"] = CaseTitle + " - COPY"; } if (CustomerId != null && CustomerName != null) { parameters["customerid"] = CustomerId; parameters["customeridname"] = CustomerName; parameters["customeridtype"] = CustomerType; } if (SubjectId != null && SubjectName != null) { parameters["subjectid"] = SubjectId; parameters["subjectidname"] = SubjectName; } if (CaseOriginCode != null) { parameters["caseorigincode"] = CaseOriginCode; } if (CaseTypeCode != null) { parameters["casetypecode"] = CaseTypeCode; } if (CaseDate != null) { parameters["new_dateofincident"] = CaseDate; } if (CaseUrgent != null) { parameters["new_urgent"] = CaseUrgent; } if (CaseClaimAmount != null) { parameters["new_claimamount"] = CaseClaimAmount; } if (CurrencyId != null && CurrencyName != null) { parameters["transactioncurrencyid"] = CurrencyId; parameters["transactioncurrencyidname"] = CurrencyName; } if (OwnerId != null && OwnerName != null) { parameters["ownerid"] = OwnerId; parameters["owneridname"] = OwnerName; parameters["owneridtype"] = OwnerType; } if (CaseDescription != null) { parameters["description"] = CaseDescription; } if (CaseId != null && CaseTitle != null) { parameters["new_parentcase"] = CaseId; parameters["new_parentcasename"] = CaseTitle; } //pop incident form with default values Xrm.Utility.openEntityForm("incident", null, parameters); } // This function takes the fieldname of a date field as input and returns the value of that field in MM/DD/YYYY format // Note: the day, month and year variables are numbers function FormatDate(fieldname) { var d = Xrm.Page.data.entity.attributes.get(fieldname).getValue(); if (d != null) { var curr_date = d.getDate(); var curr_month = d.getMonth(); curr_month++; // getMonth() considers Jan month 0, need to add 1 var curr_year = d.getFullYear(); return curr_month + "/" + curr_date + "/" + curr_year; } else return null; }



Источник: http://gtcrm.wordpress.com/2012/07/3...soft-crm-2011/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
CRM DE LA CREME! CRM 4.0 Disaster Recovery Blog bot Dynamics CRM: Blogs 2 26.02.2016 08:23
Gareth Tucker: MyCRM add-ons for Microsoft CRM 2011 Blog bot Dynamics CRM: Blogs 0 06.07.2012 18:11
Gareth Tucker: Click Dimensions Email Marketing for Microsoft CRM 2011 Blog bot Dynamics CRM: Blogs 0 28.04.2012 11:11
crminthefield: Microsoft Dynamics CRM 2011 White Papers & Technical Documentation Blog bot Dynamics CRM: Blogs 0 29.12.2011 01:12
Microsoft Dynamics CRM Team Blog: Microsoft Dynamics CRM 2011 ~ Online Test Drive Guide Blog bot Dynamics CRM: Blogs 0 05.08.2011 20:13
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 19:28.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.