I am trying to write an Apex Trigger that will change a Checkbox field called Paciente_Senadis__c to TRUE if the Contact record is created within an App called Proyecto_Senadis (DeveloperName: Proyecto_Senadis, Label: Proyecto Senadis, ID: 02u4C0000008tmbQAA), but have not being able to do so.
Here is what I have tried using the AppDefinition Object:
trigger contactoSenadis on Contact (before insert) {
AppDefinition appInfo = [SELECT Id, Label, DeveloperName FROM AppDefinition LIMIT 1];
for(Contact contactoSenadis : Trigger.new){
if(appInfo.DeveloperName == 'Proyecto_Senadis'){
contactoSenadis.Paciente_Senadis__c = true;
}
}
}
And here using the UserAppInfo Object and the App Id:
trigger contactoSenadis on Contact (before insert) {
UserAppInfo appInfo = [SELECT AppDefinitionId FROM UserAppInfo LIMIT 1];
for(Contact contactoSenadis : Trigger.new){
if(appInfo.AppDefinitionId == '02u4C0000008tmbQAA'){
contactoSenadis.Paciente_Senadis__c = true;
}
}
}
After saving the trigger and creating new Contact records, the Paciente_Senadis__c checkbox is still FALSE. What am I missing here? Thanks in advance for your support!
AppDefinition
object, so this might not acually apply... But in your initialAppDefinition
query you are limiting it to 1 record. If you have more than oneAppDefinition
record, you are not guaranteed that it is the one that you want without aWHERE
clause.