4

I am using "ToolingAPI" to create a trigger dynamically , but I received this error:

"INVALID_ARGUMENT_TYPE : Missing field [TableEnumOrId] on trigger create"

I am using this code:

ToolingAPI toolingAPI = new ToolingAPI();
toolingAPI.ApexTrigger newTrigger = new ToolingAPI.ApexTrigger();
newTrigger.Name = 'HelloWorld77';
newTrigger.Body = 'trigger HelloWorld77 on Account (before insert) {}';

and when I added this line :

newTrigger.tableEnumOrId = 'Account';

I received an another error:

System.StringException: Invalid id: Account

The tooling api wrapper used here is provided on Github.

6
  • "System.StringException: Invalid id: Account" indicates that you are trying to assign a string to an ID typed property. The ToolingAPI clearly indicates "Public Id tableEnumOrId;", line 566.
    – TSmith
    Mar 12 '17 at 16:14
  • Yes , but according to the error Missing field , I have to add tableEnumOrId for example in my case I am creating a trigger for Account so I have to add it into the variable **tableEnumOrId ** , how can i do it ? thank
    – Raphym
    Mar 12 '17 at 16:18
  • Maybe you should log an issue in GitHub.
    – Raul
    Mar 12 '17 at 17:12
  • I also did it but it is a very interesting question , for example we can use ToolingApi to create a class and it is very easy , but there is not a simple example about the creation of a trigger from apex code.
    – Raphym
    Mar 12 '17 at 17:41
  • @Raphym yes please post this as an issue on the GitHub repo, github.com/afawcett/apex-toolingapi/blob/apex-toolingapi-rest/… thanks Mar 12 '17 at 19:05
4

The problem is likely related the Tooling API wrapper you are using.

From the source for ApexTrigger:

public class ApexTrigger extends SObject_x implements ISerialize {
    //...
    public Id         tableEnumOrId;
    //...
}

Note how that is an Id rather than string type. It needs to be a string so in can specify either the TableEnum or the Id.

From the Docs on TableEnumOrId:

Type Restricted picklist
Description The enum (for example, Account) or ID of the object this field is on.


I was able to create a new ApexTrigger specifying just the Name, Body, and TableEnumOrId fields.

Creating an Apex Trigger directly with the Tooling API

1
0

Yes, you are right , thank you very much !

In conclusion the solution is:

In the class **ToolingAPI.cls change two things :**

  1. public Id tableEnumOrId; to public String tableEnumOrId;
  2. jsonGen.writeIdField('tableEnumOrId', tableEnumOrId); to jsonGen.writeStringField('tableEnumOrId', tableEnumOrId);

And of course you can know use it here :

    ToolingAPI toolingAPI = new ToolingAPI();
    toolingAPI.ApexTrigger newTrigger = new ToolingAPI.ApexTrigger();
    newTrigger.Name = 'HelloWorld77';
    newTrigger.Body = 'trigger HelloWorld77 on Account (before insert) {}';
    newTrigger.tableEnumOrId='Account';
    toolingAPI.createSObject(newTrigger);

I advise you to use the Andrew API from here: https://github.com/afawcett/apex-toolingapi/tree/apex-toolingapi-rest

Thank you all for your cooperation

Raphael

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.