1

I'm getting an

Error: Can't find variable: UserEntity

that is happening in the file task-calendar-controller.js

I'm using angular to make a salesforce app. The UserEntity is used to convert the retrieved Salesforce database data to JavaScript.

File structure.

enter image description here

app.js

angular

    .module("taskCalendar", ['ngRoute'])

    .constant('resourceUrl', '/resource/'+Date.now()+'/taskmanagement__taskCalendar/taskCalendar/app')

    .config(['$routeProvider', 'resourceUrl', function($routeProvider, resourceUrl) {
        $routeProvider
            .when('/', {
                templateUrl:  resourceUrl+'/task/task-calendar.html'
                ,controller: 'taskCalendarController'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])

    .service('task', function() {
        return SObjectModel.Task;
    })

    .service('user', function() {
        return SObjectModel.User;
    });

user-entity.js

//Used to convert the retrieved Salesforce database data to JavaScript

var UserEntity = function() {

};

UserEntity.fromRemoteObjectModel = function(model) {
    var entity = new UserEntity();

    entity.id = model.get('Id');
    entity.firstName = model.get('FirstName');
    entity.lastName = model.get('LastName');
    entity.email = model.get('Email');

    return entity;
}

UserEntity.prototype.toRemoteObjectModelDetails = function() {
    var details = {};

    if (this.id) {
        details.Id = this.id;
    }

    details.FirstName = this.firstName;
    details.LastName = this.lastName;
    details.Email = this.email;

    return details;
}

UserEntity.prototype.id = '';
UserEntity.prototype.firstName = '';
UserEntity.prototype.lastName = '';
UserEntity.prototype.email = '';

task-calendar-controller.js

angular

    .module('taskCalendar')

    .controller('taskCalendarController', ['$scope', 'task', 'user', function($scope, task , user) {
        //Search box code
        var u = new user();
        $scope.users = []
        $scope.searchBox = null;

       // Get user input from search box
        $scope.change = function(text) {
            userInput = $scope.searchBox;
            console.log('searchBox = ' + userInput);

            // Make input useable for search criteria
            var elements = userInput.split(" ");
            var first = (elements[0] || "") + "%";
            var last  = (elements[1] || "") + "%";

            // Salesforce criteria to be able to search server
            var criteria = { where: {
                    FirstName: {like: first},
                    LastName:  {like: last}
                },
                orderby: [ {LastName: 'ASC'}, {FirstName: 'ASC'} ],
                limit: 10 
            };

            u.retrieve(criteria, function(error, results, event){

                if(error) {
                    alert(error.message);
                }
                $scope.$apply(function() {

                    results.forEach(function(element, index, array) {

                        // Error happens right here
                        var entity = UserEntity.fromRemoteObjectModel(element);
                        $scope.users.push(entity);
                    });
                    console.log($scope.users);
                });
            });}

}]);

task-calendar.html

 <input type="text" class="form-control" id="typeahead_example_1" name="typeahead_example_1" ng-model="searchBox" ng-change="change(text)">

1 Answer 1

1

You should create a new instance of UserEntity in your controller before you can use it.

var _userentity = new UserEntity();
_userentity.fromRemoteObjectModel(element);
Sign up to request clarification or add additional context in comments.

1 Comment

create it anywhere after .controller('taskCalendarController', ['$scope', 'task', 'user', function($scope, task , user) { ? If that is the case, it's not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.