11package batch
22
33import (
4- "bytes"
54 "encoding/json"
65 "fmt"
76 force "github.com/ForceCLI/force/lib"
8- "github.com/benhoyt/goawk/interp"
97 anon "github.com/octoberswimmer/batchforce/apex"
108 "strings"
119)
@@ -20,36 +18,50 @@ func (e *Execution) getApexContext() (map[string]any, error) {
2018 for _ , v := range apexVars {
2119 lines = append (lines , fmt .Sprintf (`b_f_c_t_x.put('%s', %s);` , v , v ))
2220 }
23- lines = append (lines , `System.debug(JSON.serialize(b_f_c_t_x));` )
21+ // Throw an exception with the JSON data to retrieve it via Tooling API
22+ lines = append (lines , `throw new System.HandledException('BATCHFORCE_CONTEXT:' + JSON.serialize(b_f_c_t_x));` )
2423 apex = apex + strings .Join (lines , "\n " )
2524
26- // retrieve underlying *force.Force for Partner API calls
25+ // retrieve underlying *force.Force
2726 fs , ok := e .session ().(* force.Force )
2827 if ! ok {
2928 return nil , fmt .Errorf ("session is not a *force.Force" )
3029 }
31- debugLog , err := fs .Partner .ExecuteAnonymous (apex )
30+
31+ // Use Tooling API for ExecuteAnonymous (works in WASM)
32+ result , err := fs .ExecuteAnonymousTooling (apex )
3233 if err != nil {
33- return nil , err
34+ return nil , fmt . Errorf ( "failed to execute anonymous apex: %v" , err )
3435 }
35- val , err := varFromDebugLog (debugLog )
36- if err != nil {
37- return nil , err
36+
37+ // Check if we got our expected exception with the context data
38+ if ! result .Success && result .ExceptionMessage != "" {
39+ // Look for our marker in the exception message (may have exception type prefix)
40+ marker := "BATCHFORCE_CONTEXT:"
41+ idx := strings .Index (result .ExceptionMessage , marker )
42+ if idx != - 1 {
43+ // Extract the JSON from the exception message
44+ jsonStr := result .ExceptionMessage [idx + len (marker ):]
45+ var n map [string ]any
46+ err = json .Unmarshal ([]byte (jsonStr ), & n )
47+ if err != nil {
48+ return nil , fmt .Errorf ("failed to parse context JSON: %v" , err )
49+ }
50+ return n , nil
51+ }
52+ // This is a real exception, not our context data
53+ return nil , fmt .Errorf ("execution error: %s" , result .ExceptionMessage )
3854 }
39- var n map [ string ] any
40- err = json . Unmarshal ( val , & n )
41- if err != nil {
42- return nil , err
55+
56+ // If no exception was thrown (no variables to extract), return empty map
57+ if result . Success {
58+ return map [ string ] any {}, nil
4359 }
44- return n , nil
45- }
4660
47- func varFromDebugLog (log string ) ([]byte , error ) {
48- input := strings .NewReader (log )
49- output := new (bytes.Buffer )
50- err := interp .Exec (`$2~/USER_DEBUG/ { var = $5 } END { print var }` , "|" , input , output )
51- if err != nil {
52- return nil , err
61+ // Handle compilation errors
62+ if result .CompileProblem != "" {
63+ return nil , fmt .Errorf ("compilation error: %s" , result .CompileProblem )
5364 }
54- return output .Bytes (), nil
65+
66+ return nil , fmt .Errorf ("execution failed" )
5567}
0 commit comments