You need to sign in to do that
Don't have an account?
Harshwardhan Singh Karki
How can we insert records of custom metadata type using tooling api(Using http callout) from regular user?
I have A custom metadata type named "ImportNotes_Setting__mdt" with fields , DeveloperName , MasterLabel as standard and custom fields are , ImportId__c,Notes__c, IsDelete__c ,so i need to insert records from user for this custom metadata type using tooling api (Http callout) .
Code executed to insert record of custom metadata type which is returning error {"message":"entity type cannot be inserted: ImportNotes Setting","errorCode":"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY","fields":[]} , So i need the solution regarding this if it is possible to do so with this approach
global class TestRecordInsertUsingToolingApi {
@future (callout = true)
public static void connectOrg(){
Http h = new Http();
Httprequest req = new Httprequest();
string json ='{"Masterlabel":"Import Note","ImportID__c":"Imp36862577","Notes__c":"Test ToolinApi"}';
String autho ='Bearer '+ UserInfo.getUserId();
req.setHeader('Authorization', autho);
string endpointRemainingUrl = '/services/data/v56.0/sobjects/ImportNotes_Setting__mdt';
string endpoint = Url.getSalesforceBaseUrl().toExternalForm() + endpointRemainingUrl;
req.setEndpoint(endpoint);
req.setHeader('Content-Type','application/json; charset=utf-8');
req.setBody(json);
req.setMethod('POST');
Httpresponse res = h.send(req);
system.debug(res.getBody()+ res.getStatusCode());
if(res.getStatusCode() == 400){
system.debug(res.getBody());
Map<String, Object> m = (Map<String, Object>)System.JSON.deserializeUntyped(res.getBody());
system.debug(m);
}
}
Code executed to insert record of custom metadata type which is returning error {"message":"entity type cannot be inserted: ImportNotes Setting","errorCode":"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY","fields":[]} , So i need the solution regarding this if it is possible to do so with this approach
global class TestRecordInsertUsingToolingApi {
@future (callout = true)
public static void connectOrg(){
Http h = new Http();
Httprequest req = new Httprequest();
string json ='{"Masterlabel":"Import Note","ImportID__c":"Imp36862577","Notes__c":"Test ToolinApi"}';
String autho ='Bearer '+ UserInfo.getUserId();
req.setHeader('Authorization', autho);
string endpointRemainingUrl = '/services/data/v56.0/sobjects/ImportNotes_Setting__mdt';
string endpoint = Url.getSalesforceBaseUrl().toExternalForm() + endpointRemainingUrl;
req.setEndpoint(endpoint);
req.setHeader('Content-Type','application/json; charset=utf-8');
req.setBody(json);
req.setMethod('POST');
Httpresponse res = h.send(req);
system.debug(res.getBody()+ res.getStatusCode());
if(res.getStatusCode() == 400){
system.debug(res.getBody());
Map<String, Object> m = (Map<String, Object>)System.JSON.deserializeUntyped(res.getBody());
system.debug(m);
}
}
Instead, you can insert records into a custom metadata type using the Metadata API. Here is an example of how you can modify your code to use the Metadata API:
Note that you will also need to add the MetadataService class from the Metadata API to your org. You can download it from the Metadata API Developer Guide.
If you find this answer helpful, Please mark it as the best answer.