+ Start a Discussion
deepa_tansendeepa_tansen 

Email to New records in Custom Object

Hello Friends,

 

Hope You all are doing good.....I have little problem with one of the trigger I am working on....Please help me becuase I don't kno what to do....

 

Objective: To build an error management email to "custom object" 
feature.

Requirement: Whenever an error occurs during either an upsert function
or response (call-out) function in the Skyvva framework, we need an
email to be sent and also a case (request) record to be created in a
custom object called "SFDC User Requests
 in our SFDC Org. This required feature should be similar to a standard email to case feature.

Reason not to use the standard email to case feature: We do not use the
standard case object to create cases/ tickets related to Salesforce
requests and issues. Instead we use a custom object called "SFDC User
Requests."

 

Here the Trigger I wrote....

trigger trgMessage on skyvvasolutions__IMessage__c (after update) {

List<Salesforce_User_Request__c> lSFUserRequest = new List<Salesforce_User_Request__c>();

 for(skyvvasolutions__IMessage__c msg: Trigger.New){

        //populate value of failed message to custom object and add to list

        if(msg.skyvvasolutions__Status__c=='Failed'){

            lSFUserRequest.add(new Salesforce_User_Request__c(

                //assign value from skyvvasolutions__IMessage__c to custom object         
                   // Status__c = 'New',
                    Name = msg.skyvvasolutions__Target__c,
                    Salesforce_Request_Details__c = msg.skyvvasolutions__Comment__c
                   // Types_of_User_Requests__c = msg.skyvvasolutions__Type__c
                    
                   ));

            }

    }
     //insert, if external Id is specify external Id, you can upsert external_Id__c
   if(lSFUserRequest.size()>0)
    {
            insert lSFUserRequest;

    }
}

 

The problem is that when we are testing it from Skyvva framework It creates two records on The Custom Object, first I thought becuase of workflow but I deactivated the workflow and it is still creating two records I dont know what to do ...

 

Please let me know the perfect solution.. Thanks in advance.........

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Seems like you have a workflow or a trigger in your object.

Well one suggestion why dont you check the status of the record before making an update?.

 

 

For example the below code makes sure there is a status change and update event is not fired because changes in some other field.

 

for(mySobject__c msg : trigger.newMap.values()){
if(msg.skyvvasolutions__Status__c != trigger.oldMap.get(msg.Id).Status__c && msg.skyvvasolutions__Status__c == 'Failed'){
/do the insert here
}
}

All Answers

Balu_SFDCBalu_SFDC

Hi Deepa,

 

it is possible through "Email Services"

Create one "Email Service"  and create one class. add this class to this email service.

 

 

 

 

 

Develop

 
Class For Email to Your Object.(Replace Conatct Object with your Object)
 

//created By:Hussainaiah.
//Use : this is used for the Creating the Contact from Email.

Global class EmailtoContact implements Messaging.inboundEmailHandler{
Contact Contact;
Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope env)
{

// Create an inboundEmailResult object for returning
//the result of the Apex Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
String strEmailId = email.fromAddress;
String strSubject = email.subject;
String myText=email.plainTextBody;
String myFromName = email.fromName;
// TextBody = email.plainTextBody,
//String HtmlBody = email.htmlBody;

//Create a new test Lead and insert it in the Test Method

Contact = new Contact(lastName=myFromName,Email=strEmailId,Description=strSubject+'\n'+myText,OwnerId='00590000000xyUx'); //Change this id with user's id to whome you want to assign this lead.
insert Contact;

}

 

If any Error send an Email to user by using 

 

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

email.setSubject( subject );
email.setToAddresses( toAddresses );
email.setPlainTextBody( body );

// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

 

and also add the Email Service Email address in this address, it will create the case in your object.

 

I hope you understand.

 

Good Day......

Balu

 
 
deepa_tansendeepa_tansen

Hi Balu,

 

Thanks for your reply. I am sorry, I didnt understand it.

 

I have to write a trigger so any error happens because of the interface that we have that message will create new record in my object. I don't have to send any email to any one because Skyvva itself sends message to appropriate person when error occurs, that is skyvva's functionality but based on that error message we have to create new record in the object so Admin/Developer can have a look and will start working on it.

 

Let me know if you have any questions....

Avidev9Avidev9

Seems like you have a workflow or a trigger in your object.

Well one suggestion why dont you check the status of the record before making an update?.

 

 

For example the below code makes sure there is a status change and update event is not fired because changes in some other field.

 

for(mySobject__c msg : trigger.newMap.values()){
if(msg.skyvvasolutions__Status__c != trigger.oldMap.get(msg.Id).Status__c && msg.skyvvasolutions__Status__c == 'Failed'){
/do the insert here
}
}
This was selected as the best answer
deepa_tansendeepa_tansen

Thanks Avi....