0

I have a JS button (on Cases) that opens an email window with the PR Contact automatically added (lookup relationship). I also have the Account lookup on the Case record and I want to use this to populate the email with the account owner. I cannot seem to get the syntax correct, however.

Below is the code:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
var caseObj = new sforce.SObject("Case"); 
var thisUrl = window.location.href; 
var listUrl = thisUrl.substring(0, thisUrl.length-12); 
caseObj.id = '{!Case.Id}'; /* Need Id field to update Case */ 
var today = new Date(); 
caseObj.status= "In Progress" 
caseObj.Hotel_Notfied_date__c = today; 
caseObj.Hotel_Notified__c = true; 
/* update method takes an array of Cases; init to 1 element - 'caseObj' */ 
var result = sforce.connection.update([caseObj]); 

if (result[0].success == 'false') { 
alert(result[0].errors.message); 
} 
else { 
location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p26=mediastay@slh.com&p2_lkid= {!Case.PR_ContactId__c}&p24_1kid={!Case.HotelCases.Owner}&template_id=00X20000001WsHn');

The part that I believe I'm getting wrong is:

p24_1kid={!Case.Hotel__c.Owner}

The error that I'm getting is:

Error: Field Case.Hotel__c.Owner does not exist. Check spelling.

Thanks in advance.


Updated Code based on the below help:

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
var caseObj = new sforce.SObject("Case"); 
var thisUrl = window.location.href; 
var listUrl = thisUrl.substring(0, thisUrl.length-12);
var records = ['{!Case.Id}']; 
caseObj.id = '{!Case.Id}'; /* Need Id field to update Case */ 
var today = new Date(); 
var cases = sforce.connection.retrieve("Hotel__r.OwnerId","Case", records); 
caseObj.status= "In Progress" 
caseObj.Hotel_Notfied_date__c = today; 
caseObj.Hotel_Notified__c = true; 
/* update method takes an array of Cases; init to 1 element - 'caseObj' */ 
var result = sforce.connection.update([caseObj]); 

if (result[0].success == 'false') { 
alert(result[0].errors.message); 
} 
else { 
location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p26=mediastay@slh.com&p2_lkid= {!Case.PR_ContactId__c}&p24_1kid=console.log(cases[0].Hotel__r.OwnerId);&template_id=00X20000001WsHn');
}

However, the Additional To text box is not populated with the account owner.

  • 1
    I think that should be OwnerId instead of just Owner and there is custom relationship field which will be written using ( __r ). So, that need to be modified as well to Case.Hostel__r.OwnerId – Devendra Oct 11 '16 at 12:27
  • unfortunately that doesn't work – neo2049 Oct 11 '16 at 12:32
  • As you need to populate Email field then it should be Case.Hostel__r.Owner.Email. What HotelCases indicates here? – Devendra Oct 11 '16 at 12:37
0

You can not directly traverse to related record fields. Only current record level fields are accessible. In order to get the case's associated hotel's owner id you need do something which is done in below code. CODE

   {!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
var caseObj = new sforce.SObject("Case"); 
var thisUrl = window.location.href; 
var listUrl = thisUrl.substring(0, thisUrl.length-12);
var records = ['{!Case.Id}']; 
caseObj.id = '{!Case.Id}'; /* Need Id field to update Case */ 
var today = new Date(); 
var cases = sforce.connection.retrieve("Hotel__r.OwnerId","Case", records); 
caseObj.status= "In Progress" 
caseObj.Hotel_Notfied_date__c = today; 
caseObj.Hotel_Notified__c = true; 
/* update method takes an array of Cases; init to 1 element - 'caseObj' */ 
var result = sforce.connection.update([caseObj]); 

if (result[0].success == 'false') { 
alert(result[0].errors.message); 
} 
else { 
location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p26=mediastay@slh.com&p2_lkid= {!Case.PR_ContactId__c}&p24_1kid='+cases[0].Hotel__r.OwnerId+'&template_id=00X20000001WsHn');
}

Now included your code and corrected it. Please note I am assuming Hotel__c is the api name if it is something else then you need to correct it. If it is Object__c then replace it with Object__r.OwnerId. I hope this solves your problem.

  • I get the following error: records is not defined. – neo2049 Oct 11 '16 at 12:56
  • You might have missed var records = ['{!Case.Id}']; //grabs the case id – Kiran Machhewar Oct 11 '16 at 12:58
  • here is the updated code: {!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} caseObj.id = '{!Case.Id}'; /* Need Id field to update Case */ var today = new Date(); var cases = sforce.connection.retrieve("Hotel__r.OwnerId","Case", records); p24_1kid=console.log(cases[0].Hotel__r.OwnerId);&template_id=00X20000001WsHn'); } – neo2049 Oct 11 '16 at 12:58
  • You have missed to define records variable. Please correct that and post corrected code in question it self. Its hard to read here. – Kiran Machhewar Oct 11 '16 at 12:59
  • Updated the question – neo2049 Oct 11 '16 at 13:07

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.