+ Start a Discussion
Krishna VegirajuKrishna Vegiraju 

visualforce page not displaying data pulled in through rest call

I have a visual force page as a hyperlink on account details page. I would like to display information about contracts related to that particalar account through the page. There is a catch through, users accessing the page does not have access to contract object, so I built a rest call to call the page as me everytime they try to access the page. I checked the debug logs, contract details are being pulled in, but are all the user can see is blank spaces on vf page. I am posting my code, please let me know what I am doing wrong. 

Vf page:
<apex:page standardController="Account" extensions="ContractdetailsfromaccountController" >
    
    <apex:outputPanel >
        <div style="float:Left;margin-top:2em;font-family:  sans-serif;font-size:12px;/*width:50%;*/">
            <b>Account Name: {!Account[0].Name}<br/>
                Account Phone: {!Account[0].Phone}</b><br/>
                <apex:outputPanel rendered="{!!error}">
            <b>Contact: {!contracts[0].Welcome_Letter_Recipient__r.Name}</b><br/>
            </apex:outputPanel>
        </div>
    </apex:outputPanel>
    <br/><br/><br/><br/><br/><br/>
    <apex:outputPanel rendered="{!error}">
            <td style="font-family:  sans-serif;font-size:30px;padding-left: 0.6em;padding-right: 0.6em;"><b>This Account Does not have any contracts linked to it. </b></td>
    </apex:outputPanel>
    <br/><br/>
    <apex:outputPanel rendered="{!!error}">
    <apex:form >
    <apex:commandLink styleclass="start" target="_blank" onclick="window.open('/apex/Contractdetailsfromaccount', 'width=300, height=250' return false;"/>
    <apex:pageblock > 
        <apex:repeat value="{!contracts}" var="con" id="therepeat">
            <apex:pageBlockSection title="Contract Details" collapsible="false" columns="2" >
                <apex:outputText label="Contract Number" value="{!con.Name}"/>
                <apex:outputText value="{!con.cllease__Lease_Status__c}"/>
                <apex:outputText label="Funded Amount" value="{0}{1, number, ###,##0.00}"><apex:param value="$"/><apex:param value="{!con.cllease__Lease_Receivable_Amount__c}"/></apex:outputText>
                <apex:outputText label="Opportunity Owner" value="{!con.Opportunity_Owner_Sales_Rep__c}"/>
                <apex:outputText label="Payment Amount" value="{0}{1, number, ###,##0.00}"><apex:param value="$"/><apex:param value="{!con.cllease__Payment_Amount__c}"/></apex:outputText>
                <apex:outputText label="Channel" value="{!con.Sales_Channel__c}"/>
                <apex:outputText label="Total Due Amount" value="{0}{1, number, ###,##0.00}"><apex:param value="$"/><apex:param value="{!con.cllease__Amount_to_Current__c}"/></apex:outputText>
                <apex:outputText value="{!con.cllease__Term__c}"/>
                <apex:InputCheckbox value="{!con.Ever_Delinquent__c}" disabled="true"/>
                <apex:outputText label="Start Date" value="{0,date,MM'/'dd'/'yyyy}"><apex:param value="{!con.cllease__Commencement_Date__c}"/></apex:outputText>
                <apex:outputText label="Delinquent Amount" value="{0}{1, number, ###,##0.00}"><apex:param value="$"/><apex:param value="{!con.cllease__Delinquent_Amount__c}"/></apex:outputText>
            	<apex:outputText label="End Date"  value="{0,date,MM'/'dd'/'yyyy}"><apex:param value="{!con.cllease__Maturity_Date__c}"/></apex:outputText>
                <apex:outputText label="Delinquent Days" value="{!con.cllease__Days_Past_Due__c}"/>
                <apex:outputText label="Next Due Date" value="{0,date,MM'/'dd'/'yyyy}"><apex:param value="{!con.cllease__Next_Due_Date__c}"/></apex:outputText>
                <apex:outputText label="Outstanding Bills" value="{!con.cllease__Dues_Details__r.size}"/>
                <apex:outputText label="Gross Yield" value="{0, number, ###,##0.00}{1}"><apex:param value="{!con.Gross_Yield__c}"/><apex:param value="%"/></apex:outputText>
                <apex:outputText value="{!con.Pre_Payment_Option__c}"/>
                <apex:outputText label="Net Yield" value="{0, number, ###,##0.00}{1}"><apex:param value="{!con.Net_Yield__c}"/><apex:param value="%"/></apex:outputText>
                <apex:InputCheckbox value="{!con.Early_Buyout_Option__c}" disabled="true"/>
                <apex:InputCheckbox label="Set up on ACH?" value="{!con.ACH_Configured__c}" disabled="true"/>
                <apex:outputText value="{!con.Same_as_Cash_Option__c}"/>
                <apex:outputText label="Pieces of Equipment" value="{!con.cllease__Contract_Equipments__r.size}"/>
                <apex:outputText label="No of Payments Remaining" value="{!con.cllease__Payment_Streams__r.size}"/>
                <!---<apex:InputCheckbox rendered="{!AND(con.cllease__Charges__r !=Null, con.cllease__Charges__r.size>0)}" label="Enrolled in Insurance?" value="{!EPP}" disabled="true"/>
                <apex:InputCheckbox rendered="{!AND(con.cllease__Charges__r !=Null,con.cllease__Charges__r.size=0)}" label="Enrolled in Insurance?" value="{!EPPfalse}" disabled="true"/>--->
                <apex:outputText />
                 <apex:outputText />
                <apex:outputText />
                <!---<apex:outputText label="Insurance Payment Amount" value="${!IF((con.cllease__Charges__r.size>0),con.cllease__Charges__r[0].cllease__Original_Amount__c,0.00)}"/>---> 
                <br/>
         <div style="border: 3px solid #afb3b1;border-radius:0px;background-color:#afb3b1;color:black;width:200%;text-align:Left;font-size:11px;font-family:  sans-serif"><b>Equipment Information</b></div>
        	<apex:repeat value="{!con.cllease__Contract_Equipments__r}" var="Equip">
                <tr>
                 <apex:outputText value="{!Equip.Equipment_Type_Name__c}"/>
                 <apex:outputText value="{!Equip.Equipment_Sub_Type_Name__c}"/>
                 <apex:outputText value="{!Equip.cllease__Total_Dealer_Charges__c}"/>
                 <apex:outputText value="{!Equip.cllease__Make__c}"/>
                 <apex:outputText value="{!Equip.cllease__Equipment_Description__c}"/>
                 <apex:outputText value="{!Equip.cllease__Model__c}"/>
                    <div style="width:200%; height:1px; background:grey;"></div>
                </tr>
         	</apex:repeat>
           </apex:pageBlockSection>
        </apex:repeat>
    </apex:pageblock>
   </apex:form>
    </apex:outputPanel>
</apex:page>

Controller:
public without sharing class ContractdetailsfromaccountController {
    public list<cllease__Lease_Account__c> contracts {get; set;}
    public List<Account> Account {get; set;}
    public boolean error {set; get;}
    public boolean EPP {set; get;}
    public boolean EPPfalse {set; get;}
   
    public ContractdetailsfromaccountController(ApexPages.standardController Acc){
        Account = [select id, Name, Phone from Account where Id =: Acc.getId()];
        
        HttpResponse resp = getRecord();
        if(resp != null && resp.getStatusCode() == 200) {
            System.debug(resp.getBody());
        	contracts = (list<cllease__Lease_Account__c>) JSON.deserialize(resp.getBody(), list<cllease__Lease_Account__c>.class);
        }
        EPP=true;
        EPPfalse =false;
        If(contracts == null || contracts.size()==0){
            error =true;
        }
    }
    public HttpResponse getRecord() {
        Map<String, String> authDetail;
        
        Integration_Details__c cred = Integration_Details__c.getInstance('ContractAccess');
        
        if(cred != null) {
            Blob decryptedData = Crypto.decryptWithManagedIV('AES256', EncodingUtil.base64Decode(cred.Key__c), EncodingUtil.base64Decode(cred.Password__c));
            authDetail = getSessionId(cred.Instance_URL__c, cred.UserName__c, decryptedData.toString());
        } else {
            // error handling
        }
        
        HttpRequest req = new HttpRequest();
        req.setEndpoint(cred.Instance_URL__c+'/services/apexrest/contractDetailsFromAccount/'+Account[0].Id);
        req.setMethod('GET');
        req.setHeader('Authorization', 'OAuth ' + authDetail.get('sessionId'));
        System.debug(req);
        return new Http().send(req);
    }
    
    public Map<String, String> getSessionId(String url, String username, String password) {
        Map<String, String> authDetails;
        String sessionId;
        String email;

        HttpRequest request = new HttpRequest();
        request.setEndpoint(url+'/services/Soap/u/44.0');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        // creating soap xml request
        request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>'
                        + username + '</username><password>' + password + '</password></login></Body></Envelope>');
		
        HttpResponse resp = new Http().send(request);

        if(resp != null && resp.getStatusCode() == 200) {

        	Dom.Document doc = resp.getBodyDocument();

            sessionId = doc.getRootElement().getChildElement('Body','http://schemas.xmlsoap.org/soap/envelope/').getChildElement('loginResponse','urn:partner.soap.sforce.com')
                .getChildElement('result','urn:partner.soap.sforce.com').getChildElement('sessionId','urn:partner.soap.sforce.com').getText();

            email = doc.getRootElement().getChildElement('Body','http://schemas.xmlsoap.org/soap/envelope/').getChildElement('loginResponse','urn:partner.soap.sforce.com')
                .getChildElement('result','urn:partner.soap.sforce.com').getChildElement('userInfo','urn:partner.soap.sforce.com').getChildElement('userEmail','urn:partner.soap.sforce.com').getText();

            System.debug('sending request as '+email);

            authDetails = new Map<String, String>();
            authDetails.put('sessionId', sessionId);
            authDetails.put('email', email);
        } else {
            System.debug('resp error: '+resp.getBody());
        }

        return authDetails;
    }

}

Api:
@RestResource(urlMapping = '/contractDetailsFromAccount/*')
global class ContractDetailsAPI {
    
    @HttpGET
    global static List<cllease__Lease_Account__c> getContractsFromAccount() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        list<cllease__Lease_Account__c> contracts = [select id, name, cllease__Account__c, cllease__Account__r.name,Account_Phone__c, cllease__Lease_Status__c,ACH_Configured__c,
                     cllease__Lease_Receivable_Amount__c,cllease__Commencement_Date__c,cllease__Maturity_Date__c,cllease__Payment_Amount__c,
                     cllease__Next_Due_Date__c,cllease__Amount_to_Current__c,cllease__Delinquent_Amount__c, Net_Yield__c, Gross_Yield__c, cllease__Term__c,
                     Opportunity_Owner_Sales_Rep__c,Sales_Channel__c, cllease__Days_Past_Due__c,Welcome_Letter_Recipient__c,Welcome_Letter_Recipient__r.Name,
                     Early_Buyout_Option__c,Pre_Payment_Option__c,Same_as_Cash_Option__c,Ever_Delinquent__c,
                     (select id, cllease__Payment_Satisfied__c from cllease__Dues_Details__r where cllease__Payment_Satisfied__c=false),
                     (select id, cllease__Billed__c, cllease__Active__c from cllease__Payment_Streams__r where cllease__Billed__c=false and cllease__Active__c=true),
                     (select id, cllease__Fee_Definition__c, cllease__Fee_Definition__r.Name, cllease__Original_Amount__c from cllease__Charges__r 
                      where cllease__Fee_Definition__r.Name='Equipment Protection Program'),
                     (select id, cllease__Equipment_Cost__c, Equipment_Type_Name__c, Equipment_Sub_Type_Name__c,
                      cllease__Equipment_Description__c, cllease__Model__c, cllease__Make__c,cllease__Equipment_Serial_Number__c, cllease__Total_Dealer_Charges__c
                      from cllease__Contract_Equipments__r order by cllease__Equipment_Cost__c DESC)
                     from cllease__Lease_Account__c where cllease__Account__c =: accId and cllease__Lease_Status__c !='PARTIAL APPLICATION' order by createddate DESC];
       
        return contracts;
    }
}

Would really appreciate any help in this.

Thanks,
​​​​​​​Krishna Vegiraju
Shiva RajendranShiva Rajendran
Hey Krishna
It is a bit out of topic question, even if user dont have profile level security , still they can see the records in visualforce, not sure why you used soap here to get the records if it's from the same salesforce instance.
Could you elaborate on the requirement ?

Thanks,
Shiva RV
Krishna VegirajuKrishna Vegiraju
Hey Shiva,

Thanks for the response. I should have been clear. The object is part of a managed package, hence, users without license cannot read the data from the object. 
On a positive note. I was able to figure the issue out, it is working now. I wrote a wrapper class and added field values to variables and used them in vf page. It is working now. 

Thanks,
Krishna Vegiraju
Shiva RajendranShiva Rajendran
Was it Getter setter issue then ? Cool
Krishna VegirajuKrishna Vegiraju
Not getter setter issue to be precise. Like you can see in the code I posted, querying the object through api and feeding it to controller. The issue was with how outputtext was not able to read that data. So, I had to decalre each field as a variable(getters and setters) and then use those variables in output text instead of the actual fields itself. 

Thanks,
Krishna Vegiraju
Shiva RajendranShiva Rajendran
Ok Krishna.. cool