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
EvertonSzekeresEvertonSzekeres 

Bring all assets

Hi,

I am trying send and email that will bring me all the assets from account.
Set <id> ids = new Set <id> ();
    for (Account acc: trigger.new) {
            ids.add(acc.id);
        }
        
    for (Asset ast: [SELECT Id, Name, Quantity FROM Asset WHERE Quantity>0 AND accountid IN: ids]){

    for(Account acc : trigger.new){
        if(acc.separador__c == 'pesquisa'){        
	        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	        String[] toAddresses = new String[]{};   
	            toAddresses.add(acc.PersonEmail);
	            toAddresses.add(acc.Email_alternativo__c);
	        mail.setToAddresses(toAddresses);
            
            mail.setreplyto('naoresponda@cp7.com.br');
            
	        mail.setSubject('Pesquisa');
            mail.setHtmlBody('Olá, '+acc.FirstName+''+acc.LastName+'<p>'+ast.Name+'</p>');

In this trigger I only get the last asset created and not all the list.

Thanks !
~Onkar~Onkar
Hi

Its not complete code. Can you paste your complete code

Take the referance from this code. May be It will help you to solve your issue.


Map<Id,Account> accountMap = new Map<Id,Map>([Select Id,NAme,(Select Id,Name from Assets where Quantity>0) from Account and Id in : Trigger.New]);

for(Account Ac : Trigger.New){
  if(acc.separador__c == 'pesquisa'){
   String AssetName;
   if(accountMap.get(Ac.Id).Assets.size() > 0){
    for(Asset obj : accountMap.get(Ac.Id).Assets){
     AssetName = AssetName + '-' obj.Name;
    }
    //
     Email Code....................

    //
   }
  }
}

 
~Thanks
Onkar
EvertonSzekeresEvertonSzekeres
Thanks !
It's almost perfect !

But in my email I am receiving a null asset:

Olá, Xxx Xxx

null, "AMPLIAÇÃO", "FOTO 20X30CM", "DVD", "AR - SUPER VIP"


Why I am receiving null?

My code:
Map<Id,Account> accountMap = new Map<Id,Account>([Select Id, Name, (Select Id,Name from Assets where Quantity>0) from Account Where Id in : Trigger.New]);

for(Account Ac : Trigger.New){
  if(ac.separador__c == 'pesquisa'){
   String AssetName;
   if(accountMap.get(Ac.Id).Assets.size() > 0){
    for(Asset obj : accountMap.get(Ac.Id).Assets){
     AssetName = AssetName + ', ' + +'"'+obj.Name+'"';
    }  
    if(AssetName != null){ 
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{};   
                toAddresses.add(ac.PersonEmail);
            mail.setToAddresses(toAddresses);
            
            mail.setreplyto('naoresponda@cp7.com.br');
            
            mail.setSubject('Pesquisa');
            mail.setHtmlBody('Olá, '+ac.FirstName+' '+ac.LastName+'<p>'+AssetName+'</p>');
                
            Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
            }
        }
        }
    }
}


~Onkar~Onkar
Check null before build asset string like
if(accountMap.get(Ac.Id).Assets.size() > 0){
for(Asset obj : accountMap.get(Ac.Id).Assets){
if(obj.Name != null){
     AssetName = AssetName + ', ' + +'"'+obj.Name+'"';
}
  }
EvertonSzekeresEvertonSzekeres
Didn't work.
~Onkar~Onkar
Is this you complete code. If not please add ur code here.. let me look.
EvertonSzekeresEvertonSzekeres
It is my complete code, but I will post here again:

trigger Email_Pesquisa_Mercado_account on Account (after update) {

Map<Id,Account> accountMap = new Map<Id,Account>([Select Id, Name, (Select Id,Name from Assets where Quantity>0) from Account Where Id in : Trigger.New]);

for(Account Ac : Trigger.New){
  if(ac.separador__c == 'pesquisa'){
   String AssetName;
   if(accountMap.get(Ac.Id).Assets.size() > 0){
    for(Asset obj : accountMap.get(Ac.Id).Assets){
        if(obj.name != null){
     AssetName = AssetName + ', ' + '"'+obj.Name+'"';
        }
    }
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{};   
                toAddresses.add(ac.PersonEmail);
            mail.setToAddresses(toAddresses);
            
            mail.setreplyto('naoresponda@cp7.com.br');
            
            mail.setSubject('Pesquisa');
            mail.setHtmlBody('Olá, '+ac.FirstName+' '+ac.LastName+'<p>'+AssetName+'</p>');
                
            Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
        }
    }
}
}