You need to sign in to do that
Don't have an account?
virk
retrieving data from old map and new map
Can someone help me in writing a single query to retrieve old data and new data for the oparticular condition in if satatement?
List Account accs = Trigger.newMap.keySet() ;
for (Account a : accs)
if (a.RecordTypeID == '012D00000002qdE' && a.R_seau_int_gr__c == true && a.Etat_relation_GEO__c == 'Déploiement' && (oldmap == null || oldMap.get(a.Id).Etat_relation_GEO__c != 'Déploiement'))
{
hasHierarchy = true;
accIds.add(a.id);
while (hasHierarchy)
{
List<Account> res = [SELECT id, Etat_relation_GEO__c FROM Account WHERE Etat_relation_GEO__c != 'Terminé' AND parentId IN :accIds];
accIds.clear();
if (res == null || res.size() == 0)
hasHierarchy = false;
for (Account aa : res)
{
accIds.add(aa.id);
toUpdate.add(aa);
}
}
}
List Account accs = Trigger.newMap.keySet() ;
for (Account a : accs)
if (a.RecordTypeID == '012D00000002qdE' && a.R_seau_int_gr__c == true && a.Etat_relation_GEO__c == 'Déploiement' && (oldmap == null || oldMap.get(a.Id).Etat_relation_GEO__c != 'Déploiement'))
{
hasHierarchy = true;
accIds.add(a.id);
while (hasHierarchy)
{
List<Account> res = [SELECT id, Etat_relation_GEO__c FROM Account WHERE Etat_relation_GEO__c != 'Terminé' AND parentId IN :accIds];
accIds.clear();
if (res == null || res.size() == 0)
hasHierarchy = false;
for (Account aa : res)
{
accIds.add(aa.id);
toUpdate.add(aa);
}
}
}
All Answers
<pre>
trigger xxx on Account ( before insert, before update )
{
RecordType accountRT =
[ SELECT Id FROM RecordType
WHERE sObjectType = 'Account' AND DeveloperName = 'Record_Type_Name'];
Set<Id> accountIds = new Set<Id>();
for ( Account account : Trigger.new )
{
Account oldAccount = Trigger.isUpdate ? Trigger.oldMap.get( account.Id ) : null;
if ( account.RecordTypeId == accountRT.Id
&& account.R_seau_int_gr__c
&& account.Etat_relation_GEO__c == 'Déploiement'
&& ( oldAccount == null
|| oldAccount.Etat_relation_GEO__c != 'Déploiement'
)
)
{
accountIds.add( account.Id );
}
}
List<Account> accountsToUpdate = new List<Account>();
while ( ! accountIds.isEmpty() )
{
Map<Id,Account> parentAccounts = new Map<Id,Account>
( [ SELECT Id, Etat_relation_GEO__c
FROM Account
WHERE ( Etat_relation_GEO__c != 'Terminé'
AND parentId IN :accountIds
)
]
);
accountsToUpdate.addAll( parentAccounts.values() );
accountIds = parentAccounts.keySet();
}
update accountsToUpdate;
}
</pre>