1.0.6 • Published 6 years ago

ng-salesforce v1.0.6

Weekly downloads
537
License
-
Repository
github
Last release
6 years ago

ng-salesforce

Pre Installation

Prior to installing ng-salesforce, you must have access to a Salesforce org and have an @angular/cli application.

Installation

To install this library, run:

$ npm install ng-salesforce

During installation, you will be asked for a number of things to connect the application to your salesforce org.

and then from your Angular AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// Import your library
import { SalesforceModule } from 'ng-salesforce';

// salesforce.config.ts will be created during installation and saved at src/app/salesforce.config.ts
import { Configuration } from './salesforce.config';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,

    // Specify your ng-salesforce as an import
    SalesforceModule.forRoot(Configuration)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Once your library is imported, you can use its components, directives and pipes in your Angular application:

Defining a model

Propert state management is crucial to a successful Angular PWA. To that end, accessing data within Salesforce requires you to define your model within a Typescript class. Behind the scenes, ng-salesforce is managing how your components access and interact with that data to avoid uncessary operations and maintain synchronicity.

Here's an example that defines the account and contact relationship for a component.

import { SObjectModel, SObject, ChildRecord } from 'ng-salesforce';

// We use the decorator SObjectModel to tell ng-salesforce this class maps to the 'Account' object
// In order to access standard sObject fields, our Account class must extend 'SObject'
// The class name can be anything, but the name property in the SObjectModel decorator must map to an object API name
@SObjectModel({name : 'Account'})             
export class Account extends SObject{         

  // Define the fields you would like in your object. The name of the field must map to a field API name in salesforce
  public Name: string = null;                 
  public IsCustomerPortal: boolean = null;
  public My_Custom_Field__c: number = null;

  //Related Lists
  public Opportunities: ChildRecord = new ChildRecord(new Opportunity())
}

@SObjectModel({name : 'Opportunity'})
export class Oppoortunity extends SObject{
  public Name: string = null;
  public AccountId: string = null;
}

@SobjectModel({name : 'Contact'})
export class Contact extends SObject{
  public Name: string = null;

  //Lookup
  public Account: Account = new Account();
}

Defining a Service

After defining your model, you can access the data by creating a service for the models. The SObjectService class contains many of the standard DML and query operations to access the data, however you may add any convenience methods you want to your service. The core service methods are usually very simple.

import { SObjectService, SObjectType } from 'ng-salesforce';
import { Injectable } from '@angular/core';
import { Account } from './account.model.ts'  // This is a reference to the account model created in the previous section


@Injectable({
    providedIn : 'root'
})
export class AccountService extends SObjectService{
  //Add service methods here
  type = Account;
}

Access the data in your component

import { Component, OnOnit } from '@angular/core';
import { AccountService } from './account.service.ts';
import { Account } from './account.model.ts';

@Component({
  selector : 'app-account',
  template : `

  `,
  styles : [``]
})
export class AccountComponent implements OnInit{
  
  constructor(private accountService: AccountService){}

  // Always perform service methods in the ngOnInit method
  ngOnInit(){
    this.accountService.where(`Id <> NULL LIMIT 1`).subscribe(res => {
      /*
      res === [
        {
          Name : 'Account A',
          IsCustomerPortal : false,
          My_Custom_Field__c : 1,
          Id : 'xxx',
          ...
          Opportunities : {
            totalSize : 1,
            records : [
              Name : 'Opportunity A',
              AccountId : 'xxx',
              Id : 'xxx',
              ...
            ]
          }
        }
      ]
      */
    });
    this.accountService.describe('My_Custom_Field__c', false).subscribe(res => {
      // Describe information for My_Custom_Field__c
    });

    this.accountService.search(`FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name)`).subscribe(res => {
      // SOSL Search Results
      // Note : search does not follow the model pattern and will return results specified in the query
    });

    this.accountService.get(['00Fxxxxxxx', '00Fxxxxxxx']).subscribe(res => {
      // Returns an array of account records
    })

    this.accountService.aggregate(`ID <> NULL`).subscribe(res => {
      // Returns aggregates for the specified clause. (i.e. total records as well as max/min values for all 
      // fields specified in the model)
    })

    this.accountService.create([new Account()]).subscribe(res => {
      // Returns list of id's created
    })
  
    this.accountService.update([new Account()]).subscribe(res => {
      // Returns list of id's updated
    })  

    this.accountService.upsert([new Account()]).subscribe(res => {
      // Returns list of objects upserted
    }) 

    this.accountService.delete([new Account()]).subscribe(res => {
      // Returns list of boolean values for accounts that were successfully deleted
    }) 

    /**
     *  Low level method to build a structured query
     *  @Param where clause
     *  @Param limit (optional)
     *  @Param offset (optional)
     *  @Param sort by (optional)
     *  @Param sort direction (optional)
     *  @Param ignore cache (optional default false)
     *  @Param ignore constraints (optional default false)
     */
    this.accountService.queryBuilder('ID <> NULL', 5, 2, 'Name', 'ASC', false, false).subscribe(res => {
      // Returns query results
    })
  }
}

deploy

To lint all *.ts files:

$ npm run lint

To deploy your code to your salesforce org

$ npm run deploy

Known Issues

@Angular/cli 6.0.0 Can't Resolve Stream

If you've upgraded to @angular/cli 6, and you're seeing the following error

WARNING in C:/Workspace/ngs-workspace/node_modules/xml2js/node_modules/sax/lib/sax.js
Module not found: Error: Can't resolve 'stream' in 'C:\Workspace\ngs-workspace\node_modules\xml2js\node_modules\sax\lib'

ERROR in C:/Workspace/ngs-workspace/node_modules/csv-parse/lib/index.js
Module not found: Error: Can't resolve 'stream' in 'C:\Workspace\ngs-workspace\node_modules\csv-parse\lib'
ERROR in C:/Workspace/ngs-workspace/node_modules/csv-stringify/lib/index.js
Module not found: Error: Can't resolve 'stream' in 'C:\Workspace\ngs-workspace\node_modules\csv-stringify\lib'
ERROR in C:/Workspace/ngs-workspace/node_modules/xml2js/lib/parser.js
Module not found: Error: Can't resolve 'timers' in 'C:\Workspace\ngs-workspace\node_modules\xml2js\lib'
i 「wdm」: Failed to compile.

Fix: Add path

Add the following to your tsconfig.app.json file under 'compilerOptions':

"paths" : {
  "jsforce" : ["./node_modules/jsforce/build/jsforce.min.js"]
  ...
}

Uncaught Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.

In order to work on a visualforce page, your app needs to be setup to use the hash routing location strategy instead of the default

Fix: Set the useHash flag in your app-routing.module.ts file

In your app-routing.module.ts file, set the useHash flag in the RouterModule.forRoot(...) call

@NgModule({
  imports: [RouterModule.forRoot(appRoutes,  { useHash: true })],
  exports: [RouterModule]
})
export class AppRoutingModule { }

No 'Access-Control-Allow-Origin' header is present on the requested resource

Even if you enabled CORS during setup, Salesforce only supports it for certain APIs. This won't be an issue when you deploy to production. However, during development you will see this error for calls to the SOAP API. To bypass this, you must setup a proxy in @angular/cli.

Fix: Setup @angular/cli proxy

First, create a file called 'proxy.conf.json' in your root directory. This json file will look like the following:

{
  "/services/Soap/*": {
    "target": "https://my.community.url.force.com",
    "secure": false,
    "logLevel": "debug"
  }
}

Make sure you set the target to be the community url you are using.

Secondly, point your angular.json configuration to this newly created proxy configuration.

{
  ...
  "projects" : {
    ...
    "myProject" : {
      ...
      "architect" : {
        ...
        "serve" : {
          ...
          "options" : {
            ...
            "proxyConfig": "./proxy.conf.json"
          }
        }
      }
    }
  }
}

License

MIT © Christopher Moyle

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.2327

6 years ago

0.1.2326

6 years ago

0.1.2325

6 years ago

0.1.2324

6 years ago

0.1.2323

6 years ago

0.1.2322

6 years ago

0.1.2320

6 years ago

0.1.2319

6 years ago

0.1.2318

6 years ago

0.1.2317

6 years ago

0.1.2316

6 years ago

0.1.2315

6 years ago

0.1.2314

6 years ago

0.1.2313

6 years ago

0.1.2311

6 years ago

0.1.2310

6 years ago

0.1.2309

6 years ago

0.1.2308

6 years ago

0.1.2307

6 years ago

0.1.2306

6 years ago

0.1.2305

6 years ago

0.1.2304

6 years ago

0.1.2303

6 years ago

0.1.2302

6 years ago

0.1.2301

6 years ago

0.1.2300

6 years ago

0.1.2212

6 years ago

0.1.2211

6 years ago

0.1.2210

6 years ago

0.1.2209

6 years ago

0.1.2208

6 years ago

0.1.2207

6 years ago

0.1.2206

6 years ago

0.1.2205

6 years ago

0.1.2204

6 years ago

0.1.2203

6 years ago

0.1.2202

6 years ago

0.1.2201

6 years ago

0.1.2200

6 years ago

0.1.2110

6 years ago

0.1.2109

6 years ago

0.1.2108

6 years ago

0.1.2107

6 years ago

0.1.2106

6 years ago

0.1.2105

6 years ago

0.1.2104

6 years ago

0.1.2103

6 years ago

0.1.2102

6 years ago

0.1.2101

6 years ago

0.1.2100

6 years ago

0.1.2015

6 years ago

0.1.2014

6 years ago

0.1.2013

6 years ago

0.1.2012

6 years ago

0.1.2010

6 years ago

0.1.2009

6 years ago

0.1.2008

6 years ago

0.1.2007

6 years ago

0.1.2006

6 years ago

0.1.2005

6 years ago

0.1.2004

6 years ago

0.1.2003

6 years ago

0.1.2002

6 years ago

0.1.2001

6 years ago

0.1.2000

6 years ago

0.1.1199

6 years ago

0.1.1198

6 years ago

0.1.1197

6 years ago

0.1.1195

6 years ago

0.1.119

6 years ago

0.1.118

6 years ago

0.1.117

6 years ago

0.1.116

6 years ago

0.1.115

6 years ago

0.1.114

6 years ago

0.1.112

6 years ago

0.1.111

6 years ago

0.1.110

6 years ago

0.1.109

6 years ago

0.1.108

6 years ago

0.1.107

6 years ago

0.1.106

6 years ago

0.1.105

6 years ago

0.1.104

6 years ago

0.1.103

6 years ago

0.1.102

6 years ago

0.1.101

6 years ago

0.1.100

6 years ago

0.1.94

6 years ago

0.1.90

6 years ago

0.1.89

6 years ago

0.1.87

6 years ago

0.1.86

6 years ago

0.1.85

6 years ago

0.1.84

6 years ago

0.1.83

6 years ago

0.1.82

6 years ago

0.1.81

6 years ago

0.1.80

6 years ago

0.1.79

6 years ago

0.1.78

6 years ago

0.1.77

6 years ago

0.1.76

6 years ago

0.1.75

6 years ago

0.1.74

6 years ago

0.1.73

6 years ago

0.1.72

6 years ago

0.1.71

6 years ago

0.1.70

6 years ago

0.1.69

6 years ago

0.1.68

6 years ago

0.1.67

6 years ago

0.1.66

6 years ago

0.1.65

6 years ago

0.1.64

6 years ago

0.1.63

6 years ago

0.1.62

6 years ago

0.1.61

6 years ago

0.1.60

6 years ago

0.1.56

6 years ago

0.1.55

6 years ago

0.1.54

6 years ago

0.1.53

6 years ago

0.1.52

6 years ago

0.1.51

6 years ago

0.1.50

6 years ago

0.1.40

6 years ago

0.1.39

6 years ago

0.1.38

6 years ago

0.1.37

6 years ago

0.1.36

6 years ago

0.1.35

6 years ago

0.1.34

6 years ago

0.1.33

6 years ago

0.1.32

6 years ago

0.1.31

6 years ago

0.1.30

6 years ago

0.1.29

6 years ago

0.1.28

6 years ago

0.1.27

6 years ago

0.1.26

6 years ago

0.1.25

6 years ago

0.1.24

6 years ago

0.1.23

6 years ago

0.1.22

6 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

7 years ago