• Mathieu DAVOUST
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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.

Since Workflow Email Alerts don't allow you to specify a sender, I'm looking for a workaround to achieve that end.

 

I'm thinking of adding a criteria (Last Modified By = User.ID) that only a specific user can trigger the workflow but that's full of problems, especially with regards to time-based and the queue: subsequent changes invalidate the rule and delete the queued actions.

 

Any brilliant suggestions out there? I'm feeling a little dull-witted today...

 

Thanks,

Luke C