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 

Email trigger getting null

Hi,

I am asking this in everywhere and no one can help me.

I have this trigger that will bring all assets in an email.

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});
        }
    }
}
}

I get all the assets, but I received the email like this:

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

Why I am receiving a null asset?
What should I do?

Thanks !

Best Answer chosen by EvertonSzekeres
Ramu_SFDCRamu_SFDC
The problem is at the below line

AssetName = AssetName + ', ' + '"'+obj.Name+'"';

Initially there will be no value set for AssetName hence it shows the value as null + the current asset details. To get rid of this, you would need to add one more if statement something like the below


for(Asset obj : accountMap.get(Ac.Id).Assets){
        if(obj.name != null){
if(AssetName==null){
AssetName = obj.Name;

}
else{
     AssetName = AssetName + ', ' + '"'+obj.Name+'"';
}
        }
........
.....
}


Mark this as the best answer if it resolved your issue.

All Answers

Ramu_SFDCRamu_SFDC
The problem is at the below line

AssetName = AssetName + ', ' + '"'+obj.Name+'"';

Initially there will be no value set for AssetName hence it shows the value as null + the current asset details. To get rid of this, you would need to add one more if statement something like the below


for(Asset obj : accountMap.get(Ac.Id).Assets){
        if(obj.name != null){
if(AssetName==null){
AssetName = obj.Name;

}
else{
     AssetName = AssetName + ', ' + '"'+obj.Name+'"';
}
        }
........
.....
}


Mark this as the best answer if it resolved your issue.

This was selected as the best answer
ShashForceShashForce
Hi,

Because of this line:

String AssetName;

The value of AssetName is initially 'null'. So, the result of this line:

AssetName = AssetName + ', ' + '"'+obj.Name+'"';

will overwrite AssetName like this: null,"AMPLIAÇÃO"

To avoid null, please try something like this:

if(obj.name != null){
    if(AssetName==null) { AssetName = obj.Name; }
    else { AssetName = AssetName + ', ' + '"'+obj.Name+'"';}
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank