How to change a lead id dynamically after cloning

2019-09-13 03:17发布

问题:

In my Microsoft CRM I need to create a clone button that copies a lead as is so my users can modify few data in it then save it. I succeeded in adding the button to the ribbon and cloning my lead as set by the code below:

Webresource:

<RibbonDiffXml>
<CustomActions>
<CustomAction Id="My.MSCRM.incident.form.Clone.Button.CustomAction"         Location="Mscrm.Form.incident.MainTab.Collaborate.Controls._children" Sequence="0">
  <CommandUIDefinition>
    <Button Command="MSCRM.incident.form.Clone.Command" Id="MSCRM.incident.form.Clone.Button" Image32by32="$webresource:My_Clone32" Image16by16="$webresource:My_Clone16" LabelText="$LocLabels:MSCRM.incident.form.Clone.Button.LabelText" Sequence="0" TemplateAlias="o1" ToolTipTitle="$LocLabels:MSCRM.incident.form.Clone.Button.ToolTipTitle" ToolTipDescription="$LocLabels:MSCRM.incident.form.Clone.Button.ToolTipDescription" />
  </CommandUIDefinition>
</CustomAction>
</CustomActions>
<Templates>
 <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
</Templates>
<CommandDefinitions>
<CommandDefinition Id="MSCRM.incident.form.Clone.Command">
  <EnableRules/>
  <DisplayRules>
    <DisplayRule Id="MSCRM.incident.form.Clone.DisplayRule" />
  </DisplayRules>
  <Actions>
    <JavaScriptFunction FunctionName="cloneCase" Library="$webresource:My_CustomRibbonJavascript"      />
  </Actions>
</CommandDefinition>
</CommandDefinitions>
<RuleDefinitions>
<TabDisplayRules />
<DisplayRules>
  <DisplayRule Id="MSCRM.incident.form.Clone.DisplayRule">
    <FormStateRule State="Create" InvertResult="true" />
  </DisplayRule>
</DisplayRules>
<EnableRules/>
</RuleDefinitions>
<LocLabels>
<LocLabel Id="MSCRM.incident.form.Clone.Button.LabelText">
  <Titles>
    <Title description="Clone Case" languagecode="1033" />
  </Titles>
</LocLabel>
<LocLabel Id="MSCRM.incident.form.Clone.Button.ToolTipDescription">
  <Titles>
    <Title description="Clone Case" languagecode="1033" />
  </Titles>
</LocLabel>
<LocLabel Id="MSCRM.incident.form.Clone.Button.ToolTipTitle">
  <Titles>
    <Title description="Clone Case" languagecode="1033" />
  </Titles>
</LocLabel>

Javascript:

function GetContext() {
var _context = null;
if (typeof GetGlobalContext != "undefined")
    _context = GetGlobalContext();
else if (typeof Xrm != "undefined")
    _context = Xrm.Page.context;
return _context}

function cloneCase() {

if (Xrm.Page.data.entity.getId() == null) {
    alert('First save the record before Clone Case')

}
else {
    var CRMContext = GetContext();
    var serverUrl = CRMContext.getServerUrl();
    var caseid = Xrm.Page.data.entity.getId();
    caseid = caseid.replace('{', '').replace('}', '');

    //Below URL is for CRM online  
    var url = serverUrl + 'main.aspx?etc=112&extraqs=%3f_CreateFromId%3d%257b' + caseid + '%257d%26_CreateFromType%3d112%26etc%3d112%26pagemode%3diframe&pagetype=entityrecord';

    Window.open(url, 900, 600, 'toolbar=no,menubar=no,resizable=yes');
}
}

The problem is that when I save my cloned lead, since what I am copying is the URL with the id of the original lead, when I save it, it is not saving as a new lead, but saving instead of the original one since it has the same id. Any idea on how to modify my JavaScript code in a way that keeps the URL cloned since it is the only way to get exactly the same info copied as they are in the original one but save as a new lead not as the original in Microsoft CRM. Thank you!

回答1:

If you take a look at this page, you will find an example at the bottom of the page of how you can open a new form and send data to the new form which you can use to pre populate fields.