1

I need to assign an Entitlement to all Cases created from an Opportunity, when a user clicks a custom button.

I've created a custom field on the Account which contains the Entitlement Id Application_Entitlement_ID__c that will be used in order to populate the Case lookup field EntitlementId. But I can't work out how to fetch that value, without adding a formula field on the Opportunity, which I'm keen to avoid.

This post Related Object Fields in custom button (URL Hack) probably contains the solution but I'm new to JavaScript and the use case and code are too complicated for me to adapt, in order to solve my issue.

Here's the code that I've written so far -

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

try{ 
    var recToCreate = new sforce.SObject("Case");
    recToCreate.RecordTypeId = "012180000004Kle";  
    recToCreate.OwnerId = '00G18000000Ntra';   
    recToCreate.AccountId = '{!Opportunity.AccountId}';  
    recToCreate.Subject = '{!Opportunity.Name}';  
    recToCreate.Origin = "Customer Application";
    recToCreate.Opportunity__c = '{!Opportunity.Id}';
    recToCreate.User__c = '{!Opportunity.OwnerId}';

    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);
}
1

As we can see on the other post, you need to query the account object to retrieve the entitlementId and then set it on the case. Please find below the updated code in which we have queried on the account object to get the entitlementId and set it on the case record. Also, you have hardcoded the recordType ID and the owner fields which is not a good practice and will definitely fail when you migrate to production. The code below shows how you should query the recordTyeId using the developer name and then set it on the record. Similary, query for the groupName and get the id to set it as the owner.

{!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");
    // TODO: update the filter with your record type name
    var recordTypeQuery = sforce.connection.query("SELECT name,Id from RecordType where developername = 'yourRecordTypeName'");
    var recordTypeQueryResult = recordTypeQuery.getArray("records");
    recToCreate.RecordTypeId = recordTypeQueryResult[0].Id;
    // select Queue as owner 
    var queueNameQuery = sforce.connection.query("SELECT Queue.Name, QueueId from QueueSobject where Queue.Name = 'WE 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}';
    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);
}

}
  • Thanks for going the extra mile there, I've added var queueNameQuery = sforce.connection.query("SELECT Queue.Name, QueueId from QueueSobject where Queue.Name = 'WE_Customer_Services'"); var queueNameQueryResult = queueNameQuery.getArray("records"); recToCreate.OwnerId = queueNameQueryResult[0].Id; but it's returning Cannot read property 'Id' of undefined? – Alex S Apr 25 '15 at 11:47
  • you havent fetched the "Id" in the soql, i am not sure what is the valid value for the owner is it the "Id" or the QueueId, if it is QueueId then you need to set recToCreate.OwnerId = queueNameQueryResult[0].QueueId; or else you need to fetch Id as well in the SOQL and then recToCreate.OwnerId = queueNameQueryResult[0].Id; should work. Hope this helps. – Rajiv Bhatt Apr 25 '15 at 11:54
  • Thanks again but adding Id to the SOQL returns the same error. Changing queueNameQueryResult[0].Id; to queueNameQueryResult[0].QueueId; makes the error message change to "Cannot read property 'QueueId' of undefined".. – Alex S Apr 25 '15 at 11:59
  • Are you sure the SOQL "SELECT Queue.Name, QueueId from QueueSobject where Queue.Name = 'WE_Customer_Services'" returns at least one record? Can you please execute the soql and verify the output? – Rajiv Bhatt Apr 25 '15 at 12:02
  • 1
    If you dont want the query to look at the label then i guess you should filter the results by queue.DeveloperName instead of Queue.Name – Rajiv Bhatt Apr 25 '15 at 12:28

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.