1

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"); 

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]); 

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 need to update this code to include an element which assigns the current user to a Case Team Role called Requestor.

I've found this code and tried to combine the two, to create the below. I'm new to JavaScript so (unsurprisingly) it hasn't worked.

{!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 rolename + "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);
}

}

On button click, the following error message is displayed and I'm not sure what's going wrong here?

unexpected token var

  • 2
    If you can't see syntax errors in your JavaScript, paste it into this web site jshint.com. Generally it does a good job of identifying the line number where the problem is (line 6 for 2 reasons in this case). – Keith C Jun 10 '15 at 12:30
1

this might sound silly, you have used a comma instead of a semicolon in the below line

var roleName = "CaseRole",

replace it with

var roleName = "CaseRole";

once u go past this one, you might face error in the next line as well

var qr = sforce.connection.query("SELECT Id, Name FROM CaseTeamRole where rolename + "Requestor");

it should be

var qr = sforce.connection.query("SELECT Id, Name FROM CaseTeamRole where Name = 'Requestor'");
  • Thanks for this, my alert's telling me that Case Id is undefined [from line alert('Case ID:'+ct.parentId);] and then another returns 'Invalid parameter value'.. – Alex S Jun 10 '15 at 12:41
  • So I've added an additional alert below the line var result = sforce.connection.create([recToCreate]); which also tells me that result[0].Id is undefined, is this the wrong way to fetch the Id or could it be something to do with the Case not having been inserted yet? – Alex S Jun 10 '15 at 13:10
  • can u do an alert on error message immediately after the create.. alert("An Error has Occurred. Error:" + result[0].errors.message); – Vamsi Krishna Jun 10 '15 at 13:18
  • 1
    u shd start here and understand how to use the create calls and handle the results : developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/… – Vamsi Krishna Jun 10 '15 at 13:28
  • 1
    In the end it was the upper case i in ct.parentId= result[0].Id; that was causing the issue, I've updated the code in my question to fix that. 5 hours wasted because of a single character! – Alex S Jun 11 '15 at 10:58

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.