Code Monkey home page Code Monkey logo

csv-export's Introduction

Parsing Salesforce json response is easy

Sometimes the best way to hanlde salesforce data is in json. If you like exeucting soql queries which include related fields, while you can see the results in developer console, if you were to execute the soql query via the sfdx cli like this -

sfdx force:data:soql:query -q "SELECT Id, Subject, WhatId, Owner.Name,Type,
    TYPEOF What WHEN Case THEN Contact.Email, Contact.State__c END
FROM Task
WHERE
      What.Type IN ('Case')  AND Subject like '%legal%' and Type = 'Call'
AND
       WhatId   IN (SELECT Id FROM Case WHERE RecordType.Name != 'iContact Service'
AND ClosedDate != TODAY)
" -r csv > data/input.csv

You would see this -

Id,Subject,WhatId,Owner.Name,Type,What
00T3E00000BpvEtUAJ,Legal Case,5003E00000FjvYQQAZ,Micheal Gill,Call,[object Object]
00T3E00000BqL8VUAV,Legal,5003E00000FjvYQQAZ,Micheal Gill,Call,[object Object]

However output as json -

sfdx force:data:soql:query -q "SELECT Id, Subject, WhatId, Owner.Name,Type,
    TYPEOF What WHEN Case THEN Contact.Email, Contact.State__c END
FROM Task
WHERE
      What.Type IN ('Case')  AND Subject like '%legal%' and Type = 'Call'
AND
       WhatId   IN (SELECT Id FROM Case WHERE RecordType.Name != 'iContact Service'
AND ClosedDate != TODAY)
" -r json > data/input.json

And you now get all your data...

{
  "status": 0,
  "result": {
    "totalSize": 2,
    "done": true,
    "records": [
      {
        "attributes": {
          "type": "Task",
          "url": "/services/data/v46.0/sobjects/Task/00T3E00000BpvEtUAJ"
        },
        "Id": "00T3E00000BpvEtUAJ",
        "Subject": "Legal Case",
        "WhatId": "5003E00000FjvYQQAZ",
        "Owner": {
          "attributes": {
            "type": "Name",
            "url": "/services/data/v46.0/sobjects/User/0053E000003dbA5QAI"
          },
          "Name": "Micheal Gill"
        },
        "Type": "Call",
        "CaseOwnerTeam__c": "Legal Services",
        "What": {
          "attributes": {
            "type": "Case",
            "url": "/services/data/v46.0/sobjects/Case/5003E00000FjvYQQAZ"
          },
          "Contact": {
            "attributes": {
              "type": "Contact",
              "url": "/services/data/v46.0/sobjects/Contact/0033E00001ASR8YQAX"
            },
            "Email": "[email protected]",
            "State__c": "Prospect"
          }
        }
      },
      {
        "attributes": {
          "type": "Task",
          "url": "/services/data/v46.0/sobjects/Task/00T3E00000BqL8VUAV"
        },
        "Id": "00T3E00000BqL8VUAV",
        "Subject": "Legal",
        "WhatId": "5003E00000FjvYQQAZ",
        "Owner": {
          "attributes": {
            "type": "Name",
            "url": "/services/data/v46.0/sobjects/User/0053E000003dbA5QAI"
          },
          "Name": "Micheal Gill"
        },
        "Type": "Call",
        "CaseOwnerTeam__c": "Legal Services",
        "What": {
          "attributes": {
            "type": "Case",
            "url": "/services/data/v46.0/sobjects/Case/5003E00000FjvYQQAZ"
          },
          "Contact": {
            "attributes": {
              "type": "Contact",
              "url": "/services/data/v46.0/sobjects/Contact/0033E00001ASR8YQAX"
            },
            "Email": "[email protected]",
            "State__c": "Prospect"
          }
        }
      }
    ]
  }
}

json looks great but sometime all you really want is a CSV file!

With a little help of node and the following libraries - you can injest json and output as csv

  • json2csv
  • fs
node bin/export.js

csv-export's People

Contributors

agentgill avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.