jsforce icon indicating copy to clipboard operation
jsforce copied to clipboard

How to get an Opportunity Sales Process / Stage / Selected Values

Open zo103181 opened this issue 9 years ago • 3 comments

What I am attempting to do is get the selected values from the screen shot below:

process-renewal-stages

Using the following function I can get the different Sales Processes:

Layouts SObject

 conn.sobject("Opportunity").layouts(function(err, meta) {
   if (err) { return console.error(err); }
   console.log(meta["recordTypeMappings"]);
 });

Results:

[
    {
        "available": true,
        "defaultRecordTypeMapping": true,
        "layoutId": "00h41000005Xx7fAAC",
        "master": false,
        "name": "New Business",
        "picklistsForRecordType": [],
        "recordTypeId": "01241000000vEPaAAM",
        "urls": {
            "layout": "/services/data/v37.0/sobjects/Opportunity/describe/layouts/01241000000vEPaAAM"
        }
    },
    {
        "available": true,
        "defaultRecordTypeMapping": false,
        "layoutId": "00h4100000401ZZAAY",
        "master": false,
        "name": "Renewal",
        "picklistsForRecordType": [],
        "recordTypeId": "01241000000vE4gAAE",
        "urls": {
            "layout": "/services/data/v37.0/sobjects/Opportunity/describe/layouts/01241000000vE4gAAE"
        }
    },
    {
        "available": true,
        "defaultRecordTypeMapping": false,
        "layoutId": "00h41000005Xx42AAC",
        "master": false,
        "name": "Win Back",
        "picklistsForRecordType": [],
        "recordTypeId": "01241000000vELDAA2",
        "urls": {
            "layout": "/services/data/v37.0/sobjects/Opportunity/describe/layouts/01241000000vELDAA2"
        }
    },
    {
        "available": true,
        "defaultRecordTypeMapping": false,
        "layoutId": "00h41000005Xx7fAAC",
        "master": true,
        "name": "Master",
        "picklistsForRecordType": [],
        "recordTypeId": "012000000000000AAA",
        "urls": {
            "layout": "/services/data/v37.0/sobjects/Opportunity/describe/layouts/012000000000000AAA"
        }
    }
]

Using the Salesforce Workbench: REST Explorer I am able to paste in the Renewal > "urls" > "layout": /services/data/v37.0/sobjects/Opportunity/describe/layouts/01241000000vE4gAAE and see that the values I am looking for are buried in the following path

editLayoutSections/[Item 1]/layoutRows/[Item 4]/layoutItems/Stage/layoutComponents/StageName/details/picklistValues

Is there a way in JSforce to get the picklistValues?

zo103181 avatar Jul 27 '17 15:07 zo103181

Found the following Pull Request #586 referencing layouts/recordTypeId

zo103181 avatar Jul 28 '17 14:07 zo103181

Using the following JSforce logic I was able to get what I was looking for:

var _request = {
    body: "",
    header: { "Content-Type": "application/json" },
    method: "get",
    url: "/services/data/v37.0/sobjects/Opportunity/describe/layouts/01241000000vE4gAAE"
};

conn.request(_request, function(err, resp) {
    if (err) return res.send(err.toString());
    console.log(resp["editLayoutSections"][0]["layoutRows"][3]["layoutItems"][1]["layoutComponents"][0]["details"]["picklistValues"]);
});

zo103181 avatar Aug 01 '17 18:08 zo103181

The original logic to get the picklistValues was not very dynamic, so with the help of the code from Alex Quan I made the following changes:

function findObjects(obj, targetProp, targetValue, finalResults) {
    function getObject(theObject) {
        let result = null;
        if (theObject instanceof Array) {
            for (let i = 0; i < theObject.length; i++) {
                getObject(theObject[i]);
            }
        } else {
            for (let prop in theObject) {
                if (theObject.hasOwnProperty(prop)) {
                    if (prop === targetProp) {
                        if (theObject[prop] === targetValue) {
                            finalResults.push(theObject);
                        }
                    }
                    if (theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
                        getObject(theObject[prop]);
                    }
                }
            }
        }
    }
    getObject(obj);
};

var _request = {
    body: "",
    header: { "Content-Type": "application/json" },
    method: "get",
    url: "/services/data/v37.0/sobjects/Opportunity/describe/layouts/01241000000vE4gAAE"
};

conn.request(_request, function(err, resp) {
    if (err) return res.send(err.toString());

    var StageNames = [];
    var Types = [];
    findObjects(resp, 'name', 'StageName', StageNames);
    findObjects(resp, 'name', 'Type', Types);
    console.log('StageNames: ', StageNames[0]["picklistValues"]);
    console.log('Types:', Types[0]["picklistValues"]);
});

zo103181 avatar Aug 02 '17 23:08 zo103181