Create and populate contact details for unknown inbound email IDs

If an inbound message is received from an unknown email ID, the contact details won't be populated in the inbound record or service request.

To create a new contact during inbound email processing, you can use the following steps. The first script shows the initial trigger notification, the second script can be used to populate the newly created contact as the primary contact in the service request.
  1. In Application Composer, select Objects > Standard Objects > Inbound Message > Server Scripts.
  2. Create custom fields on the Inbound Message object to store the contact id and sender email address using the following:
    • Field Name: ContactId Type: Number.

    • Field Name: SenderEmail Type: Text.

  3. Click Triggers > Object Triggers > Add a New Trigger .
  4. Select Before Insert in Database for trigger and provide a trigger name.
    See the following groovy script for Trigger definition.:
    try{
      def email = nvl(Sender,'noreply@xxx.com');
        
      def pProfileVO = newView('PersonProfile')
      pProfileVO.appendViewCriteria("PrimaryEmailAddress = '${email}'")
      pProfileVO.executeQuery()
        
      //Check if the contact already exists
      if (!(pProfileVO.hasNext())) {
            
          def firstname = substringBefore(Sender,'@')
          def lastname = substringBefore(Sender,'@');
        
          //Create Contact Party
          def ContactPartyObject = newView('PersonParty');
          def ContactParty = ContactPartyObject.createRow();
          ContactParty.setAttribute('CreatedByModule','FUSE');
        
          //Create Contact Profile
          def ContactPartyProfile = ContactParty.PersonProfile;
          def ContactProfileRow = ContactPartyProfile.createRow();
          ContactProfileRow.setAttribute('CreatedByModule','FUSE');
          ContactProfileRow.setAttribute('DeceasedFlag','N');
          ContactProfileRow.setAttribute('PersonFirstName',firstname);
          ContactProfileRow.setAttribute('PersonLastName',lastname);
        
          //Create Contact Usage assignment
          def ContactPartyUsageAssignment = ContactParty.PartyUsageAssignment;
          def ContactPuaRow = ContactPartyUsageAssignment.createRow();
          ContactPuaRow.setAttribute('CreatedByModule','FUSE');
          ContactPuaRow.setAttribute('PartyUsageCode','CONTACT');
        
          //Create Contact Email
          def emailcollection = ContactProfileRow.Email;
          def Emailrec = emailcollection.createRow();
          Emailrec.setAttribute('EmailAddress',email);
          Emailrec.setAttribute('CreatedByModule','FUSE');
        
          //Insert rows
          emailcollection.insertRow(Emailrec);
          ContactPartyUsageAssignment.insertRow(ContactPuaRow);
          ContactPartyProfile.insertRow(ContactProfileRow);
          ContactPartyObject.insertRow(ContactParty);
           
          //Set contact id and email
          def partyId = ContactProfileRow.PartyId;
          this.setAttribute('SenderEmail_c',email);
          this.setAttribute('ContactId_c',partyId); 
           
      }
    } catch(Exception e){
        println(e.getMessage())
    }
  5. In Application Composer, select Objects > Standard Objects > Service Request > Server Scripts.
  6. Click Triggers > Object Triggers > Add a New Trigger.
  7. Select Before Insert in Database for trigger and provide a trigger name.

    See the following groovy script for Trigger definition.

    try{
      if (SourceCd=='ORA_SVC_INBOUND_MSG' && ChannelTypeCd=='ORA_SVC_EMAIL'){
         def viaVo = this.getAttribute("channelCommunication");
         def inbdMsgId;
         def strRoutingCd = "";
         while(viaVo.hasNext()){
         def r = viaVo.next();
         strRoutingCd = r.getAttribute("RoutingCd");
         if (strRoutingCd == "ORA_SVC_FROM")
            inbdMsgId = r.getAttribute("InboundObjectId");
          break;
         }
     
        def inboundMsgVO=newView('InboundMessagesVO');
        def voRows = inboundMsgVO.findByKey(key(inbdMsgId),1)
        def voInboundRow = voRows[0]
        def partyId = voInboundRow.getAttribute("ContactId_c");
        this.setAttribute('PrimaryContactPartyId',partyId);
      }
    } catch(Exception e){
      println(e.getMessage())
    }
  8. Publish your sandbox.