I am writing a code where I have to create an AccountTeamMember Record if the AccountTeamMember object exists on Org. To check the existence of AccountTeamMember object I have used the following code :

public static Boolean checkObjectExistance (String objectName) {
    Boolean objectExist = false ;
    Map<String, Schema.SObjectType> objectMap = Schema.getGlobalDescribe();
    return objectExist;

Now using its Return value and IF condition, I have to create AccountTeamMember Record :


If(FetchMetadata.checkObjectExistance('AccountTeamMember') == true){
AccountTeamMember testAccTeamMember = new AccountTeamMember();
Insert testAccTeamMember;

But,I can not save the code because the AccountTeamMember currently does not exist in ORG. Is there any way which will allow me to save this code so that it can work on any org which does not have AccountTeamMember object and also on the org which has AccountTeamMember ?


Use the Dynamic DML for this , the sample code would look like below

  if(FetchMetadata.checkObjectExistance('AccountTeamMember') == true){
        Schema.SObjectType t = Schema.getGlobalDescribe().get('AccountTeamMember');
       SObject sObj = t.newSObject();
       sObj.put('AccountId','SomeId');//SomeId is valid AccountID
       insert sObj ;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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