3

Scenario: I am working on a button which should create a new Sprint__c child record with Record Type "Sprint Deployment" which will be automatically linked (via lookup) to the Sprint__c record that I pressed the button from, which is a standard "Sprint" Record Type.

The Kicker: I want to auto-generate the name of the new child record to contain the child record number, based on how many child records already exist for that parent. (Example: If there are already 5 child records on the parent record, I want to name the new record "Sprint Deployment 6" (5 being the current, +1 for the new).


I am attempting this by:

  1. SOQL Querying the RecordType "Sprint Deployment" for the Sprint__c Object (limit 1), and then getting the Id of item [0] in the array. I am doing this via SOQL Query so that the button will work between Orgs without modification. (This works as expected and returns the RecordTypeId into the 'recordType' variable.

  2. SOQL Querying the Sprint__c object WHERE RecordTypeId = recordType variable and Related_To__c (lookup) = sprintId variable.


I am receiving the following error when pressing the button:

missing ) after argument list


From my Stack searches, I am aware that this is generally a syntax error commonly caused by not escaping " or ' characters, but I have tried to escape them in various different ways and keep coming up with the same error. Can someone please help me with the correct syntax for this query?


Query where Error Occurs:

var deploymentQuery = sforce.connection.query("SELECT Id FROM Sprint__c WHERE RecordTypeId = '"+recordType"' AND Related_To__c = '"+sprintId"'");

Full Code Snippet as it exists currently (lots of alerts for debug) and incomplete code since this error is blocking me:

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;
var newSprint = new sforce.SObject("Sprint__c");
var recordTypeQuery = sforce.connection.query("SELECT Id FROM RecordType WHERE Name = 'Sprint Deployment' AND SObjectType = 'Sprint__c' limit 1"); 
var recordTypeQueryResult = recordTypeQuery.getArray("records");
var recordType = recordTypeQueryResult[0].Id;
var sprintId = "{!Sprint__c.Id}";

var deploymentQuery = sforce.connection.query("SELECT Id FROM Sprint__c WHERE RecordTypeId = '"+recordType"' AND Related_To__c = '" +sprintId"'");
     alert(deploymentQuery);
var deploymentQueryResult = deploymentQuery.getArray("records");
     alert(deploymentQueryResult);
var deploymentQueryLength = deploymentQueryResult.length;
     alert(deploymentQueryLength);
3

You need to add extra '+' character:

var deploymentQuery = sforce.connection.query("SELECT Id FROM Sprint__c WHERE RecordTypeId = '"+recordType"' AND Related_To__c = '" +sprintId+ "'");
  • 1
    Well, don't I feel stupid. I've been spending so much time in APEX I forgot that to syntax Javascript in the middle of a string you need a + in the front of the var and then another + to continue on to the rest of the string. Thank you for such a quick assist! – Morgan Marchese Mar 21 '16 at 15:52
  • @MorganMarch I am pretty sure that in apex those things should be done as well. For myself, I am always trying to do things like this in separate variable as well as multiline. That saves me more time and easier to read from up to down. Good luck! – kurunve Mar 21 '16 at 15:56
  • Yeah I usually multi-line these things as well, this was one of those 'quick-and-dirty' kind of buttons that I will go through and clean up the syntax for later. I guess it's just been a while since I added a variable to the middle of a string. Always forgetting the little things when working with the big picture ;) I'll be selecting your answer as best as soon as it lets me. Thanks again! – Morgan Marchese Mar 21 '16 at 16:00

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.