Create dynamic lookup field through code: Metadata service class

 Vf Page :- Get Session ID


<apex:page >

    Start_Of_Session_Id{!$Api.Session_ID}End_Of_Session_Id

</apex:page>


---------------------------------------------------------------------------------------------------


public static String fetchUserSessionId(){

String sessionId = '';

try{

PageReference reportPage = Page.getSessionIdVfPage;

String vfContent = reportPage.getContent().toString();

Integer startP = vfContent.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(),

endP = vfContent.indexOf('End_Of_Session_Id');

sessionId = vfContent.substring(startP, endP);

} catch(exception ex){

String msg = ex.getMessage();

Integer linenum = ex.getLineNumber();

return null;

}

return sessionId;

}


@AuraEnabled 

public static String saveTemplateWithoutHeaderFooter(String selectedObjName){

System.debug('selectedObjName WithoutHeaderFooter : '+ selectedObjName);

String sessionId = fetchUserSessionId();

System.debug('Session ID: ' + sessionId);

MetadataService.MetadataPort service = new MetadataService.MetadataPort();                        

MetadataService.SessionHeader_element sessionHeader = new MetadataService.SessionHeader_element();

sessionHeader.sessionId = sessionId;

service.SessionHeader = sessionHeader;

MetadataService.CustomField customField = new MetadataService.CustomField();

customField.fullName = 'docgenx__Envelope__c.docgenx__Envelope_'+selectedObjName+'__c';

customField.label = 'Envelope Reference';

customField.type_x = 'Lookup';

customField.relationshipLabel = 'Related ' + 'Lookup';

customField.relationshipName = 'Related_' + 'Lookup';

customField.referenceTo = selectedObjName;  // Update with the object you want to reference

List<MetadataService.SaveResult> results =                       

service.createMetadata(

new MetadataService.Metadata[] { customField });

System.debug('results : '+results);

for (MetadataService.SaveResult result : results) {

if (result.errors != null) {

for (MetadataService.Error error : result.errors) {

System.debug('Error message: ' + error.message);

}

} else {

System.debug('Custom field created successfully: ' + result.fullName);

//Give to permission to the partuculler a field

MetadataService.MetadataPort service1 = new MetadataService.MetadataPort();

service1.SessionHeader = new MetadataService.SessionHeader_element();

service1.SessionHeader.sessionId = sessionId;

List<MetadataService.ProfileFieldLevelSecurity> profileFieldPermLst = new List<MetadataService.ProfileFieldLevelSecurity>();        

MetadataService.ProfileFieldLevelSecurity profileFieldPermissions = new MetadataService.ProfileFieldLevelSecurity();

profileFieldPermissions.field = 'docgenx__Envelope__c.docgenx__Envelope_'+selectedObjName+'__c';

profileFieldPermissions.editable = true;

profileFieldPermLst.add(profileFieldPermissions);

MetadataService.Profile profile = new MetadataService.Profile();

profile.fullName = 'Admin';

profile.fieldPermissions = profileFieldPermLst;

MetadataService.SaveResult[] results1 = service1.updateMetadata(

new List<MetadataService.Metadata>{ profile });

}

}

----------------------------------------------------------------------------------------

Add also Metadata service class, this is o n new post find it.




Comments

Popular posts from this blog

Custom List View Button In Salesforce

Get Record Id RecordId in LWC

PDF LWC (Link for pdf liabrary)