You need to sign in to do that
Don't have an account?
sfdc03
I need some urgent help on this
I want to send email to Account'contact from opportunity if opportunitycontactrole are not exist using visualforce page along with checkboxes
Example: suppose GreenwattOpportunity has Burlington Account. Burlington Account has SteveContact1, SteveContact2, SteveContact3 then email should be sent to SteveContact1, SteveContact2(If user select only 2 contacts)..
Thank you!!
Example: suppose GreenwattOpportunity has Burlington Account. Burlington Account has SteveContact1, SteveContact2, SteveContact3 then email should be sent to SteveContact1, SteveContact2(If user select only 2 contacts)..
Thank you!!
I've gone through your requirement and you can refer the below solution:
Aura component:
<aura:component controller="EmailNotificationToAccountHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInitController}"/>
<aura:attribute name="OpportunityWrapper" type="EmailNotificationToAccountHandler.WrapOpportunity[]"/>
<table class="slds-table slds-table--bordered slds-table--cell-buffer" role="grid">
<tr>
<td> <b>Notification</b></td>
<td> <b>Opportunity Name</b></td>
</tr>
<aura:iteration items="{!v.OpportunityWrapper}" var="opp">
<tr>
<td> <ui:inputCheckBox value="{!opp.SelectedOpportunity}"/></td>
<td> {!opp.OpportunityName}</td>
</tr>
</aura:iteration>
</table>
<br></br>
<lightning:Button label="Send Notification" variant="Brand" onclick="{!c.SendNotifyController}"/>
</aura:component>
JSController:
({
doInitController : function(component, event, helper)
{
helper.doInitHelper(component,event,helper);
}
,
SendNotifyController : function(component, event, helper)
{
helper.SendNotifyHelper(component,event,helper);
}
})
JSHelper:
({
doInitHelper : function(c,e,v)
{
var action=c.get("c.returnAllOpportunity");
action.setCallback(this,function(res)
{
if(res.getState()=='SUCCESS')
{
console.log('return data>>>'+JSON.stringify(res.getReturnValue()));
c.set("v.OpportunityWrapper",res.getReturnValue());
}
});
$A.enqueueAction(action);
}
,
SendNotifyHelper :function(c,e,v)
{
var getwrappedList=c.get("v.OpportunityWrapper");
var action=c.get("c.SendNotificationToContact");
action.setParams({
allwrappedOpportunity:getwrappedList
});
action.setCallback(this,function(res)
{
if(res.getState()==='SUCCESS')
{
alert('Notification sended');
}
});
$A.enqueueAction(action);
}
})
Apex controller:
public class EmailNotificationToAccountHandler
{
@AuraEnabled
public static List<WrapOpportunity> returnAllOpportunity()
{
List<WrapOpportunity> allWrapOpportunity=new List<WrapOpportunity>();
List<Opportunity> allOpportunity=new List<Opportunity>([select id,Name,AccountId from Opportunity where AccountId!=null]);
if(allOpportunity.size()>0)
{
for(Opportunity opp:allOpportunity)
{
WrapOpportunity wr=new WrapOpportunity();
wr.OpportunityName=opp.Name;
wr.OpportunityAccountId=opp.AccountId;
wr.OpportunityId=opp.Id;
allWrapOpportunity.add(wr);
}
}
return allWrapOpportunity;
}
@AuraEnabled
public static void SendNotificationToContact(List<WrapOpportunity> allwrappedOpportunity)
{
try
{
System.debug('allwrappedOpportunity>>>'+allwrappedOpportunity);
Map<Id,String> OpportunityVSAccountMap=new Map<Id,String>();
Set<Id> AccountIds=new Set<Id>();
for(WrapOpportunity wr:allwrappedOpportunity)
{
if(wr.SelectedOpportunity==true)
{
OpportunityVSAccountMap.put(wr.OpportunityAccountId,wr.OpportunityName);
AccountIds.add(wr.OpportunityAccountId);
}
}
List<Contact> allcontacts=new List<Contact>([select id,Name,Email,AccountId from Contact where AccountID IN:AccountIds]);
Map<Id,List<Contact>> AccountVSContactMap=new Map<Id,List<Contact>>();
for(Contact con:allcontacts)
{
if(AccountVSContactMap.containsKey(con.AccountId))
{
AccountVSContactMap.get(con.AccountId).add(con);
}
else
{
AccountVSContactMap.put(con.AccountId,new List<Contact>());
AccountVSContactMap.get(con.AccountId).add(con);
}
}
System.debug('AccountVSContactMap>>>'+AccountVSContactMap);
List<Messaging.SingleEmailMessage> allemailList=new List<Messaging.SingleEmailMessage>();
EmailTemplate template=[select id,Name,Body,Subject from EmailTemplate where Id='00X4J000001BHNN' limit 1 ];
EmailTemplate emTemp=template.clone(false);
for(Id accId:AccountVSContactMap.keySet())
{
for(Contact con:AccountVSContactMap.get(accId))
{
String emailBody=emTemp.Body;
List<String> contEmail=new List<String>();
contEmail.add(con.Email);
String s='';
if(emailBody.contains('{!Contact.Email}' ))
{
s+=emailBody.replace('{!Contact.Email}', con.Name);
}
String s1=s;
if(emailBody.contains( '{!Opportunity.Name}' ))
{
s1+= emailBody.replace(' {!Opportunity.Name}', OpportunityVSAccountMap.get(accId));
}
Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
mail.setTemplateId(template.Id);
mail.setSubject(template.Subject);
mail.setToAddresses(contEmail);
mail.setHtmlBody(s1);
System.debug('mail>>>'+mail);
allemailList.add(mail);
}
}
if(allemailList.size()>0)
{
Messaging.SendEmailResult[] emailResult=Messaging.sendEmail(allemailList);
for(Messaging.SendEmailResult msg:emailResult)
{
System.debug('Status>>>'+msg.isSuccess());
}
}
}
catch(Exception e)
{
System.debug('Error in--->'+e.getLineNumber()+' and Error is--->'+e.getMessage());
}
}
public class WrapOpportunity
{
@AuraEnabled
public String OpportunityName{get;set;}
@AuraEnabled
public String OpportunityId{get;set;}
@AuraEnabled
public String OpportunityAccountId{get;set;}
@AuraEnabled
public Boolean SelectedOpportunity{get;set;}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com