1

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();
    if(objectMap.get(objectName)!=null){
        objectExist=true;
    }
    return objectExist;
}

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

e.g

If(FetchMetadata.checkObjectExistance('AccountTeamMember') == true){
AccountTeamMember testAccTeamMember = new AccountTeamMember();
            testAccTeamMember.AccountId=//SomeAcountId;
            testAccTeamMember.UserId=//someId;
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 ?

1 Answer 1

4

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
       sObj.put('UserId','SomeId');
       insert sObj ;
  }
0

You must log in to answer this question.

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