+ Start a Discussion
Richa_LearningRicha_Learning 

Trigger not Updating fields

Hi,

I am trying to insert/update the value when the status change to Reviewed it should put the date time in a_date__C  and

when status changed to Approved it should put the date time in r_date__C. 

But it's not giving me an error neither its' updating the field.

 

 

trigger updatefield on imp__c (after update) {
Map<String,imp__c > recordsimp = new Map<String,imp__c >();
List<account> accountToUpdate = new List<account>();

List<imp__c> impToInsert = new List<imp__c>();

 

if (Trigger.isInsert){
        for (imp__c impRecord : trigger.new){
            if(imprecord.Status__c == 'Reviewed' || imprecord.Status__c == 'Approved'){
                imp__c impObj = new imp__c (a_Date__c = datetime.Now(),r_date__c=datetime.now());
                insert  impObj;                  
            }
              
        }
        insert impToInsert;
    }

 

 

Else{

 

 


for(imp__c impRecord : trigger.new){

Imp__c oldImp = trigger.oldMap.get(impRecord.Id);

// only add IMP record Ids to the Map if the IMP record Status__c field changes

if(impRecord.Status__c != oldImp.Status__c)
recordsimp.put(impRecord.rec_ID__c,impRecord);
}

 

// only execute below if the Map is not empty (any Status__c changes occured above)

if(!recordsimp.isEmpty(){
accountToUpdate = [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()];
for(account accounts: accountToUpdate){
accounts.status__c = recordsimp.get(accounts.account_num__c).Status__c;
}
if(accountToUpdate.size() > 0){
update accountToUpdate;
}
}

}

}

Ishan K SharmaIshan K Sharma

trigger updatefield on imp__c (after update,after insert) in 1st line.

 

you are missing after insert.

Richa_LearningRicha_Learning

I added after insert but still not updating the field.

SidharthSidharth

try this out:

 

trigger updatefield on imp__c (after insert, after update) {
		
	List<imp__c> impToInsert = new List<imp__c>();
	Map<Id,String > recordsimp = new Map<Id,String >();
	List<account> accountToUpdate = new List<account>();
	
	if(trigger.isInsert){
		
		for(imp__c impRecord: trigger.new){
			if(imprecord.Status__c == 'Reviewed' || imprecord.Status__c == 'Approved'){
				imp__c impObj = new imp__c (a_Date__c = datetime.Now(),r_date__c=datetime.now());
				impToInsert.add(impObj);
			}
		}
		if(impToInsert.size() > 0)	insert impToInsert;
		
	}
	
	else{
		
		for(imp__c impRecord: trigger.new){			
			Imp__c oldImp = trigger.oldMap.get(impRecord.Id);
			if(impRecord.Status__c != oldImp.Status__c){
				recordsimp.put(impRecord.account_num__c,impRecord.Status__c);
			}
		}
		
		if(!recordsimp.size() > 0){			
			accountToUpdate = [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()];			
			if(accountToUpdate.size() > 0){
				for(account accounts: accountToUpdate){
					accounts.status__c = recordsimp.get(accounts.id);
				}
				update accountToUpdate;
			}
		}
	}	

}

 

Samba GSamba G

Source Code:

 

if(!recordsimp.size() > 0){			
			accountToUpdate = [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()];			
			if(accountToUpdate.size() > 0){
				for(account accounts: accountToUpdate){
					accounts.status__c = recordsimp.get(accounts.id);
				}
				update accountToUpdate;
			}
		}

 You can change it:

List<Account> accounts = new List<Account>();
for(account acc: [select id,Status__c,account_num__c from account where account_num__c IN: recordsimp.keyset()])
{
     acc.status__c = recordsimp.get(accounts.id);
     accounts.add(acc);
}
update accounts;

 

Thanks,

Samba