function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mathieu DAVOUSTMathieu DAVOUST 

How to duplicate OpportunityLineItems into OrderItems

Hi,

I need to create a button that create an Order from Opportunities. The order created this way will need to be linked to :
  • The opportunity's account
  • The opportunity
  • A predefinied Pricebook
I suceed to create a button that create the order but i can't link it nor the opportunity nor the pricebook.

I'm not using the Salesforce button because I plan to add the OpportunityLineItem into the order (When the order will be correctly link at the opportunity). 

Here is my code :
{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')}

getDate = function(dateObj){
    var day = dateObj.getDay() < 9 ? '0' + dateObj.getDay():dateObj.getDay();
    var month = dateObj.getMonth() < 9 ? '0' + dateObj.getMonth():dateObj.getMonth();

    return dateObj.getFullYear()+'-'+month+'-'+day;
    }
    
success = function(result) {
    if (result[0].getBoolean("success")) {
        alert("new record created with id " + result[0].id);
    }
    else {
        alert("failed to create record" + result[0]);
    }
}

failed = function (error) {
    alert("An error has occurred " + error);
 }

var Commande = new sforce.SObject('Order');

var CommandesAssociees = sforce.connection.query("select Id from Opportunity where Opportunit__c = '{!Opportunity.Id}'"); 
NbCommandesAssociees = CommandesAssociees.getArray("records"); 

var CommandeX = NbCommandesAssociees.length + 1;

Commande.Name = 'Intervention '+ CommandeX + ' - {!Opportunity.Name}';
Commande.AccountId = '{!Opportunity.AccountId}';
Commande.CurrencyIsoCode = '{!Opportunity.CurrencyIsoCode}';
Commande.EffectiveDate = getDate(new Date());
Commande.Status = 'TestMDAV';
Commande.OpportunityId = '{!Opportunity.Id}';
Commande.RecordTypeId = '01211000000DALj';
result = sforce.connection.create([Commande],
   {onSuccess : success, onFailure : failed});;


location.reload() 

And there is the error i got: 
INVALID_FIELD_FOR_INSERT_UPDATE


It's strange because when I make an "insert" with Dataloader (with the same information), it works perfectly. 
If I remove the lines that concerne OpportunityId and Pricebook2Id, my button works.

Help needed, every ideas is welcome.