• DivyanshD Singh
  • NEWBIE
  • 5 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 7
    Replies
I am trying to add new field items to a Layout section.Everytime a new section is getting created even if i am assigning the same Section.
If i deploy the same section to the Layout, it gives an error 'Field appears more than once' as it tries to deploy the same fields again.
If i make a new column it creates a new section automatically.
How can i add new LayoutColumn to same section through Metadata API.
Below is my code :
public static void addFieldToLayout(String[] lstOfDataPointsToInsert){
        list<String> listTrimmedFields = new list<String>();
        MetadataService.LayoutColumn newLayoutColumn = new MetadataService.LayoutColumn();
        MetadataService.LayoutColumn newLayoutColumn1 = new MetadataService.LayoutColumn();
        list<MetadataService.LayoutColumn> mdcolumn1 = new List<MetadataService.LayoutColumn>();
        list<MetadataService.LayoutColumn> mdcolumn2 = new List<MetadataService.LayoutColumn>();
        
        list<MetadataService.LayoutColumn> listColumns = new list<MetadataService.LayoutColumn>();
        list<MetadataService.LayoutColumn> listColumns1 = new list<MetadataService.LayoutColumn>();
        List<MetadataService.LayoutItem> lstLayoutItems = new List<MetadataService.LayoutItem>();
        List<MetadataService.LayoutItem> lstLayoutItems1 = new List<MetadataService.LayoutItem>();
        List<MetadataService.LayoutItem> lstLayoutItems2 = new List<MetadataService.LayoutItem>();
        List<MetadataService.LayoutItem> lstLayoutItems3 = new List<MetadataService.LayoutItem>();
        
        
        list<String> listString = new list<String>();
        list<String> listString1 = new list<String>();
        
        MetadataService.MetadataPort service  = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        // Read the Layout
        MetadataService.Layout layout = (MetadataService.Layout) service.readMetadata('Layout', new String[] { 'IBP__Ifbyphone_Call__c-IBP__Ifbyphone_Call_Layout' }).getRecords()[0]; 
        list<MetadataService.Layout> listLayout = new list<MetadataService.Layout>();
        
        for(Integer i=0; i<lstOfDataPointsToInsert.size(); i++){
            if(lstOfDataPointsToInsert.size() > 5){
                listTrimmedFields.add(lstOfDataPointsToInsert.remove(i));
            }
        }
        system.debug('actaul list-->' + lstOfDataPointsToInsert + 'Trimmmmed List -->' + listTrimmedFields);
        
        
        // Add Layout section, layout, item and field
        if(layout.layoutSections==null )
            layout.layoutSections = new List<MetadataService.LayoutSection>();
        MetadataService.LayoutSection newLayoutSection = new MetadataService.LayoutSection();
       
        if(sections() == null || sections().label != 'Keyword fields'){
            system.debug('inside thissssssss');
            newLayoutSection.customLabel = true;
            newLayoutSection.detailHeading = true;
            newLayoutSection.editHeading = true;
            newLayoutSection.label = 'Keyword fields';	
            newLayoutSection.style = 'TwoColumnsLeftToRight';
            system.debug('listTrimmedFields>>>1322'+listTrimmedFields);
            system.debug('lstOfDataPointsToInsert>>>13212'+lstOfDataPointsToInsert);
            for(String objField : lstOfDataPointsToInsert){
                system.debug('ObjectfieldAPIName for 1st 5>>>'+ObjectfieldAPIName(objField, false));
                MetadataService.LayoutItem newLayoutItem = new MetadataService.LayoutItem();
                newLayoutItem.field = ObjectfieldAPIName(objField, false);
                //newLayoutItem.behavior = 'Edit';
                lstLayoutItems.add(newLayoutItem);
                system.debug('in 1st columnnnnnn---->' +lstOfDataPointsToInsert);
            }
            
            for(String objField1 : listTrimmedFields){
                system.debug('ObjectfieldAPIName for last 5>>>'+ObjectfieldAPIName(objField1, false));
                MetadataService.LayoutItem newLayoutItem1 = new MetadataService.LayoutItem();
                newLayoutItem1.field = ObjectfieldAPIName(objField1, false);
                //newLayoutItem.behavior = 'Edit';
                lstLayoutItems1.add(newLayoutItem1);
                system.debug('in 2nd list column'+listTrimmedFields);
                //newLayoutColumn.layoutItems = new List<MetadataService.LayoutItem> { newLayoutItem };
                
            }
            system.debug('check for 1st column ' + lstLayoutItems + 'Check for 2nd column ' + lstLayoutItems1);
            
            
            newLayoutColumn.layoutItems = lstLayoutItems;
            newLayoutColumn1.layoutItems = lstLayoutItems1;
            
            system.debug('check newLayoutColumn' + newLayoutColumn + 'Check newLayoutColumn1 List' + newLayoutColumn1);
            system.debug('newLayoutSection>>>>qw'+newLayoutSection);
            newLayoutSection.layoutColumns = new List<MetadataService.LayoutColumn> { newLayoutColumn,newLayoutColumn1 };
            system.debug('newLayoutSection.layoutColumns>>>>qw'+newLayoutSection.layoutColumns);
            layout.layoutSections.add(newLayoutSection);
        }
        
        else{system.debug('in elseeeee---->');
             system.debug('listTrimmedFields>>>12'+listTrimmedFields);
             system.debug('lstOfDataPointsToInsert>>>12'+lstOfDataPointsToInsert);
            for(String objField : lstOfDataPointsToInsert){
                system.debug('ObjectfieldAPIName for 3rd 5>>>'+ObjectfieldAPIName(objField, false));
                MetadataService.LayoutItem newLayoutItem = new MetadataService.LayoutItem();
                newLayoutItem.field = ObjectfieldAPIName(objField, false);
                //newLayoutItem.behavior = 'Edit';
                lstLayoutItems2.add(newLayoutItem);
                system.debug('in 1st columnnnnnn---->' +lstOfDataPointsToInsert);
            }
            
            for(String objField1 : listTrimmedFields){
                system.debug('ObjectfieldAPIName for 4th 5>>>'+ObjectfieldAPIName(objField1, false));
                MetadataService.LayoutItem newLayoutItem1 = new MetadataService.LayoutItem();
                newLayoutItem1.field = ObjectfieldAPIName(objField1, false);
                //newLayoutItem.behavior = 'Edit';
                lstLayoutItems3.add(newLayoutItem1);
                system.debug('in 2nd list column'+listTrimmedFields);
                //newLayoutColumn.layoutItems = new List<MetadataService.LayoutItem> { newLayoutItem };
                
            }
            system.debug('check for 1st column ' + lstLayoutItems2 + 'Check for 2nd column ' + lstLayoutItems3);
            
            MetadataService.LayoutColumn newLayoutColumn12 = new MetadataService.LayoutColumn();
            MetadataService.LayoutColumn newLayoutColumn13 = new MetadataService.LayoutColumn();
            newLayoutColumn12.layoutItems = lstLayoutItems2;
            newLayoutColumn13.layoutItems = lstLayoutItems3;
            
            system.debug('newLayoutColumn12ncolumn ' + newLayoutColumn12 + 'newLayoutColumn13 2nd column ' + newLayoutColumn13); 
             
            system.debug('check listtsssssss' + listString + 'Check second List' + listString1);
            MetadataService.layoutSection tragetSections = sections();
            system.debug('tragetSections>>>'+tragetSections.layoutColumns);
            tragetSections.layoutColumns = new List<MetadataService.LayoutColumn> { newLayoutColumn12,newLayoutColumn13 };
                //tragetSections.layoutColumns.add(newLayoutColumn12);
                //tragetSections.layoutColumns.add(newLayoutColumn13);
             
            system.debug('newLayoutSection>>>>!2'+tragetSections);    
            layout.layoutSections.add(tragetSections);
             system.debug('hereeee');
        }
        
        handleSaveResults(
            service.updateMetadata(
                new metadataService.Metadata[]{layout} )[0]);
        
    }
    
    public static MetadataService.layoutSection sections(){
        MetadataService.MetadataPort service  = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        MetadataService.Layout layouts = (MetadataService.Layout) service.readMetadata('Layout', new String[] { 'IBP__Ifbyphone_Call__c-IBP__Ifbyphone_Call_Layout' }).getRecords()[0];
        MetadataService.LayoutSection mdSection ;
        for(MetadataService.LayoutSection mdlayout:layouts.layoutSections){
            if(mdlayout.label == 'Keyword Fields'){
                mdSection = mdlayout;
                break;
            }
        }
        return mdSection;
    }

 
Here is my code for a separate section and a column.I need to 2 two columns as i have many field to show on that section of layout.
Can anyone help how to divide a section in two columns by apex.I am using Metadata API currently.
if(layout.layoutSections==null)
            layout.layoutSections = new List<MetadataService.LayoutSection>();
        MetadataService.LayoutSection newLayoutSection = new MetadataService.LayoutSection();
        newLayoutSection.customLabel = true;
        newLayoutSection.detailHeading = true;
        newLayoutSection.editHeading = true;
        newLayoutSection.label = 'Keyword fields';
        newLayoutSection.style = 'TwoColumnsLeftToRight';}

 
When i deploy Layout with 1 field everything works fine whereas when i deploy fields as a list , not even a single field is showing in the Layout. Below is the code i am using for field creation, then assign profile and atlast add it to the Layout:
public class DynamicDatapoints {
    public static void dynamicCreation(list<String> lstOfDataPointsToInsert){
        
		//create instance and session id for metadata service
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
		
		//create instance for metadata create custom field class
       
        List<MetadataService.CustomField> lstAllCustoField = new List<MetadataService.CustomField>();
        for(integer i=0; i<lstOfDataPointsToInsert.size(); i++){
             MetadataService.CustomField customField = new MetadataService.CustomField();
			customField.fullName = lstOfDataPointsToInsert[i];
			customField.label = lstOfDataPointsToInsert[i];
			customField.type_x = 'Text';
			customField.length = 42;
            lstAllCustoField.add(customField);
            List<MetadataService.SaveResult> results =
            service.createMetadata(new List<MetadataService.Metadata> { customField });
			system.debug('results>>'+results);
        }          
		
		//create instance for metadata profile class
        MetadataService.Profile admin = new MetadataService.Profile();
        for(String str: lstOfDataPointsToInsert){
			admin.fullName = 'Admin';
			admin.custom = false;
			MetadataService.ProfileFieldLevelSecurity fieldSec = new MetadataService.ProfileFieldLevelSecurity();
			fieldSec.field=str;
			fieldSec.editable=true;
			admin.fieldPermissions  = new MetadataService.ProfileFieldLevelSecurity[] {fieldSec} ;
			List<MetadataService.SaveResult> result = service.updateMetadata( new MetadataService.Metadata[] { admin });
			system.debug('Success FLS' + result);
		}     

        //Adding created fields to a Layout
         Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
        List<Metadata.Metadata> layoutList  = Metadata.Operations.retrieve(Metadata.MetadataType.Layout, new List<String> {'Account-Account Layout'});
        if(layoutList != null && !layoutList.isEmpty()){
			Metadata.Layout layoutMetadata = (Metadata.Layout) layoutList.get(0);
			list<Metadata.Layout> listlayoutMetadata = new list<Metadata.Layout>();
			
			Metadata.LayoutSection sec = new Metadata.LayoutSection();
			sec.customLabel = true;
			sec.detailHeading = true;
			sec.editHeading = true;
			sec.label = 'Keyword fields';
			sec.style = metadata.LayoutSectionStyle.OneColumn;
			system.debug('inside Section');
			Metadata.LayoutColumn LayoutColumnsItems = new Metadata.LayoutColumn();
            list<Metadata.LayoutColumn> listLayoutColumnsItems = new list<Metadata.LayoutColumn>();
            
			Metadata.LayoutItem layoutField =new Metadata.LayoutItem();	 
	   //     list<Metadata.LayoutItem> listlayoutField =new list<Metadata.LayoutItem>();
			Metadata.DeployContainer dc;
			list<Metadata.DeployContainer> listdc =new list<Metadata.DeployContainer>();  
			String field;
			
			for(String st: lstOfDataPointsToInsert){
				
				field = st.removeStart('Account.'); 
				//layoutField = new Metadata.LayoutItem();				
				layoutField.field = field;
				layoutField.behavior = metadata.UiBehavior.Edit;
				system.debug('inside layout section' + field);
				//LayoutColumnsItems = new Metadata.LayoutColumn();
				LayoutColumnsItems.layoutItems.add(layoutField);
                
                system.debug('check Items' + LayoutColumnsItems);
                sec.layoutColumns.add(LayoutColumnsItems);
                layoutMetadata.layoutSections.add(sec);
                //listLayoutColumnsItems.add(LayoutColumnsItems);
				
				
				listlayoutMetadata.add(layoutMetadata);
			}
			
            system.debug('check sectionss -->' + listlayoutMetadata);
            for(Metadata.Layout objLaoutMeta : listlayoutMetadata){
                dc = new Metadata.DeployContainer();
                dc.addMetadata(objLaoutMeta);
                system.debug('dc>>'+dc);
                Id jobid2 = Metadata.Operations.enqueueDeployment(dc,null); 
                system.debug('Metadata Deployment'+jobid2);
            }

 

I have a requirement where i have to integrate SFDC to AWS and thinking to use Platform events for the same.

My question is how can pass the data(records) to Platform events so that it can process it and send it to DynamoDB(AWS).

Like in trigger we pass trigger.new to our http class and it is therafter processed. How can i do this using Platform events so that our events publishes that particular data which i have to send it to DynamoDB.

Also, I have to process that in bulk.(more than 1k recs at a time.)

Any help would be appreciated.

I have a requirement where i have to push JSON in a specific format where Key should be Field Label and Value should be values in Tigger.new.  
Problem here is the Trigger.new or Trigger.newMap alwayz returns sObject let say Key-API name and their corresponding values.
How do i get Label Name instead of API name corresponding to Trigger.new values ??
Any help would be  appreciated
obj A mandatory lookup to obj B,obj C mandatory lookup to obj B , obj A lookup to obj C (not mandatory)...Provided this i have the following requirement.....if A created check whether obj C exists for obj B or not...if not create one and also link obj A to C.
there is a date time field in obj A and obj C...We need to check whether their d/f is more thasn 12 hrs or not..if yes create record in obj C if no simply link Obj A to obj C (link the records)....given obj 1- photo , obj 2 - asset , obj 3- asset visit
my trigger goes like this but not working as expected..

 
trigger createAV on AUDIT__c (after insert) {
​​​​​​​  
    list<ASSET_VISIT__c> assetVisit=new list<ASSET_VISIT__c>();
    list<AUDIT__c> listphoto=new list<AUDIT__c>();
    set<id> setAid=new set<id>();
    set<id> setAVid=new set<id>();
    for(AUDIT__c ph:Trigger.new){
        setAid.add(ph.ASSETS__c);
    //  setAVid.add(ph.ASSET_VISIT__c);
    }
    list<ASSET_VISIT__c> listAsset=[select id,name,FIRST_PHOTO_TAKEN__c from ASSET_VISIT__c where id=:setAid];
    
    //   for(integer i=0; i<=; i++){
    for(ASSETS__c a:[select id,name,PLANT__c from ASSETS__c where id=:setAid]){
        for(AUDIT__c p:Trigger.new){
            if(listasset.size() != 0 ){
                ASSET_VISIT__c ab=new ASSET_VISIT__c();
                if(p.PHOTOGRAPH_TAKEN__c !=null &&  p.PHOTOGRAPH_TAKEN__c.addhours(12)  > p.ASSET_VISIT__r.FIRST_PHOTO_TAKEN__c){
                    //            ASSET_VISIT__c ab=new ASSET_VISIT__c();
                    ab.ASSETS__c=p.ASSETS__c;
                    ab.PLANT__c=p.ASSETS__r.PLANT__c;
                    assetvisit.add(ab);
                    insert assetvisit;
                    update assetvisit;
                }
                else{
                    p.ASSET_VISIT__c=ab.Id;
                    p.ASSETS__c=a.Id;
                    listphoto.add(p);
                    update listphoto;
                }
            }
            else {
                ASSET_VISIT__c av=new ASSET_VISIT__c();
       //       ASSETS__c a=new ASSETS__c(); 
                av.ASSETS__c=a.id;
                av.PLANT__c=a.PLANT__c;
        //      p.ASSET_VISIT__c=av.Id;
                av.Id=p.ASSET_VISIT__c;
                assetVisit.add(av);
            }
            insert assetVisit;         
            update assetVisit;   
        }
    }
}

 
Here is my code for a separate section and a column.I need to 2 two columns as i have many field to show on that section of layout.
Can anyone help how to divide a section in two columns by apex.I am using Metadata API currently.
if(layout.layoutSections==null)
            layout.layoutSections = new List<MetadataService.LayoutSection>();
        MetadataService.LayoutSection newLayoutSection = new MetadataService.LayoutSection();
        newLayoutSection.customLabel = true;
        newLayoutSection.detailHeading = true;
        newLayoutSection.editHeading = true;
        newLayoutSection.label = 'Keyword fields';
        newLayoutSection.style = 'TwoColumnsLeftToRight';}

 
Here is my code for a separate section and a column.I need to 2 two columns as i have many field to show on that section of layout.
Can anyone help how to divide a section in two columns by apex.I am using Metadata API currently.
if(layout.layoutSections==null)
            layout.layoutSections = new List<MetadataService.LayoutSection>();
        MetadataService.LayoutSection newLayoutSection = new MetadataService.LayoutSection();
        newLayoutSection.customLabel = true;
        newLayoutSection.detailHeading = true;
        newLayoutSection.editHeading = true;
        newLayoutSection.label = 'Keyword fields';
        newLayoutSection.style = 'TwoColumnsLeftToRight';}

 

public class emp {
integer A;

 
    public  emp (string Name,  String Qualification,Integer Age, Date DOB ,Integer Address, Integer pSalary){
        for (A = 0; A>= 1000 ; A++ ) {
            system.debug(A);
        String D = 'Department IT' ;
        system.debug(D);
        }
            
    }
            
    

            public void ComSal() {
                    Integer vSalary = 15000;
                    abc(vSalary);
                    System.assertEquals(vSalary , 1);
                    system.debug(vSalary);
    }
               public void abc (Integer vSalary){
                    Integer C;
                    integer pSalary;
                    double hCommission;
                    C = vSalary;
                    system.debug(c);
                    if (pSalary >=15000){
                             hCommission = pSalary * .25 ;
                        system.debug(hCommission); }
        
    }

   /* public void date today(){
        date.today();
        return date ;
    }*/
}

 

------

emp em = new emp('Harsh', ' BSc Micro' , 21, 1997-05-26,1234,16000);
em.abc(16000);

hi, I m a bigginer, I want to insert set of record in object. how set of sobject is converted into list ?  How to update those records?
Same for Map.
  Please help me with it.
I've completed the challenge, it has 100% coverage. I've checked all the method names. The URL is valid. I've used Work Bench and curl to test and even tested with multiple Accounts with and without contacts.

I know on other challenges, punctionation was important.  What about the defination of the return? Are there expected names?

I built a class to hold Account ID & Name along with a List of Contact names and IDs. Is this my issue?  Anyone else have a challenge with this challenge?


Any help or hints will be appreciated.

Here are snippets of my code:

@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {

....

global class APIAccount {
        public ID Id;
        public String Name;
        List<APIContact> Contacts;

...

@HttpGet
    global static APIAccount getAccount() {
        RestRequest request = RestContext.request;
...