+ Start a Discussion
EvertonSzekeresEvertonSzekeres 

Update Account field based on insert Asset

I'm trying to update a field in "Account" if insert one "Asset" called AR.

 

I tried to create the trigger in "Asset", but it doesn't work.

 

Any help?

 

Best Answer chosen by Admin (Salesforce Developers) 
Tim BarsottiTim Barsotti

You are using the wrong ID. Swap the asset ID with the account id. See the code below:

 

trigger AtualizarCampoAR_Case on Asset (after insert, after update) {

    Asset[] ass = Trigger.new;
    
    Set<ID> accIds = new Set<ID>();
    for (Asset a:ass) {
      if (a.name.substring(0,2) == 'AR') {
          accIds.add(a.Account);
}
}
List<Account> accList = new List<Account>(); for (Account acc : [select ID, recordtypeid, Antecipado__c from Account where Id in :accIds for update]) { if (acc.recordtypeid == '012U00000000vYx'){ acc.Antecipado__c = 'AR';
accList.add(acc); }
}
update accList;
}

All Answers

Tim BarsottiTim Barsotti

Please post your code and the behavior your are experiencing.

EvertonSzekeresEvertonSzekeres
trigger AtualizarCampoAR_Case on Asset (after insert, after update) {

    Asset[] ass = Trigger.new;
    
    Set<ID> assIds = new Set<ID>();
    for (Asset a:ass)
      if (a.name.substring(0,2) == 'AR')
      assIds.add(a.Id);
    
for (Account acc : [select recordtypeid, Antecipado__c from Account where Assetid in :assIds for update])
    if (acc.recordtypeid == '012U00000000vYx'){
        acc.Antecipado__c = 'AR';
update acc;
} }

"No such column 'Assetid' on entity 'Account'"

 

It's because doesn't have this field (assetid) in account.

But the trigger is pretty simple and I'm just posting here for you understand what I'm trying to do.

Tim BarsottiTim Barsotti

You are using the wrong ID. Swap the asset ID with the account id. See the code below:

 

trigger AtualizarCampoAR_Case on Asset (after insert, after update) {

    Asset[] ass = Trigger.new;
    
    Set<ID> accIds = new Set<ID>();
    for (Asset a:ass) {
      if (a.name.substring(0,2) == 'AR') {
          accIds.add(a.Account);
}
}
List<Account> accList = new List<Account>(); for (Account acc : [select ID, recordtypeid, Antecipado__c from Account where Id in :accIds for update]) { if (acc.recordtypeid == '012U00000000vYx'){ acc.Antecipado__c = 'AR';
accList.add(acc); }
}
update accList;
}
This was selected as the best answer