8

I am trying to runa query in a .js button to pull out an ID of a contact that is associated with a record so I can auto send an EchoSign document.

However I can not get this working, my googling skills have been made defective by this!

Basically I have a button that runs a URL and sends a document to EchoSign to be sent out, but it needs to be sent to the most recent contact that was sent an EchoSign document.

Example below:

var contactCheck = sforce.connection.query("SELECT echosign_dev1__Recipient__c
FROM echosign_dev1__SIGN_Agreement__c
WHERE echosign_dev1__Account__c = '{!Account.Id}'
AND echosign_dev1__Status__c = 'Signed'");

I'm still very new to .js so I am not 100% sure if I am doing things correctly, but I have seen the following 2 ways of pulling out just the ID and neither work:

var contactId = templateId.records[0].Id;
var contactId = contactCheck.records[i].get("Id"));

Can anyone help? This is stressing me out... I am going to turn prematurely grey at the age of 22

10

Javascript can be very hard especially when u want to switch gears from APEX a totally free flow language that never cares about character case to a totally drama queen style javascript where case sensitive is a sin.

Refer here for synchronous ajax examples :

https://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_more_samples.htm

Try this :)

var contactCheck = sforce.connection.query("SELECT echosign_dev1__Recipient__c
FROM echosign_dev1__SIGN_Agreement__c
WHERE echosign_dev1__Account__c = '{!Account.Id}'
AND echosign_dev1__Status__c = 'Signed'");

records = contactCheck.getArray("records");

  for (var i=0; i< records.length; i++) {
    var record = records[i];
    alert("Contact name"+ record.Name + "Contact Id -- " + record.Id);
  }
4

First, hopefully you have only put line breaks in your "SELECT echosign ... Signed'" string to make it clearer here; that all needs to be on a single line.

If it is the echosign_dev1__Recipient__c value you want this should work:

var contactId = contactCheck.records[0].echosign_dev1__Recipient__c;

or if it is the Id of the echosign_dev1__Account__c object you want then this:

var contactId = contactCheck.records[0].Id;

The sforce.connection.query request returns an object with a "records" field that is an array of the result SObjects and you can access the fields of the result SObjects using the dot notation. (JavaScript will accept several syntaxes; this is just the most compact.) Hopefully you are confident that you will always get a row back; if not you should check the length or the returned array using contactCheck.records.length.

You should get used to using the Developer Console of your browser. That will report syntax errors and has full debugging capabilities.

As rao comments, the simplest way to debug is to add console.log('something to output in the console') and alert('something to popup in an alert dialog') into your JavaScript code to check how far you've got and examine data.

(Is it worth learning about JavaScript? I would say emphatically yes. There is great community and innovation. For example at https://www.npmjs.org/ there are 67,000 packages available.)

  • Adding my 2 cents on to this, since we are dealing with javascript alert, log are great commands that you can rely on which would help you understand the flow of the program better. This is how I got my beak wet as a beginner when I started with jS :) – Rao Apr 3 '14 at 17:52
  • Yes I was just adding line breaks for the ease of reading... I hate it when people add in unnecessary line breaks – Timothy Apr 3 '14 at 18:51
  • I keep on getting an error when I use the: – Timothy Apr 3 '14 at 19:50
  • var contactId = contactCheck.records[0].Id; it is saying "Cannot read property 'Id' of undefined" do you know what that means? – Timothy Apr 3 '14 at 19:51
  • Yes it means the expression to the left of ".Id" is undefined. First step would be to add console.log or alerts of contactCheck, contactCheck.records, contactCheck. contactCheck.records.length etc to see where something first goes wrong. Note that long threads of debugging comments is not what this site is about... – Keith C Apr 3 '14 at 20:11

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.