0

I have the below code for a JavaScript button which works fine

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var entitlementQuery = "SELECT  Application_Entitlement_ID__c FROM Account WHERE Id = '{!Opportunity.AccountId}' limit 1";
result = sforce.connection.query(entitlementQuery); 
records = result.getArray("records"); 

var qr = sforce.connection.query("SELECT Id, Name FROM CaseTeamRole WHERE Name = 'Requestor'");
var crRecords = qr.getArray("records");
var rec = crRecords[0];


if(records[0]){ 
  var relatedAccount = records[0]; 
  try{ 
    var recToCreate = new sforce.SObject("Case");

    var recordTypeQuery = sforce.connection.query("SELECT Name,Id from RecordType WHERE developername = 'WE_Application'");
    var recordTypeQueryResult = recordTypeQuery.getArray("records");
    recToCreate.RecordTypeId = recordTypeQueryResult[0].Id;

    var queueNameQuery = sforce.connection.query("SELECT Queue.DeveloperName, QueueId from QueueSobject WHERE Queue.DeveloperName = 'Wex_Europe_Applications'");
    var queueNameQueryResult = queueNameQuery.getArray("records");
    recToCreate.OwnerId = queueNameQueryResult[0].QueueId;

    >>adding BusinessHours code here

    recToCreate.AccountId = '{!Opportunity.AccountId}';  
    recToCreate.Subject = '{!Opportunity.Name}';  
    recToCreate.Origin = "Customer Application";
    recToCreate.Opportunity__c = '{!Opportunity.Id}';
    recToCreate.User__c = '{!Opportunity.OwnerId}';

    recToCreate.EntitlementId = relatedAccount.Application_Entitlement_ID__c;


    var result = sforce.connection.create([recToCreate]);


    var ct = new sforce.SObject("CaseTeamMember");
    ct.MemberId = "{!$User.Id}";
    ct.parentId = result[0].id;
    ct.TeamRoleID = rec.Id;

    var result = sforce.connection.create([ct]);


    // check that Case has been created
    if(result[0].success == "true"){
      location.reload();
    }
    else{
      alert("An Error has Occurred. Error: " + result[0].errors.message);
    }
  }
  catch(e){
    alert("An Error has Occurred. Error: " + e);
  }


}

I'm now trying to set the Business Hours for the Case, that's being created through the button and I've written

var businessHoursQuery = sforce.connection.query("SELECT Id FROM BusinessHours WHERE Name = 'Wex Europe CS Working Hours'");
var bhQueryResult = businessHoursQuery.getArray("records");
alert("bhQueryResult contains " + bhQueryResult); 
recToCreate.BusinessHours = bhQueryResult;

My alert confirms that bhQueryResult contains the 18 character BusinessHours Id but I then hit an error

Invalid Parameter Value

and I'm not sure why?

I do have read & edit access on the BusinessHours field, for the record's that're being created by the button.

  • try to use 15 digit id and check if it solve your problem. – Tushar Sharma Dec 3 '15 at 17:05
  • @TusharSharma could you please let me know how to convert the 18 character Id to 15 in JavaScript? – Alex S Dec 3 '15 at 18:36
  • use this var newId =bhQueryResult.substring(0,15); – Tushar Sharma Dec 4 '15 at 10:00
  • 1
    @TusharSharma I found the solution to the issue and tested it using your suggestion & without. The 18 character Id was accepted in the end but thank you for the suggestion. – Alex S Dec 4 '15 at 10:49
1

I think businessHoursQuery.getArray("records") returns array.

To retrieve the first result from the array, try -

var bhQueryResult = businessHoursQuery.getArray("records");
recToCreate.BusinessHoursId = bhQueryResult[0].Id;

Note that BusinessHours needs to be changed to BusinessHoursId, as shown in the Field Reference Guide.

For debugging I would suggest using console.log('===bhQueryResult===',bhQueryResult);

and check in the browser console, to see what the actual result is. Use F12 to open the console.

  • Thanks, the log displays the 18 character Id & the type BusinessHours in the console. The console also shows to errors 'Refused to set unsafe header "User-Agent" ' and 'Uncaught Error: Backbone.history has already been started' does that help? I also tried using BusinessHoursId but this didn't solve the issue. – Alex S Dec 3 '15 at 18:33
  • @AlexS are you sure bhQueryResult contains only id not array. as per my knowledge getArray("records") always return array. – Ratan Paul Dec 4 '15 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.