+ Start a Discussion
sfdclearnsfdclearn 

Trigger to update the Account status(custom field) based on Asset status(Standard field)

Hi,

 

   I am using the following trigger to Update the Account status to 'customer'  when 

AccountStatus__c == 'PROSPECT'  and Asset.Status!='Returned'.

 

trigger AccountUpdate on Account (before insert, before update)
{

if(trigger.isAfter && trigger.isUpdate)
{
List<Account> lstAccount=new List<Account>();
List<Asset> lstAsset = [SELECT Status FROM Asset WHERE AccountID IN :trigger.new];
for(Account A : lstAccount)
{
for(Asset Asobj : lstAsset)
{
if(A.AccountStatus__c == 'PROSPECT' && Asobj.Status!='Returned')
{
A.AccountStatus__c= 'Customer';
Update A;
}


}

}
}

}

The Account status changes to customer only when i edit and save the Asset record.

For ex., if the account status='Prospect' && Asset.status='Purchased' this is not updated when the account is created or saved.

It works only when the asset it edited.

 

Please advise!!!

SidharthSidharth

It will not enter your trigger code if(trigger.isAfter && trigger.isUpdate)

because the trigger defination is: (before insert, before update)

 

change the trigger to after insert, after update, OR change if condition to if(trigger.isBefore && trigger.isUpdate)

sfdclearnsfdclearn

I have changed it to if(trigger.isbefore && trigger.isUpdate) 

but still it updates the account only if I edit , save the Asset record.

 

Thanks!

SidharthSidharth

Try this code:

 

trigger test1 on Account (before insert, before update) {
	
	map<String,Account> accsIds = new map<String,Account>();
	list<Asset> assetRecords = new list<Asset>();
	list<Account> accsToUpdate = new list<Account>();
	
	if(trigger.isBefore && trigger.isUpdate){
		
		for(Account accs  : trigger.new){
			if(accs.AccountStatus__c == 'PROSPECT'){
				accsIds.put(accs.id,accs);
			}
		}
		
		if(accsIds.size()>0){
			assetRecords = [SELECT Status,AccountID FROM Asset WHERE AccountID IN :accsIds.keySet() and Status!='Returned'];
		}
		
		if(assetRecords.size()>0){
			for(Asset asset: assetRecords){
				if(accsIds.containsKey(asset.AccountID)){
					accsIds.get(asset.AccountID).AccountStatus__c = 'Customer';
					accsToUpdate.add(accsIds.get(asset.AccountID));
				}
			}
		}
		
	}

}

 

SidharthSidharth

Sorry this one:

 

trigger test1 on Account (before insert, before update) {
	
	map<String,Account> accsIds = new map<String,Account>();
	list<Asset> assetRecords = new list<Asset>();
	
	if(trigger.isBefore && trigger.isUpdate){
		
		for(Account accs  : trigger.new){
			if(accs.Status__c == 'PROSPECT'){
				accsIds.put(accs.id,accs);
			}
		}
		
		if(accsIds.size()>0){
			assetRecords = [SELECT Status,AccountID FROM Asset WHERE AccountID IN :accsIds.keySet() and Status!='Returned'];
		}
		
		if(assetRecords.size()>0){
			for(Asset asset: assetRecords){
				if(accsIds.containsKey(asset.AccountID)){
					accsIds.get(asset.AccountID).Status__c = 'Customer';
				}
			}
		}
		
	}
}

 

sfdclearnsfdclearn

This code worked . I just wrote the following test class for this code and it has only 25% code coverage. could you please advise?

 

@isTest
private class AssetToAccountTest{

static testMethod void myunittest() {
Test.startTest();

Account Accountobj = new Account(Name = 'Test Account',CustomerType__c='Academic',AccountStatus__c='PROSPECT');
insert Accountobj;

Asset Assetobj=new Asset(Name='Test Asset',Status='Purchased',AccountId=Accountobj.ID);
insert Assetobj;



Test.stopTest();
}
}

 

Thanks!