+ Start a Discussion
chikkuchikku 

How to fetch data form apex class to JavaScript?

I have Loan object with three fields and look up relation with application in application object also i has same three fields ,now i need to fetch the data from apex class

 This my apex code:
public static List <SObject> chart(Id id){
    List<sObject> entities= [SELECT Total_Jobs_Created__c,Total_Jobs_Lost__c,Total_Jobs_Maintained__c, Application__r.Total_Jobs_Created__c,Application__r.Total_Jobs_Lost__c,Application__r.Total_Jobs_Maintained__c
  FROM Loan__c  WHERE Id=:id];

   return entities;
}

This my js code:
loanImpactjobs() {
          console.log("sai")
        this.loading=true;
        let loanjobs={'sobjectType':'Loan__c'};
        chart({id:this.recordId }).then(data=>{
        if(data!==''&& data!== 'null' && data!=='undefined'){  
            var jobs=JSON.parse(this.details);
            this.LCreatedjobsValue=jobs["Total_Jobs_Created__c"];
            console.log(this.LMaintainedjobsValue)
            this.LLostjobsValue=jobs["Total_Jobs_Lost__c"];
            console.log(this.LCreatedjobsValue);
            this.LMaintainedjobsValue=jobs["Total_Jobs_Maintained__c"];
            console.log(this.LLostjobsValue);
           
        }
        } ).catch(error => {
            this.errorMessage(error);
        });
    }

i used this code only ,but it doesnt worked anything ineed to chnage?
Best Answer chosen by chikku
chikkuchikku
@Anudeep  
 No my requirment is to apex with js,i dont use vs page
 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Cibi, 

Can you take the following VF remoting example as a reference?

Apex Code
global with sharing class AccountRemoter {

    public String accountName { get; set; }
    public static Account account { get; set; }
    public AccountRemoter() { } // empty constructor
    
    @RemoteAction
    global static Account getAccount(String accountName) {
        account = [SELECT Id, Name, Phone, Type, NumberOfEmployees 
                   FROM Account WHERE Name = :accountName];
        return account;
    }
}
 
function getRemoteAccount() {
        var accountName = document.getElementById('acctSearch').value;

        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.AccountRemoter.getAccount}',
            accountName, 
            function(result, event){
                if (event.status) {
                    // Get DOM IDs for HTML and Visualforce elements like this
                    document.getElementById('remoteAcctId').innerHTML = result.Id
                    document.getElementById(
                        "{!$Component.block.blockSection.secondItem.acctNumEmployees}"
                        ).innerHTML = result.NumberOfEmployees;
                } else if (event.type === 'exception') {
                    document.getElementById("responseErrors").innerHTML = 
                        event.message + "<br/>\n<pre>" + event.where + "</pre>";
                } else {
                    document.getElementById("responseErrors").innerHTML = event.message;
                }
            }, 
            {escape: true}
        );
    }

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_example.htm
chikkuchikku
@Anudeep  
 No my requirment is to apex with js,i dont use vs page
 
This was selected as the best answer