+ Start a Discussion
K SrikanthK Srikanth 

help with test class for APT META DATA call

Hi,
Can anyone help with test class for below methods.

@RemoteAction
public static String Createcfield(String ObjectName,List<id> recordids) {
Map<String, Schema.SObjectField> fieldMap;
fieldMap = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
Blob blobKey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
String uniq = key.substring(0,4);
String cField='PVListviewFiled'+uniq+'__c';
HTTP httpService = new HTTP();
HTTPRequest request = new HTTPRequest();
pvMetadataService.MetadataPort metadataservice = new pvMetadataService.MetadataPort();
metadataservice.SessionHeader = new pvMetadataService.SessionHeader_element();
metadataservice.SessionHeader.sessionId = UserInfo.getSessionId();
PVMetadataService.CustomField customField = new PVMetadataService.CustomField();
customField.fullName = ObjectName+'.'+ cField;
customField.label = cField.replace('__c','');
customField.defaultvalue = 'false';
customField.type_x = 'Checkbox';
List<PVMetadataService.Metadata> fields = new List<PVMetadataService.Metadata>();
fields.add(CustomField);
list<pvMetadataService.SaveResult> results =MetadataService.createMetadata(fields);
string strObjectName = ObjectName;
string strField2API= cField;
if(results[0].success) {
String strRequestBody1 = '<?xml version="1.0" encoding="UTF-8"?>'
+ '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
+ '<soapenv:Header>'
+ '<ns1:SessionHeader soapenv:mustUnderstand="0" xmlns:ns1="http://soap.sforce.com/2006/04/metadata">'
+ '<ns1:sessionId>' + UserInfo.getSessionId() + '</ns1:sessionId>'
+ '</ns1:SessionHeader>'
+ '</soapenv:Header>'
+ '<soapenv:Body>'
+ '<upsertMetadata xmlns="http://soap.sforce.com/2006/04/metadata">'
+ '<metadata xsi:type="ns2:Profile" xmlns:ns2="http://soap.sforce.com/2006/04/metadata">'
+ '<fullName>Admin</fullName>'
+ '<custom>false</custom>'
+ '<fieldPermissions>'
+ '<field>'+strObjectName + '.' +strField2API+' </field>'
+ '<editable>true</editable>'
+ '</fieldPermissions>'
+ '</metadata>'
+ '</upsertMetadata>'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>';
HTTP httpService1 = new HTTP();
HTTPRequest request1 = new HTTPRequest();
request1.setMethod('POST');
request1.setHeader('Content-Type', 'text/xml');
request1.setHeader('SOAPAction', 'upsertMetadata');
request1.setBody(strRequestBody1);
request1.setCompressed(false);
request1.setEndpoint(ENDPOINT_URL);
if(!Test.isRunningTest()) {
HTTPResponse response1 = httpService.send(request1);
System.debug('Response ::: '+response1.getBody());
}}
updaterecord(ObjectName,recordids,cField);
return cField;
}
@future(Callout=true)
public static void updaterecord(String ObjectName,List<id> recordids,string cField){
List<id> Updateids = recordids;
List<sObject> recordsToUpdate = new List<sObject>();
string objname=ObjectName;
for(id str:Updateids){
Sobject record;
sObject sRecord = Schema.getGlobalDescribe().get(objname).newSObject() ;
sRecord.put('Id',Str);
sRecord.put(cField, True);
recordsToUpdate.add(sRecord);
}
update recordsToUpdate;
}
@RemoteAction
public static List<String> Createlistviewfield(String ObjectName, String CFieldName, String ListViewName) {
string failedmesg='';
string finalurl='';
pvMetadataService.MetadataPort metadataservice = new pvMetadataService.MetadataPort();
metadataservice.SessionHeader = new pvMetadataService.SessionHeader_element();
metadataservice.SessionHeader.sessionId = UserInfo.getSessionId();
pvMetadataService.ListViewFilter filter = new pvMetadataService.ListViewFilter();
filter.field=CFieldName;
filter.operation = 'equals' ;
filter.value='1';
system.debug(ObjectName+'.'+ListViewName);
pvMetadataService.ListView listView = new pvMetadataService.ListView();
listView.fullName = ObjectName+'.'+ListViewName.replace(' ','_');
listView.label = ListViewName;
listView.filterScope = 'Everything';
listView.filters= new list<pvMetadataService.ListViewFilter>{filter};
list<pvMetadataService.Metadata> fieldsabc = new list<pvMetadataService.Metadata>();
system.debug(listView.fullName);
fieldsabc.add(listView);
List<pvMetadataService.SaveResult> resultss = MetadataService.createMetadata(fieldsabc);
list<PVMetadataService.Error> ermsg = new list<PVMetadataService.Error>();
if(!resultss[0].success){
ermsg=resultss[0].errors;
failedmesg+=ermsg[0].message;
}
Schema.SObjectType objType = Schema.getGlobalDescribe().get(objectName);
Schema.DescribeSObjectResult r = objType.getDescribe();
String keyPrefix = r.getKeyPrefix();
if(resultss[0].success){
listview displaylistview= [SELECT Id,Name,Sobjecttype FROM listview WHERE Name=:ListViewName AND Sobjecttype =: ObjectName ];
finalurl+=URL.getSalesforceBaseUrl().toExternalForm() +'/'+keyPrefix+'?fcf='+String.valueOf(displaylistview.id).substring(0, 15);
}
List<String> resultmsg= new List<String>{failedmesg,String.valueOf(resultss[0].success),finalurl};
return resultmsg;
}
AnudeepAnudeep (Salesforce Developers) 
From what I understand you need to test the Metadata service cls

I suggest reviewing the test class sample code here. It should help

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!