You need to sign in to do that
Don't have an account?
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!!!
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)
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!
Try this code:
Sorry this one:
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!