function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
virkvirk 

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);
                    }
                }
            }
 
Best Answer chosen by virk
venkat-Dvenkat-D
Why are you querying the inforamtion, In update context you have that information in Trigger.OldMap and Trigger.NewMap




 
To check some conditions you can do following

For(Account acc : Trigger.NewMap.values()){
if()//condition
{
OldValue = Trigger.oldmap.get(acc.Id).fieldname;
}


}

 

All Answers

venkat-Dvenkat-D
Why are you querying the inforamtion, In update context you have that information in Trigger.OldMap and Trigger.NewMap




 
To check some conditions you can do following

For(Account acc : Trigger.NewMap.values()){
if()//condition
{
OldValue = Trigger.oldmap.get(acc.Id).fieldname;
}


}

 
This was selected as the best answer
Glyn Anderson 3Glyn Anderson 3
virk,  Please consider the following code:

<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>
virkvirk
Thanks Glyn and Venkat for your reply.
Glyn Anderson 3Glyn Anderson 3
Virk, Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved the problam another way, please post your solution for everyone's benefit.  Thanks!