github.com/jfcote87/salesforce@v0.1.0/example_test.go (about)

     1  // Copyright 2022 James Cote
     2  // All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package salesforce_test
     7  
     8  import (
     9  	"context"
    10  	"log"
    11  
    12  	"github.com/jfcote87/oauth2"
    13  	"github.com/jfcote87/oauth2/clientcredentials"
    14  	"github.com/jfcote87/salesforce"
    15  	"github.com/jfcote87/salesforce/auth/jwt"
    16  )
    17  
    18  const credentialFilename = "testfiles/example.jwt.json"
    19  
    20  func ExampleService_Call() {
    21  	ctx := context.Background()
    22  	sv, err := jwt.ServiceFromFile(credentialFilename, nil)
    23  	if err != nil {
    24  		log.Fatalf("%v", err)
    25  	}
    26  	// retrieve contact records
    27  	var records []Contact
    28  	var qry = "SELECT Id, Name FROM Contact WHERE MailingPostalCode = '80907'"
    29  	if err := sv.Query(ctx, qry, &records); err != nil {
    30  		log.Fatalf("query error %v", err)
    31  	}
    32  
    33  	var updateRecs []salesforce.SObject
    34  
    35  	// prepare updates
    36  	for _, c := range records {
    37  		c.DoNotCall = true
    38  		updateRecs = append(updateRecs)
    39  	}
    40  
    41  	opResponses, err := sv.UpdateRecords(ctx, false, updateRecs)
    42  	if err != nil {
    43  		log.Fatalf("update error %v", err)
    44  	}
    45  	// loop through responses looking for errors
    46  	for i, r := range opResponses {
    47  		if !r.Success {
    48  			contact, _ := updateRecs[i].(Contact)
    49  			log.Printf("%s %s %v", contact.ContactID, contact.Name, r.Errors)
    50  		}
    51  	}
    52  }
    53  
    54  func ExampleNew_password() {
    55  	// The example uses username/password entries to authorize the new service. More details may be found at:
    56  	// https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_username_password_flow.htm&type=5
    57  
    58  	cfg := &clientcredentials.Config{
    59  		ClientID:     "<clientID>",
    60  		ClientSecret: "<clientSecret>",
    61  		TokenURL:     "https://login.salesforce.com/services/oauth2/token",
    62  		EndpointParams: map[string][]string{
    63  			"grant_type": {"<password>"},
    64  			"username":   {"<username>"},
    65  			"password":   {"<password>" + "<securityToken>"},
    66  		},
    67  	}
    68  	sv := salesforce.New("<instance url>", "<version>", cfg.TokenSource(nil))
    69  	var recs []Contact
    70  	if err := sv.Query(context.Background(), "SELECT Id FROM Contact", &recs); err != nil {
    71  		log.Fatalf("query error: %v", err)
    72  	}
    73  	log.Printf("total recs = %d", len(recs))
    74  
    75  }
    76  
    77  func ExampleNew_fromtoken() {
    78  	tk := &oauth2.Token{
    79  		AccessToken: "valid token from somewhere",
    80  	}
    81  	sv := salesforce.New("<instance url>", "<version>", oauth2.StaticTokenSource(tk))
    82  	var recs []Contact
    83  	if err := sv.Query(context.Background(), "SELECT Id FROM Contact", &recs); err != nil {
    84  		log.Fatalf("query error: %v", err)
    85  	}
    86  	log.Printf("total recs = %d", len(recs))
    87  }