0

I have a quick action button in feed and it is related to lead object. Picklist value called "External user" . While selecting the value the "Email" field is enabled. Take the email value and pass the value to controller.

Lightning Component:

<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader" access="global" controller="LeadEmailOutIntegratorSS">
  <aura:attribute name="leadId" type="String" default="NotDefined" />
  <aura:attribute name="recordId" type="String" default="NotDefined" />
  <aura:attribute name="selectedText" type="string" />
  <aura:attribute name="myText" type="string" />
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  <div class="c-container">
    <lightning:card>
      <aura:set attribute="title">
        Send Lead To External
      </aura:set>
      <lightning:layout verticalAlign="end" horizontalAlign="space">
        <lightning:layoutItem flexibility="grow" padding="around-small">
          <ui:inputSelect aura:id="ExternalSystemSelector" multiple="false" value="{!v.selectedText}">
            <ui:inputSelectOption text="ATURB" label="ATURB" value="true" />
            <ui:inputSelectOption text="Lead Central" label="Lead Central" />
            <ui:inputSelectOption text="Baldor" label="Baldor" />
            <ui:inputSelectOption text="External User" label="External User" />
          </ui:inputSelect>
        </lightning:layoutItem>
        <lightning:layoutItem flexibility="shrink" padding="around-small">
          <aura:if isTrue="{!v.selectedText == 'External User' ? 'true' : 'false'  }">
            <ui:inputText aura:id="ExternalUserEmailId" label="Email" class="field" value="{!v.myText}" />
          </aura:if>
        </lightning:layoutItem>
        <lightning:layoutItem flexibility="shrink" padding="around-small">
          <lightning:button variant="brand" label="Send" onclick="{!c.sendToExternalSystem}" />
        </lightning:layoutItem>
      </lightning:layout>
    </lightning:card>
  </div>
</aura:component>

Lightning Controller:

({
  doInit: function(cmp, event, helper) {
    var leadId = cmp.get("v.leadId");
    //alert('test ' + leadId);
    var recordId = cmp.get("v.recordId");
    //alert('recordId ' + recordId);
    if (leadId.indexOf('[') != -1) {
      leadId = leadId.substring(1, leadId.length - 1)
      //alert('test ' + leadId);
      var newleadId = leadId.split(', ');
      //alert('test ' + newleadId);
      cmp.set("v.leadId", newleadId);
    }
    if (leadId === 'NotDefined') {
      leadId = [recordId];
      cmp.set("v.leadId", leadId);
    };
    var leadId = cmp.get("v.leadId");
    //alert('newLeadId ' +leadId);
  },
  sendToExternalSystem: function(component, event, helper) {
    var leadIds = component.get("v.leadId");
    //alert('sendToExternalSystem ' + leadIds); 
    //alert('isArray ' + Array.isArray(leadId)); 
    //alert('isArray ' + Array.isArray(leadId);
    var selectCmp = component.find("ExternalSystemSelector");
    var theExternalSystemName = selectCmp.get("v.value"); ** // Passing the aura:id to here
    var ExtUser = component.find("ExternalUserEmailId");
    var theExtUserEmail = selectCmp.get("v.value"); **

    //alert('theExternalSystemName ' + theExternalSystemName);        
    var action = component.get("c.sendLeadToExternalSystem");
    //var action = component.get("c.sendLeadToTechnicalContacts")
    **
    action.setParams({
      externalSystem: theExternalSystemName,
      leadIds: leadIds,
      extUserEmailId: theExtUserEmail
    }); // here setting the parameters**
    action.setCallback(this, function(response) {


    });
    var btn = event.getSource();
    btn.set("v.disabled", true); //Disable the button   
    $A.enqueueAction(action);
  }
})

Apex Class:

@AuraEnabled
public static void sendLeadToExternalSystem(String externalSystem, List < Id > leadIds, String extUserEmailId) {
    System.debug('externalSystem :' + externalSystem);
    System.debug('leadIds :' + leadIds);
    System.debug('leadIds.size() :' + leadIds.size());
    System.debug('extUserEmailId :' + extUserEmailId);

    List < Lead > leads = [
        SELECT Id, FirstName, LastName, Name, Email, Phone, Company, City, State, PostalCode, Country, RefererUrl__c, Description__c
        FROM Lead
        WHERE ID in: leadIds Limit 50000
    ];



    Map < String, Id > technicalcontact;
    List < Contact > conList;
    conList = [select id, name, email from contact where Account.name = 'Technical Account'
        and Email != null AND(name = 'ATURB'
            or Name = 'Lead Central'
            or Name = 'Baldor'
            or Email =: extUserEmailId) limit 50000
    ];
    account accquery = new Account();
    accQuery = [Select id from account where name = 'Technical Account'
        limit 1
    ];
    System.debug('conList : ' + conList);
    technicalcontact = new Map < String, Id > ();
    Contact con;
    //technicalcontact= new Map<String,Id>();
    for (contact ct: conlist) {
        if (ct.name.equalsIgnorecase('Lead Central')) {
            technicalcontact.put('Lead Central', ct.id);
            System.debug('******************' + technicalcontact);
        }
        if (ct.name.equalsIgnorecase('ATURB')) {
            technicalcontact.put('ATURB', ct.id);
        }
        if (ct.name.equalsIgnorecase('Baldor')) {
            technicalcontact.put('Baldor', ct.id);
        }

        if (externalSystem.equals('External User') && extUserEmailId != NULL && externalSystem != NULL && extUserEmailId.equals(ct.email)) {
            technicalcontact.put('External User', ct.id);
        } else {
            con = new Contact();
            con.LastName = 'Technical Contact';
            con.Email = extUserEmailId;
            con.accountId = accquery.id;

        }
    }

    if (con != NULL) {
        insert con;
        System.debug('con:' + con);
        technicalcontact.put('External User', con.id);
    }

    System.debug('technicalcontact : ' + technicalcontact);
    Id owaId = null;
    EmailServices.Template theTemplate = EmailServices.prepareTemplate('Lead_To_External_System_Template');
    //Id externalSystemId = externalSystem2Id.get( externalSystem );
    id externalSystemId = technicalcontact.get(externalSystem);
    List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
    List < EmailMessage > emailMessages = new List < EmailMessage > ();
    List < EmailWrapper > emailWrappers = new List < EmailWrapper > ();
    List < Task > tasks = new List < Task > ();
    String unqualifiedReason = externalSystem2UnqualifiedReason.get(externalSystem);
    for (Lead theLead: leads) {
        Map < String, String > placeholderName2Value = prepareMergeFields(theLead);
        Messaging.SingleEmailMessage theEmail = theTemplate.renderSingleEmail(placeholderName2Value);
        theEmail.setTargetObjectId(externalSystemId);
        theEmail.setSaveAsActivity(false);
        emails.add(theEmail);
        theLead.Status = 'Unqualified';
        theLead.Unqualifying_Reason__c = unqualifiedReason;
        EmailMessage theEmailMessage = new EmailMessage(
            HtmlBody = theEmail.getHtmlBody(),
            Subject = theEmail.getSubject(),
            //ActivityId = '00T7E000008HF2m'
            Status = '3',
            LeadId__c = thelead.id

            //FromAddress = '[email protected]',
            //ToAddress = '[email protected]'                
        );
        //theEmailMessage.ToIds = new List<Id>{theLead.Id};
        emailMessages.add(theEmailMessage);
        //EmailMessageRelation theEmailMessageRelation = new EmailMessageRelation(
        Task theTask = new Task(
            whoId = theLead.Id,
            Description = theEmail.getHtmlBody(),
            Subject = theEmail.getSubject(),
            TaskSubtype = 'Email'
        );

        tasks.add(theTask);
        EmailWrapper theEmailWrapper = new EmailWrapper(
            theLead,
            theEmailMessage,
            theTask
        );
        emailWrappers.add(theEmailWrapper);

    }
    Messaging.sendEmail(emails);
    update leads;


    List < EmailMessageRelation > emailRelations = new List < EmailMessageRelation > ();


    for (EmailWrapper theEmailWrapper: emailWrappers) {
        /* EmailMessageRelation theEmailRelation = new EmailMessageRelation(
             EmailMessageId = theEmailWrapper.emailMember.Id,
             RelationId = theEmailWrapper.leadMember.Id,
             RelationType = 'fromAddress'
         );*/
        theEmailWrapper.taskMember.whoId = theEmailWrapper.leadMember.Id;
        //theEmailWrapper.taskMember.whatId = theEmailWrapper.emailMember.Id;
        // emailRelations.add( theEmailRelation );
    }
    insert tasks;
    insert emailMessages;
    for (EmailWrapper theEmailWrapper: emailWrappers) {
        EmailMessageRelation theEmailRelationTo = new EmailMessageRelation(
            EmailMessageId = theEmailWrapper.emailMember.Id,
            RelationId = externalSystemId,
            RelationType = 'toAddress'
        );
        //theEmailWrapper.emailMember.RelatedToId = theEmailWrapper.taskMember.Id;
        emailRelations.add(theEmailRelationTo);
        EmailMessageRelation theEmailRelationFrom = new EmailMessageRelation(
            EmailMessageId = theEmailWrapper.emailMember.Id,
            RelationId = UserInfo.getUserId(),
            RelationType = 'fromAddress'
        );
        emailRelations.add(theEmailRelationFrom);
    }
    insert emailRelations;
}

1 Answer 1

0

You have problem in your sendToExternalSystem method. The way you are trying to get selected value from "ExternalSystemSelector" picklist and email entered in "" input text is incorrect.

Some correction needs in sendToExternalSystem in Component's controller.js

    sendToExternalSystem: function(component, event, helper) {
        var leadIds = component.get("v.leadId");
        // We have selectedText attribute binded to picklist
        // So user selected value will be stored in it. 
        // Here get selected value from selectedText attribute.
        var theExternalSystemName = component.get("v.selectedText");

        // Similar to picklist we have myText attribute binded to input text
        // So user selected value will be stored in it. 
        // Here get selected value from myText attribute.
        var theExtUserEmail = component.get("v.myText");

        console.log('1. theExternalSystemName ' + theExternalSystemName);
        console.log('2. theExtUserEmail ' + theExtUserEmail);   
        var action = component.get("c.sendLeadToExternalSystem");
        action.setParams({
            externalSystem: theExternalSystemName,
            leadIds: leadIds,
            extUserEmailId: theExtUserEmail
        }); 
        action.setCallback(this, function(response) {
        });
        var btn = event.getSource();
        btn.set("v.disabled", true); //Disable the button   
        $A.enqueueAction(action);
    }

As User selected and entered values are getting stored in aura:attribute, you can easily get them in your controller.js as:

var theExternalSystemName = component.get("v.selectedText");
var theExtUserEmail = component.get("v.myText");

I hope this helps.

4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .