ChuckJonas / ts-force

A Salesforce REST Client written in Typescript for Typescript
88 stars 21 forks source link

Access a SObject property by Salesforce API name? #67

Closed ScottMGerstl closed 5 years ago

ScottMGerstl commented 5 years ago

I have a situation in which I have a store Api name from salesforce (e.g. My_Column__c) in a database. This Api name is dynamically picked by the user after making a describe call againt the Object.

I have used ts-force-gen to create the object in question within my service

When I try to access the property value in the following fashion, I get undefined where there should be a value. I know this is because the generated Object as a transformed property name on it instead of the original Salesforce API name. (e.g. myColumn).

Question: Is there any way to get the value from the SObject using the Salesforce API Name without needing to regenerate the entire object? (It would cause a large refactor)

Even if it's something as nuts-and-bolts as digging into the reflect-metadata, I would love some direction on this.

This could possibly be a feature request as well, if it doesn't exist:

allowing a system to look up the generated property name by API name

Thank you for your time.

ChuckJonas commented 5 years ago

Are you saying that you want to access the object fields via the actually Salesforce API names (IOW: My_Custom_Field__c instead of the transformed js names myCustomFIeld)?

Below is the answer based on this interpretation of your issue. If this doesn't answer your question, then it would be very helpful if you could post some example code.

There are a few options:

1: Turn off the autoConvertNames (NOTE: I just fixed a bug with the generator not respecting this setting... make sure ts-force-gen at least at v2.6.5)

 {
      "apiName": "Account",
      "autoConvertNames": false
}

This will cause ts-force to use the API names.

2: Use the "FIELDS" static constant to find the field with the API name in question:

      let apiName =  'My_Custom_Field__c';
      let keyForAPIName = Object.keys(Account.FIELDS).find(key => Account.FIELDS[key].apiName === apiName) as keyof Account;
      console.log(account[keyForAPIName]);
ChuckJonas commented 5 years ago

closing due to inactivity

robbiefryers commented 4 months ago

Hi @ChuckJonas , I am trying to run this with "autoConvertNames": false and it is not respecting this.

"ts-force": "3.4.1",
"ts-force-gen": "3.4.1"

Any thoughts? Many thanks.

robbiefryers commented 4 months ago

Hi @ChuckJonas , I am trying to run this with "autoConvertNames": false and it is not respecting this.

"ts-force": "3.4.1",
"ts-force-gen": "3.4.1"

Any thoughts? Many thanks.

for what it's worth downgrading to 2.6.5 and running fixed the issue.