mike4aday / SwiftlySalesforce

The Swift-est way to build native mobile apps that connect to Salesforce.
MIT License
136 stars 43 forks source link

Support for Anonymous Apex? #132

Closed perbrondum closed 3 years ago

perbrondum commented 3 years ago

Trying to truncate custom objects via the API i've hit a dead stop. One workaround would be to execute the following Apex: // List contentDelete = New List(); contentDelete = [SELECT CreatedById FROM WIWO_Content__c LIMIT 10000]; delete contentDelete; // Is there some way to call Anonymous Apex inside SwiftlySalesforce?

mike4aday commented 3 years ago

Hi @perbrondum - yes, you could execute anonymous Apex by creating your own struct that conforms to Swiftly Salesforce's Service protocol (example here) and calls the Salesforce Tooling API as described here.

You could also create a custom Apex web service that both queries and deletes records, and then call that web service via Swiftly Salesforce's apex method.

Another alternative: create a custom Service that calls the Salesforce Composite API to delete multiple record IDs as shown here.

perbrondum commented 3 years ago

Cool. Thanks. We will try that.

We tried the apex REST call but got stuck on getting access to the sessionId. Is there a trick to get the sessionId from connectedApp inside swift?

Thanks again for supporting swiftlysalesforce.

Per

On Jul 28, 2021, at 6:36 PM, Michael Epstein @.***> wrote:

 Hi @perbrondum - yes, you could execute anonymous Apex by creating your own struct that conforms to Swiftly Salesforce's Service protocol (example here) and calls the Salesforce Tooling API as described here.

You could also create a custom Apex web service that both queries and deletes records, and then call that web service via Swiftly Salesforce's apex method.

Another alternative: create a custom Service that calls the Salesforce Composite API to delete multiple record IDs as shown here.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

mike4aday commented 3 years ago

@perbrondum if you use Swiftly Salesforce's apex method then you won't need the session ID. You just need to provide the method arguments (or accept the defaults if applicable).

If you do need to retrieve the access token, you could get it via ConnectedApp's getCredential method.

perbrondum commented 3 years ago

Awesome. Thank you.

Per

On Jul 29, 2021, at 9:37 PM, Michael Epstein @.***> wrote:

 @perbrondum if you use Swiftly Salesforce's apex method then you won't need the session ID. You just need to provide the method arguments (or accept the defaults if applicable).

If you do need to retrieve the access token, you could get it via ConnectedApp's getCredential method.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

perbrondum commented 3 years ago

This is very cool, except I can’t seem to get the return type right (which I assume is json with status of the job).

*private* *func* bulkDeleteContent(type: contentSegment) {

    DB.salesforce.identity().sink(receiveCompletion: { (completion) *in*

        *switch* completion {

        *case* .finished:

            *break*

        *case* *let* .failure(error) :

            print("Failed to login. Error: \(error)")

        }

    }) { identity *in*

        // Call relevant APEX

        *let* method : HTTPMethod = .delete

        *let* path = "/\(instanceName).salesforce.com/services/apexrest/

(type)"

        DB.salesforce.apex(method: method,

                           path: path,

                           parameters: [:],

                           body: *nil*,

                           headers: *nil*,

                           config:

Salesforce.RequestConfig()).sink(receiveCompletion: { (completion) in

                            *switch* completion {

                            *case* *let* .failure(error):

                                print("Failed to execute Apex. Error:

(error)")

                            *case* .finished:

                                print("Success executing Apex \(type)")

                                *break*

                            }

                           }, receiveValue: { result *in*

                            //

                           }).store(in: &*self*.subscriptions)

    }.store(in: &*self*.subscriptions)

}

Error: Unable to infer type of a closure parameter 'result' in the current context

Per B Jakobsen CEO Tap2Sales.com @.***

On Jul 29, 2021 at 9:52:51 PM, Per Jakobsen @.***> wrote:

Awesome. Thank you.

Per

On Jul 29, 2021, at 9:37 PM, Michael Epstein @.***> wrote:



@perbrondum https://github.com/perbrondum if you use Swiftly Salesforce's apex https://github.com/mike4aday/SwiftlySalesforce/blob/60c9506808b3c4e62f5f1cbfbd9c6e8061458315/Sources/SwiftlySalesforce/ConnectedApp%2BApex.swift#L29 method then you won't need the session ID. You just need to provide the method arguments (or accept the defaults if applicable).

If you do need to retrieve the access token, you could get it via ConnectedApp's getCredential method https://github.com/mike4aday/SwiftlySalesforce/blob/60c9506808b3c4e62f5f1cbfbd9c6e8061458315/Sources/SwiftlySalesforce/ConnectedApp.swift#L74 .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mike4aday/SwiftlySalesforce/issues/132#issuecomment-889406280, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALCYWDGZR2NZJXMJ6Z7M7LLT2GUVVANCNFSM5A5XE5VA .

mike4aday commented 3 years ago

Hi @perbrondum It's difficult for me to follow the unformatted code, but let me offer this:

mike4aday commented 3 years ago

@perbrondum here's an example of calling an Apex method exposed as a REST endpoint. The @RestResource annotation is RandomAccount which returns a Salesforce Account record.