[SalesForce] Fetch Values From Related Object Fields – JavaScript

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

Best Answer

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

}
Related Topic