function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdcChi2sfdcChi2 

SOQL Query in On screen Javascript button

I use the below javascript button to auto populate recipients from a custom object called RvMembers (similar to the contact object) on the related list of the RVAccount (similar to the account object) when sending documents for signature using docusign. 
Problem is it works if there are more than one member in the related list but it does not populate if there is only one member in the related list. I believe it has to do with the query but i'm new to javascript so need help asap.


{!REQUIRESCRIPT("/apex/dsfs__DocuSign_JavaScript")}

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

//********* Option Declarations (Do not modify )*********//
var RC = '';
var RSL='';
var RSRO='';
var RROS='';
var CCRM='';
var CCTM='';
var CCNM='';
var CRCL='';
var CRL='';
var DST='';
var LA='';
var CEM='';
var CES='';
var STB='';
var SSB='';
var SES='';
var SEM='';
var SRS='';
var SCS ='';
var RES='';
var OCO='';
var debug = false;

//*************************************************//
/* Encode all variables from data with left brace bang URLENCODE( bang JSENCODE(salesforceobject.salesforcefield))}  */



function foundRVAccountAnyMembers()
{

      var RVAccountAnyMembers = 'Not Found';
     
      try
      {
       var result = sforce.connection.query("Select id, (Select Id, Name, rvpe__Email__c, rvpe__ExtMemberId__c, rvpe__FirstName__c, rvpe__LastName__c, rvpe__RVAccount__c From rvpe__RVMembers__r  ) From rvpe__RVAccount__c r where id = '{!rvpe__RVAccount__c.Id}' LiMIT 1");
      
       var records = result.getArray("records");
      
       if (records.length != 0)
       {
      
      RVAccountAnyMembers = 'Records Found';
       }
    }
    catch(error)
    {
     if (debug)
     {
      alert('Exception Name: '+error.name+ 'Exception Message:' + error.message);
     }
    }
    finally
    {
     if (debug)
     {
      alert('Finally - foundRVAccountAnyMembers: RVAccountAnyMembers = '+RVAccountAnyMembers);
     }
    }
              
               return RVAccountAnyMembers;
}


function getMembersInfoasCRL()
{

    
       try
       {
       var result = sforce.connection.query("Select id, (Select Id, Name, rvpe__Email__c, rvpe__ExtMemberId__c, rvpe__FirstName__c, rvpe__LastName__c, rvpe__RVAccount__c From rvpe__RVMembers__r  ) From rvpe__RVAccount__c r where id = '{!rvpe__RVAccount__c.Id}' LiMIT 99");
      
       var records = result.getArray("records");
                                   for(recs1 = 0; recs1 <100; recs1++ )
                                   {
       var RVMembers = records[recs1].get("rvpe__RVMembers__r");
              var contact = RVMembers.get("records");

       for(recs = 0; recs <100; recs++ )
                                     
       {
    
      var contactEmail = contact[recs].rvpe__Email__c;
      var contactFirstName = contact[recs].rvpe__FirstName__c;
      var contactLastName = contact[recs].rvpe__LastName__c;

      if(contactEmail==null)
      {
       alert('Member is missing email, correct and then send again');
       return;
      }
      if(contactFirstName==null)
      {
       alert('Member is missing FirstName, correct and then send again');
       return;
      }
      if(contactLastName==null)
      {
       alert('Member is missing LastName, correct and then send again');
       return;
      }
    
      CRL=CRL+'Email~'+contactEmail+';FirstName~'+contactFirstName+';LastName~'+contactLastName+';,';
     
       }
                                  }
    }
    catch(error)
    {
     if (debug)
     {
      alert('Exception Name: '+error.name+ 'Exception Message:' + error.message);
     }
    }
    finally
    {
     if (debug)
     {
      alert('Finally - getMembersInfoasCRL: CRL = '+CRL);

     }
    }
              
               return ;
}

function getDataForDocuSign()
{

var RVAccountAnyMembers = foundRVAccountAnyMembers();

if(foundRVAccountAnyMembers=='Not Found')
{
  return;
}
else
{
  getMembersInfoasCRL();
 
  if(CRL=='')
  {
   return;
  }
}



var newLocation ="/apex/dsfs__DocuSign_CreateEnvelope?DSEID=0&SourceID={!rvpe__RVAccount__c.Id}&RC="+RC+"&RSL="+RSL+"&RSRO="+RSRO+"&RROS="+RROS+"&CCRM="+CCRM+"&CCTM="+CCTM+"&CRCL="+CRCL+"&CRL="+CRL+"&OCO="+OCO+"&DST="+DST+"&CCNM="+CCNM+"&LA="+LA+"&CEM="+CEM+"&CES="+CES+"&SRS="+SRS+"&STB="+STB+"&SSB="+SSB+"&SES="+SES+"&SEM="+SEM+"&SRS="+SRS+"&SCS="+SCS+"&RES="+RES;
//********* Page Callout (Do not modify) *********//
if (debug) {
  alert(newLocation);
} else {
  window.location.href = newLocation;
}
}

//*******************************************//

getDataForDocuSign();
KevinPKevinP
Without more data there's no way to tel but you can test your hypothesis by running the queries in the workbench here: https://workbench.developerforce.com/login.php
mandycmandyc
Hello -

Did you get this resolved? I have a similar use case and would be interested to learn what ended up working for you.

Thank you,
Mandy