0

I have a custom java script button on Account object. Clicking the button should create Case (Child of Account) and Issue__c (Child of Case object) records.

Case record is created successfully, but Issue record is not created. Attached the error message screenshot.

Below is the javascript code:

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
var newcase= new sforce.SObject("Case");

newcase.Status = "New";
newcase.Origin= "Phone";
var result = sforce.connection.create([newcase]);
alert('A case has been created with number:'+result);
var newissue = new sforce.SObject("Issue__c");
newissue.Case__c = newcase.Id;
newissue.Name = "Name 1"
var result1 = sforce.connection.create([newissue]);
alert('A Issue has been created with number:'+result1);
window.location.reload();

Error screenshot: enter image description here

1

You can access the newly created Case id using the result variable.

In fact, the create method returns an array of type SaveResult as you can see here.

This type contains an id variable and a success variable as boolean.

Here's the right code:

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
var newcase= new sforce.SObject("Case");

newcase.Status = "New";
newcase.Origin= "Phone";
var result = sforce.connection.create([newcase]);
if(result[0].success == 'true'){
   var newissue = new sforce.SObject("Issue__c");
   newissue.Case__c = result[0].id;
   newissue.Name = "Name 1";
   var result1 = sforce.connection.create([newissue]);
   if(result1[0].success == 'true'){
      window.location.reload();
   }else{
      alert('An error occurred during the issue creation');
   }
}else{
   alert('An error occured during the case creation');
}
  • Thanks for your help with this martin, though i updated the above code am getting the same error. Please advice. – venky Mar 2 '17 at 10:14
  • What do you have exactly as error message? – Martin Lezer Mar 2 '17 at 10:19
  • getting the 1st alert that case has been created. in the second alert box - errors:{fields:'Case__c', message:'Required fields are missing: [Case__c]', statusCode:'REQUIRED_FIELD_MISSING',},id:null,success:'false',} – venky Mar 2 '17 at 10:24
  • Can you add an alert after this line: var result = sforce.connection.create([newcase]);. Please add: alert(result[0].id); and watch if the id is filled. – Martin Lezer Mar 2 '17 at 10:27
  • Yes, this has filled. but this is not getting assigned in this statement.. newissue.Case__c = records[0].Id; – venky Mar 2 '17 at 10:33

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.