+ Start a Discussion
TheLearnerTheLearner 

How to pass id in the java script-- very urgent

HI Experts,

I have writing code for the button, in that i need to fetch the records for some conditons for that , i wrote two inner queries now i need to pass " TWM_PMP_Letter_Information__r " object id in the window.open to open the page, but im not able to understand how to pass it, could anyone help me please. here TWM JOB is parent object and the button is there in TWM JOB, TWM PMP LETTER INFORMATION IS CHILD there is lookup relation, in my page im simply using  " 
<apex:page renderAs="pdf" standardController="TWM_PMP_Letter_Information__c"  there is no controller.  I wrot code but its not working at all.please help me out guys. thanks in advance


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

if(
'{!$Profile.Name}' == 'System Administrator' ||
'{!$Profile.Name}' == 'TWM Community User' ||
'{!$Profile.Name}' == 'TWM Community User2' ||
'{!$Profile.Name}' == 'PMP Planner' ||
'{!$Profile.Name}' == 'PMP Manager' ||
'{!$Profile.Name}' == 'PMP Administrator' ||
'{!$Profile.Name}' == 'TW PMP Report Manager' ||
'{!$Profile.Name}' == 'TWM Sandbox' ){

var jobId = '{!TWM_Job__c.Id}';
var flag = false;
var relRecords = sforce.connection.query("Select Id,(select Id,Letter_Type__c,Letter_NoAcc4_Actual__c from TWM_PMP_Letter_Information__r where Letter_Type__c = 'A1' and Letter_NoAcc4_Actual__c <> null limit 1 ),(select id, task_status__c from twm_tasks__r where recordtype.name = 'Survey' and Task_Status__c = 'Letter Sent' limit 1) from TWM_Job__c WHERE Id = '"+jobId+"'");
alert('total reocrds.'+ relRecords );
var records = relRecords.getArray("records");
var recrdlength = records.length;
var recs = relRecords.TWM_PMP_Letter_Information__r.getArray("records");
alert('total Twm pmp letter infor.'+ recs);
var pmpId = recs[0].TWM_Job__c.TWM_PMP_Letter_Information__r.Id;
alert('twmpmp id.'+ pmpId );

var InstallRecords = sforce.connection.query(" Select Id,Job__c,recordtype.name from TWM_Tasks__c Where Job__c = '"+jobId+"' and recordtype.name = 'Install'");

var insRec = InstallRecords.getArray("records");
var qryLength = insRec.length;

if(records.length >0){
for(var i=0; i<records.length; i++){
var record = records[i];
if(record.TWM_Tasks__r != null && record.TWM_Tasks__r.size > 0 && qryLength === 0){
flag = true;
}
}
if(flag === true){
window.open("/apex/TWM_Job_NoAcc4_letterPdf?id="+pmpId+"","_blank");

}
else{
window.alert('Sorry! Task criteria do not meet');
}
}
else{
window.alert('Sorry! Property and Requested Customer Number criteria do not meet');
}
}
else{
window.alert('User do not have the required permissions');
}

 
Abhishek BansalAbhishek Bansal
Hi,

Please change the below line :
window.open("/apex/TWM_Job_NoAcc4_letterPdf?id="+pmpId+"","_blank");
TO : window.open("/apex/TWM_Job_NoAcc4_letterPdf?id="+pmpId,"_blank");

Let me know if you have any other issue.

Thanks,
Abhishek
TheLearnerTheLearner
Hi Abhishek,

i made amendments as you said, but TWM_PMP_Letter_Information__c getting null records, and getting cannot read property getarray of undefined.


User-added image
User-added image
andrson mathewandrson mathew
code you type into the script block is wrapped in a function executed on window.onload:


<script type='text/javascript'>//<![CDATA[ window.onload = function () { function myFunc(id){ alert(id); } } //]]> </script>
your function myFunc is not in the global scope so is not available to your html buttons.

<script type='text/javascript'>//<![CDATA[ function myFunc(id){ alert(id); } //]]> </script>
 
TheLearnerTheLearner
Hi andrson,

I didnt got exactly , this is my vf page could you tell me please where i need to amend.

<apex:page renderAs="pdf" standardController="TWM_PMP_Letter_Information__c" applyBodyTag="false" showHeader="false" sidebar="false" standardStylesheets="false" applyHtmlTag="false">
    <Head>
        <style type="text/css" media="print">
            @page{                  
                margin-left: 1cm;
                margin-right: 1cm;                 
                margin-top: 0.5cm;
                margin-bottom: 0.75cm;
                             
                          
            }
            @page:first
            {
            
              @top-center{
                    content: element(header);
                }
                
              @bottom-center{
                    content: element(footer);
                }
            }
            
            div.footer {
                display: block;
                padding: 2px;
                position: running(footer);
            }
            
           .pagenumber:before {
                content: counter(page);
            }
            
           .pagecount:before {
                content: counter(pages);
            }   
                     
            .ParagraphText{
               font-family: Arial,Sans-Serif;
               font-size: 10pt;
            }
            
        </style>
    </Head>
    <div class = "Header">
        <div style="float: right;"><apex:image url="{!$Resource.ThamesWaterLogo}"/></div>
    </div>
    <div class="content">
           
             <apex:outputPanel style="display:block">
             <br/><br/><br/><br/><br/><br/>
                     <table style="table-layout:fixed">
                        <tr>
                              <td>
                                <table style="table-layout:fixed; font-family: Arial,Sans-Serif; font-size: 11pt;">
                                  <tr><td colspan="1">&nbsp;{!TWM_PMP_Letter_Information__c.Name__c}</td></tr>
                                  <tr><td colspan="1">&nbsp;{!TWM_PMP_Letter_Information__c.Line1_of_Mailing_Address__c}</td></tr>
                                  <tr><td colspan="1">&nbsp;{!TWM_PMP_Letter_Information__c.Line2_of_Mailing_Address__c}</td></tr>
                                  <tr><td colspan="1">&nbsp;{!TWM_PMP_Letter_Information__c.Line3_of_Mailing_Address__c}</td></tr>
                                  <tr><td colspan="1">&nbsp;{!TWM_PMP_Letter_Information__c.Town_of_Mailing_Address__c}</td></tr>
                                  <tr><td colspan="1">&nbsp;{!TWM_PMP_Letter_Information__c.Postcode_of_Mailing_Address__c}</td></tr>  
                                 </table>
                             </td>
                         <td>
                           <table style="table-layout:fixed">
                            <tr>
                                <td align="right"><apex:image url="{!$Resource.ThamesWaterSystem}"/></td>
                                <td><apex:outputLabel style="color: #195E8D;  font-family: Arial,Sans-Serif; font-size: 14px; font-weight: bold;">thameswater.co.uk/mymeter</apex:outputLabel></td>
                            </tr>
                            <tr>
                               <td align="right"><apex:image url="{!$Resource.ThamesWaterCall}" /></td>
                              <td>
                                <table style="table-layout:fixed">
                                <tr>
                                    <td align="left"><apex:outputLabel style="color: #FF0000;  font-family: Arial,Sans-Serif; font-size: 14px; font-weight: bold;">0800 009 3816</apex:outputLabel><br/>
                                    <apex:outputLabel style=" font-family: Arial,Sans-Serif; font-size: 11px; " value="Mon - Fri 8am-8pm, Sat 8am-1pm"></apex:outputLabel></td>
                                </tr>
                              </table>
                              </td>
                            </tr>
                            <tr>
                                <td></td>
                                <td><br/><apex:outputLabel style="color: #195E8D;  font-family: Arial,Sans-Serif; font-size: 14px;" value="Job ref: {!TWM_PMP_Letter_Information__c.name}"/></td>
                            </tr>
                            </table>
                           </td>
                        </tr>
                    </table>
                </apex:outputPanel>   
                <br/><br/>
                &nbsp;&nbsp;
               <apex:outputText value="{0,date,dd MMMM yyyy}" style="font-family: Arial,Sans-Serif; font-size: 14px;">
                   <apex:param value="{!TWM_PMP_Letter_Information__c.Letter_1_Actual__c}" />
              </apex:outputText><br/><br/>
              <apex:outputLabel Style="color: #195E8D; font-family: Arial,Sans-Serif; font-size: 20px; font-weight: bold;">Please call us to arrange an appointment</apex:outputLabel>
              <p>
               <apex:outputLabel Styleclass="ParagraphText">Dear {!TWM_PMP_Letter_Information__c.Name__c},<br/><br/>
                Since we first wrote to you about plans to fit water meters in your area, we have had difficulty trying to<br/>
                reach you to arrange an appointment to check the best way of fitting a water meter at your property.<br/><br/>
                
                Could you please contact us as we need an appointment with you so we can identify the most suitable<br/>
                place to fit your meter. Please call <span style="color: #ff0000; font-weight: Bold;">0800 009 3816.</span>
              </apex:outputLabel>  
            </p>            
            <p>
            <apex:outputLabel style="color: #195E8D; font-family: Arial,Sans-Serif; font-size: 10pt; font-weight: Bold;">What will my appointment involve?</apex:outputLabel><br/>
            <apex:outputLabel Styleclass="ParagraphText">We will carry out a quick test that involves asking you to check whether water comes out of your tap<br/>
               after we temporarily turn your supply off. This will ensure your meter will only record your water use<br/>
               and not your neighbours.<br/><br/>
            
               If we find from this test it isn’t possible to fit your meter in the pavement or road then we will see if we<br/>
               can fit the meter under your driveway, garden or inside your home. If we are able, we will try to install<br/>
               the meter during this appointment to minimise disturbance. This should take no more than an hour.</apex:outputLabel>
            </p>
            <p>
            <apex:outputLabel style="color: #195E8D; font-family: Arial,Sans-Serif; font-size: 10pt; font-weight: Bold;" value="Why are you fitting meters?"></apex:outputLabel><br/>
            <apex:outputLabel Styleclass="ParagraphText">Managing water supplies will only get more difficult as our population grows and our climate changes.<br/>
               As a result, we need to modernise the way we manage supplies by fitting meters across our region.<br/><br/>
               
               Meters are the fairest way to pay, as you only pay for what you use, and metered customers tend to<br/>
               use 12 per cent less water. Smart technology will enable you to view your usage online to better<br/>
               control your bill<br/><br/>
               
               We hope to hear from you soon.<br/><br/>
               Yours sincerely,<br/><br/></apex:outputLabel>
               <apex:image url="{!$Resource.Danny_Leamon_Signature}" width="50" height="40"/>
             </p>
             <p>
             <apex:outputLabel Styleclass="ParagraphText">
                Danny Leamon<br/>
                Head of metering
             </apex:outputLabel>   
             </p>      
        
        <div class = "footer" style = "color: #7E7672;  font-family: Arial,Sans-Serif; font-size: 8pt;">Registered address: Thames Water Utilities Limited, Clearwater Court, Vastern Road, Reading RG1 8DB<br/>
        Company number 02366661 Thames Water Utilities Limited is part of the Thames Water Plc group. VAT registration no GB 537-4569-15</div>
    </div>
</apex:page>