0

In salesforce visual force page I am using angular for my single page application. I have a factory which has one function and also uses a remoting call. I want to use the variable defined in the factory inside my controller . Below is the code for my factory

function userRecordFetchFactory(){
                return {
            checkRecordType : function(urlObject){
                    if(urlObject && urlObject.aid && urlObject.aid.startsWith(accPrefix)){
                     objModel = new RemoteObjectModel.Account();
      }
       if (urlObject && urlObject.aid && urlObject.aid.startsWith(leadPrefix)){
               objModel = new RemoteObjectModel.Lead();
      }
      if(urlObject == null || urlObject.aid == null || urlObject.length <= 3 || objModel == null){
          displayReadingInformationErrorView();
      }
      if(objModel){
          objModel.retrieve({ where: { Id: { eq: urlObject.aid}} }, loadRecordType);
      }

      function  loadRecordType(err,records,event){
          if(err){
              displayReadingInformationErrorView();
              }
              else if(records != null && records.length == 0 ){
                  displayReadingInformationErrorView();
                  }
                  else {
                      console.log(records[0].get('Name'));
                      var custRec = {
                              Name            : records[0].get('Name')
                            , lat             : records[0].get('Latitude__c')
                            , lon             : records[0].get('Longitude__c')
                            , SiteStreet      : records[0].get('SiteStreet__c')
                            , SiteCity        : records[0].get('SiteCity__c')
                            , SiteCountryCode : records[0].get('SiteCountryCode__c')
                            , SitePostalCode  : records[0].get('SitePostalCode__c')
                            , AddressID       : records[0].get('AddressID__c')
                            , loaded          : true
                           };
                           siteInformationReadingSuccessfulView();
                       }
                  }
              }

          }
    }

I want to get custRec in the factory to be accessible in the controller. How can I achieve this??

  • If this is purely a javascript question, it belongs on Stack Overflow. – Adrian Larson Sep 22 '16 at 0:13
  • no I am using salesforce remoting also in this – user5129806 Sep 22 '16 at 0:29
  • @AdrianLarson I guess he is asking about how to access a variable in VF controller. So should be related to Salesforce – Lance Shi Sep 22 '16 at 0:29
  • I am asking for both VF controller as well as angular controller – user5129806 Sep 22 '16 at 0:35
  • Accessing data in Angular would normally be done through a service that can then be injected into a controller as shown in these examples. While you are running on Salesforce this looks more like a question about coding in JavaScript and/or Angular patterns. – Keith C Sep 22 '16 at 8:11
1

Suppose you are also using jQuery in your page. What you can do is:

In your page, add an <apex:inputHidden> tag which binds to a variable in your controller, as below:

<apex:inputHidden id="cust-Rec" value="{!custRec}"/>

Then, in your javascript code:

var custRec = {
        Name            : records[0].get('Name')
      , lat             : records[0].get('Latitude__c')
      , lon             : records[0].get('Longitude__c')
      , SiteStreet      : records[0].get('SiteStreet__c')
      , SiteCity        : records[0].get('SiteCity__c')
      , SiteCountryCode : records[0].get('SiteCountryCode__c')
      , SitePostalCode  : records[0].get('SitePostalCode__c')
      , AddressID       : records[0].get('AddressID__c')
      , loaded          : true
     };
$("input[id$='cust-Rec']").val(JSON.stringify(custRec));

This method is using jQuery and doesn't sound very Angular. If you want to use more Augular way, feel free to change it to use ng-model and two way binding. But this code should give you an idea about how to proceed.

  • yes i tried the above manner but the problem is custRec is in the factory and I cannot access it in the controller of angular – user5129806 Sep 22 '16 at 0:39
  • @user5129806 If you are asking about how to access the variable in angular controller, it is more of a stackoverflow question – Lance Shi Sep 22 '16 at 0:41
  • @user5129806 you can follow this link: stackoverflow.com/questions/16631237/… for accessing factory in controller for angular – Lance Shi Sep 22 '16 at 0:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.