-1

Question

Is it possible to update or create NamedCredential from Apex ?

some background from internet search

From my investigation found that it's possible to update NamedCredential through MetdataAPi and then Found this Possible to update NamedCredential from Apex? , a time saver :).

However, after successful installation of apex-mdt ,i have added new apex class to create/update Namedcredential (this class will internally MetadataService Class).

when i tried to call my newly created apex class(MetadataWrapper), i was getting an error "Method does not exist or incorrect signature"

Issue Raised at : https://github.com/financialforcedev/apex-mdapi/issues/231

Not sure what is missing, Please Help!

EDIT 1:

I was using wrong logic for createService() and createMetadata(). clear steps mention in answer.

  • Actually method "createMetadata(MetadataService.Metadata[] metadata)" takes an array as parameter and you are just passing an instance of MetadataService.Metadata – Ayub Dec 20 '18 at 7:16
  • I tried using createMetadata(new List<MetadataService.Metadata> { input }) this as well – Krishna Kanth Juluru Dec 20 '18 at 9:54
  • Please add your error message verbatim and also your code. Without either of these things, your question is off topic. – Adrian Larson Dec 20 '18 at 15:11
  • @AdrianLarson sorry for inconvenience,able to resolve it with knowledge from your question. Did you try implementing this using Tooling API as well ? – Krishna Kanth Juluru Dec 20 '18 at 15:23
  • 2
    I will remove my downvote if you include the relevant code in your question and actually mention the error message verbatim. – Adrian Larson Dec 20 '18 at 15:31
2

I was using wrong function signature for createService() , hence issue.

STEP1: Deploy https://github.com/financialforcedev/apex-mdapi

STEP2: Create New Class after deploying apex-mdt: Code From

public class MetadataWrapper {
    public static MetadataService.MetadataPort createService()
    {
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;
    }
    public static List<MetadataService.SaveResult> createMetadata(MetadataService.Metadata input)
    {
    return createService().createMetadata(new List<MetadataService.Metadata> { input });
    }
    public static List<MetadataService.SaveResult> updateMetadata(MetadataService.Metadata input)
    {
    return createService().updateMetadata(new List<MetadataService.Metadata> { input });
    }
public static List<MetadataService.DeleteResult> deleteMetadata(String type, String fullName)
    {
    return createService().deleteMetadata(type, new List<String> { fullName });
    }
}

STEP 3: Example Call from Anonymous:

    MetadataService.NamedCredential credential = new MetadataService.NamedCredential();
    credential.fullName = 'Demo_Credentia';
    credential.label = 'Demo Crential';
    credential.endpoint = 'https://www.DMO2.c';
    credential.principalType = 'NamedUser';
    credential.protocol = 'NoAuthentication';
system.debug(MetadataWrapper.createMetadata(credential));
    system.debug(MetadataWrapper.updateMetadata(credential));
| improve this answer | |

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