locked
Duplicate the opportunity record(save as) on button click of MS CRM 2013 RRS feed

  • Question

  • Hi

    I have a requirement in the opportunity form, From the already saved opportunity record there should be a button named as "Duplicate opportunity" on click of it I should be able to get a another new opportunity record created with all old values of previously saved record, so that I will modify few fields if required and save it as the second opportunity. Almost it should work similar to "Save as" option.

    Could any 1 please tell me if their is any way to achieve this?

    Rekha.J


    • Edited by Rekha J Monday, November 10, 2014 11:44 AM
    Monday, November 10, 2014 11:44 AM

All replies

  • Refer this.

    Regards, Saad

    Monday, November 10, 2014 11:59 AM
  • You can use ready made addons like:
    a) https://pinpoint.microsoft.com/Applications/12884945435?id=searchResult
    b) https://pinpoint.microsoft.com/Applications/12884967391?id=searchResult

    If you want to do it yourself, then you can write plugin on opportunity entity
    which creates a duplicate record of opportunity.
    Monday, November 10, 2014 12:01 PM
  • if you want to do this with just JavaScript, please look into this blog.

    http://garethtuckercrm.com/2012/07/31/duplicate-record-button-using-jscript-in-microsoft-crm-2011/

    regards

    Jithesh

    Monday, November 10, 2014 7:19 PM
  • Thank you all for your replies, got to know various ways of achieving the record cloning.

    Thought of achieving with no code and able to complete it using this link.

    Rekha.J


     

    Tuesday, November 11, 2014 4:36 AM
  • Hi,

    Here is sample code of JScript.

       function CopyOpportunity() {
    
                var OpportunityId = Xrm.Page.data.entity.getId();
    			var serverUrl = Xrm.Page.context.getClientUrl();
               
                //var newServerUrl = serverUrl.substring(0, serverUrl.length - 1);
                var cloneRecord = new Object();
    
                var OpportunityResults = ODataCommonFunctions.GetDataUsingODataService("/new_OpportunitySet?$filter=new_OpportunityId eq guid'" + OpportunityId + "'", serverUrl);
                if (OpportunityResults != null && OpportunityResults.results != null && OpportunityResults.results[0] != null) {
                    var Opportunity = OpportunityResults.results[0];
                    if (Opportunity.new_name != null)
                        cloneRecord["new_name"] = "Copy of " + Opportunity.new_name;
                    if (Opportunity.new_newOpportunityadded != null)
                        cloneRecord["new_newOpportunityadded"] = Opportunity.new_newOpportunityadded;
                    if (Opportunity.new_DistributeManually != null)
                        cloneRecord["new_DistributeManually"] = Opportunity.new_DistributeManually;
                    if (Opportunity.new_DistributionMethod != null)
                        cloneRecord["new_DistributionMethod"] = Opportunity.new_DistributionMethod;
                    if (Opportunity.new_type != null && Opportunity.new_type.Value != null) {
                        var resultVal = { Value: parseInt(Opportunity.new_type.Value) };
                        cloneRecord["new_type"] = resultVal;
                    }
                  
    
                    if (Opportunity.new_NextDistributionDate != null) {
                        var dt = Opportunity.new_NextDistributionDate;
                        dt = dt.replace("/Date(", "");
                        dt = dt.replace(")/", "");
                        var dateValue = new Date(parseInt(dt, 10));
                        dateValue.setDate(dateValue.getDate());
                        cloneRecord["new_NextDistributionDate"] = dateValue;
                    }
                    if (Opportunity.new_LastSentOn != null) {
                        var dt = Opportunity.new_LastSentOn;
                        dt = dt.replace("/Date(", "");
                        dt = dt.replace(")/", "");
                        var dateValue = new Date(parseInt(dt, 10));
                        dateValue.setDate(dateValue.getDate());
                        cloneRecord["new_LastSentOn"] = dateValue;
                    }
    
    				cloneRecord["new_originOpportunityid"] = OpportunityId;
    				createOpportunity(cloneRecord, serverUrl)
    					
    
                }
            }
    
    		
            function createOpportunity(entityObject, serverUrl) {
                var jsonEntity = this.parent.JSON.stringify(entityObject);
               var odataSetName = "/new_OpportunitySet";
                var ODATA_ENDPOINT = "XRMServices/2011/OrganizationData.svc";
    
                $.ajax({ type: "POST",
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                   url: serverUrl+"/" + ODATA_ENDPOINT + odataSetName,
                    data: jsonEntity,
                    beforeSend: function (XMLHttpRequest) {
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                    },
                    success: function (data, textStatus, XmlHttpRequest) {
    				
                        var createdRecord = data["d"];
    			
    		var recordUrl = Xrm.Page.context.getClientUrl() + "/main.aspx?";
    
        var params = "etn=" + "new_Opportunity";
        params += "&pagetype=entityrecord";
    
        params +="&id="+ encodeURIComponent("{" + createdRecord.new_OpportunityId +"}");
    
        var URL = recordUrl + params;
    Xrm.Page.ui.close();
    	 window.open(URL, "_blank", "width=900px,height=600px,resizable=1"); 
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
    
                    }
                });
            }
    Thanks,

    Sreeni Pavalla

    Tuesday, November 11, 2014 5:26 AM