0

I am trying to pass two Sobject names to a method and match using Schema the Field API Names common in both of them.

Like I have Object1 having fields A__c, B__c,X__c. Object2 having fields A__c, B__c,Y__c.

Here A__c and B__c are the matching fields. Now I need to put these field names in a Set which will have values 'A__c' , 'B__c'.

Can someone please help me to find out how to compare between two Schema.DescribefieldResult?

Apex code:

public static String Compare(String str, String str1)
    {
              String sObjectName = str;
              String sObjectName1 = str1;
        System.debug('=========  '+ sObjectName);
        List<Schema.SObjectField> sss=new list<Schema.SObjectField>();

        if(sObjectName != null && sObjectName != '' )

        Schema.SObjectType gd = Schema.getGlobalDescribe().get(sObjectName); 
        Schema.DescribeSobjectResult a11=gd.getDescribe();
        Map<String, Schema.SObjectField> M = a11.fields.getMap();        
        for(Schema.SObjectField s1:m.values()){                            
            Schema.DescribeFieldResult f = M.get(s1.getDescribe().getName()).getDescribe();

        Schema.SObjectType gd1 = Schema.getGlobalDescribe().get(sObjectName1); 
        Schema.DescribeSobjectResult a=gd1.getDescribe();
        Map<String, Schema.SObjectField> M1 = a.fields.getMap();        
        for(Schema.SObjectField s2:m1.values()){                            
            Schema.DescribeFieldResult f2 = M2.get(s2.getDescribe().getName()).getDescribe();
        }
    }
4
  • 1
    Maybe you can elaborate on what doesn't seem to work, or what the current result is, and what you were expecting. Do you get any errors in your code? Please update your post accordingly.
    – glls
    Commented Jul 20, 2017 at 11:44
  • f has field names of Object1 and f2 has field names of Object2. Now I need to compare these API Names that match. Please find the updated question here. Commented Jul 20, 2017 at 12:02
  • I did not ask for an explanation of what you want others to resolve for you. Explain your code, what part of it is not working, why do you think it should work? Have you tried debugging?
    – glls
    Commented Jul 20, 2017 at 12:10
  • I am not able to understand how to iterate a ' for loop ' for Schema.DescribeFieldResult. Can you please help me? Didn't get an example for this in google search. Commented Jul 20, 2017 at 12:12

1 Answer 1

1

Try this:

public static String Compare(String str, String str1){

    String sObjectName = str;
    String sObjectName1 = str1;
    List<String> fieldsS1 = new list<String>();
    List<String> fieldsS2 = new list<String>();
    for(Schema.SObjectField s1:Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap().values()){      
        //get Fields of First object                     
        fieldsS1.add(s1.getDescribe().getName());

  }
    for(Schema.SObjectField s1:Schema.getGlobalDescribe().get(sObjectName1).getDescribe().fields.getMap().values()){    
        //get Fields of Second object                         
        fieldsS2.add(s1.getDescribe().getName());

  }

    Set<String> uniquefieldsS1 = new Set<String>(fieldsS1);
    List<String> uniqueFields = new List<String>();
    for(String str: fieldsS1){
        if(uniquefieldsS2.contains(str)){
            uniqueFields.add(str);
        } 
    }
}

uniqueFields is the Set which contains the Unique fields.

1
  • Thanks, I didn't know getdescribe.getname could be added as List element. Thanks! Commented Jul 20, 2017 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .