Hi am creating apex trigger using MetaData Service class but i am getting this Exception -

Line: 13432, Column: 1 System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_TYPE: This type of object is not available for this organization faultcode=sf:INVALID_TYPE faultactor=

Sample code -

MetadataService.MetadataPort service2 = new MetadataService.MetadataPort();  
service2.SessionHeader = new MetadataService.SessionHeader_element();
service2.SessionHeader.sessionId = UserInfo.getSessionId();     
MetadataService.ApexTrigger objTrigger = new MetadataService.ApexTrigger();
objTrigger.type = 'ApexTrigger';
objTrigger.apiVersion = 45.0;
objTrigger.status = 'Active';
objTrigger.fullName = 'TestTrigger';
string str= 'Trigger TestTrigger on Account (After Insert,After update){}';
//apexPage.label = 'TestPage';
objTrigger.content = EncodingUtil.base64Encode(Blob.valueOf(str));
List<MetadataService.SaveResult> results = service2.createMetadata(new MetadataService.Metadata[] { objTrigger });      
  • Hi @Swati, Welcome to SFSE, to get better reponses; please include the code causing the problem.
    – Raul
    May 14, 2019 at 9:47
  • Also, why do you need to do this?
    – Keith C
    May 14, 2019 at 10:04
  • Hi Swati, make sure you read some of the guidelines of this forum salesforce.stackexchange.com/help/how-to-ask . Describe what you have analyzed, what you have done and what you want to achieve. We need more context May 14, 2019 at 10:09
  • hi, did anyone figure this out? Jul 18, 2023 at 7:31

1 Answer 1


once I also get the same error and the solution for this error is that you have to pass the session id using @ future annotation in another method Examle

public static void deleteBooks(id ids){
public static void getthesessionid(id ids){
    String sessionId = UserInfo.getSessionId();
     MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId =sessionId;

then your logic..... note : here ids contaains id of record which i want to delete

  • hi Ravinder, what is the underlying problem of this, do you know? I ask this because the error and the suggested solution do not seem related. Jul 18, 2023 at 7:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .