Skip to main content
Edited code to fix bug
Source Link
Alex S
  • 2.3k
  • 7
  • 40
  • 68
{!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;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);
}

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

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

}
Source Link
Alex S
  • 2.3k
  • 7
  • 40
  • 68

Create Case & assign Case Team Role JavaScript Button

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