3

I have the following JavaScript code which creates a Case from an Opportunity, on button click.

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

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

var roleName = "CaseRole";
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");

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

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

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

    // select Entitlement from Account field
    recToCreate.EntitlementId = relatedAccount.Application_Entitlement_ID__c;

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


    // add business requestor to Case Team Role
    var ct = new sforce.SObject("CaseTeamMember");
    ct.MemberId = "{!$User.Id}";
    ct.parentId = result[0].id;
    ct.TeamRoleID = rec.Id;
    alert('Logged In User: '+ct.MemberId);
    alert('Case ID: '+ct.parentId);
    alert('TeamRoleID: '+ct.TeamRoleID);

    var result = sforce.connection.create([ct]);
    alert('Successfully Added to Case Team');


    // 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 now need to use the Id of the newly created Case to populate the Parent Id field of a Case Team Role which I'm creating later in the code. But the below doesn't pick up the Id of the new record.

var ct = new sforce.SObject("CaseTeamMember");
ct.parentId= result[0].Id;

An alert tells me that -

result[0].Id is undefined

I've spent the afternoon looking for documentation which explains how to fetch the Id and can't find any examples?

11
  • 1
    This example developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/… has the id field in all lower case when referencing it in JavaScript i.e. result[0].id. Try that.
    – Keith C
    Jun 10 '15 at 17:36
  • Yes I was about to say that... Use "id" instead. Jun 10 '15 at 18:05
  • Thanks but it looks like that wasn't the issue in this case, I have the same error if I can the Id to id.
    – Alex S
    Jun 10 '15 at 18:07
  • Can you show the complete code including the part that generates error? Also, don't forget to @mention our names. We get notification due to that. Jun 10 '15 at 18:30
  • @heisenberg I've updated the question with the full code but I can only tag one user, thanks for the heads up.
    – Alex S
    Jun 10 '15 at 18:49
2

When a record is created by AJAX synchronous call, please use "id" to get the record Id. Since javascript is a case sensitive language, we have to be cautious about that. In your case, replace all the "Id" by "id.

2

Why not just use a Flow?

Button URL:

/flow/FLOW_UNIQUE_NAME?var_OpportunityId={!Opportunity.Id}&retURL=/{!Opportunity.Id}

Flow: Now, do a Lookup based on the Opportunity Id to grab those fields. Make the variable in your flow "var_OpportunityId" or it wont sync to the button.

After you find the values u need (account, owner, etc.), do a Record Create on the Case Object with your new Ticket. At the end of this little screen, you have the option to create a new variable for the "Created Case's Id". Do so.

Now, you want to do a Record Create on the Team Members. You can do this with any of the logic you wanted to in your javascript.

Easy as pie :)

4
  • Also, just realized its you Alex, hi! :) Jun 10 '15 at 19:08
  • Hi! You've got a thing for these flows haven't you! So this is probably a stupid question but just to check, if I use a Flow for this, the user would only have to click the button and the Case & Case Team would be created automatically (they wouldn't have to enter any data on a Screen element)?
    – Alex S
    Jun 10 '15 at 19:35
  • Alex, yes you can. You dont need a Screen element on a Flow. If you have a Flow without a Screen element, they click the button and are redirected to the Opportunity. All the magic happens in the backend without any user interaction. Jun 10 '15 at 19:59
  • 1
    That's good to know, I've finally got the code working but I'll definitely use this option next time, debugging JavaScript is a nightmare! Thanks for the suggestion.
    – Alex S
    Jun 10 '15 at 20:02
0
var result = sforce.connection.create([recToCreate]);

if(result[0].getBoolean("success")){
    sforce.debug.log("new account created with id " + result[0].id);
} 
else{ 
    sforce.debug.log("failed to create account " + result[0]);
}
1
  • After making the changes that I've suggested in my edit, I get an error 'TypeError: Cannot read property 'document' of undefined.
    – Alex S
    Jun 10 '15 at 19:29

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.