• Geetha Chandran 1
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
Hello,
I am trying to create an event in Emma from Salesforce Apex class which will get invoked from the process builder. However, I am geting the following error:
​​​​​​An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.
Here is my sample apex class. There is no DML operation here.
public with Sharing class CreateEmmaEvent{
    @invocableMethod(label='Create Emma Event on RFI submission')
    public static void CreateEmmaEventForRFI(List<Id> RFIIds){
        List<RFI_Transaction__c> rfis = [Select ID, Contact__c, Lead__c, RFI_Email__c from RFI_Transaction__c where ID in :RFIIds];
        for (RFI_Transaction__c rfi : rfis){
           JSONGenerator gen = JSON.createGenerator(true);    
            gen.writeStartObject();      
            gen.writeStringField('event_name', 'rfi_submitted');
            gen.writeStringField('email',rfi.RFI_Email__c);
            gen.writeEndObject();
            String jsonS = gen.getAsString();
            System.debug('jsonMaterials'+jsonS);
            
            HttpRequest req = new HttpRequest();
            req.setMethod('POST');
            req.setbody(jsonS);
            req.setEndpoint('callout:emma_event/v1/1923372/events');
            req.setHeader('APIKEY', '{!$Credential.Password}');
            HttpResponse res = new Http().send(req);
           
        }
    }   
}
By the way, I created a Named Credential in Salesforce with the endpoint https://events.e2ma.net/ and the public api key as the password.
I would greatly appreciate if someone can guide me in the right direction to get this working. 

Thanks,
Geetha
Can someone please help me to get the SOQL (on opportunity object) from the for loop? I understand that we cannot have soql inside a for loop. Thanks a lot!!

      for (CampaignMember cm: cms) {
          oldcm= (CampaignMember)oldlist[i];
             //Update Opportunity Amount  
             o = [Select ID, Contact__c, CloseDate, CampaignID, OE_Program_of_Interest__c, Amount, Cvent_Balance_Due__c, Amount_Paid__c,       Description, StageName from Opportunity where Contact__c = :cm.ContactID and  Campaign.ID = :cm.CampaignId limit 1];       
             if(o.size() == 1){
                if(cm.Cost__c != oldcm.Cost__c){o[0].Amount = cm.Cost__c;}
                if(cm.Amount_Paid__c != oldcm.Amount_Paid__c){o[0].Amount_Paid__c = cm.Amount_Paid__c;}
                if(cm.Balance_Due__c != oldcm.Balance_Due__c){o[0].Cvent_Balance_Due__c = cm.Balance_Due__c;}                
                if(cm.Status != oldcm.Status && cm.Status == 'Registered'){o[0].StageName = 'Closed Won';}
                if(cm.Payment_Status__c != oldcm.Payment_Status__c && cm.Payment_Status__c == 'Paid in Full'){o[0].StageName = 'Closed Paid';} 
                if(cm.Status != oldcm.Status && (cm.Status == 'Cancelled' || cm.Status == 'Unattended' || cm.Status == 'Withdraw')){o[0].StageName = 'Closed Lost';}
                Updopps.add(o[0]);
             }   
      i++;      
      }
I am using the following AMPScript in my email template to insert a new record in to a custom object each time an email is sent. The custom object has both contact and lead lookup fields. So using the subscriber key, I first determine whether it is a lead or a contact id and then insert the new record accordingly. My problem is that duplicate records are inserted in to the custom object each time. The email is sent out using a salesforce send definition with campaign members as recipients and a SF report to exclude contacts/leads who have already received the same email. Could someone please help me understand why the following script is inserting the record twice each time an email is sent?

%%[

var @emailName, @subKey, @rsContact, @rsLead, @ContactRow, @LeadRow, @contactName, @leadName, @SFNewRec, @rowCount

Set @emailName = emailName_

set @subKey = AttributeValue("_subscriberkey")

Set @rsContact = RetrieveSalesforceObjects("Contact","Name","Id", "=", @subKey)

set @rowCount = rowcount(@rsContact)

if @rowCount > 0 then

    Set @ContactRow = ROW(@rsContact,1)

    Set @contactName = FIELD(@ContactRow,"Name")

        IF _messagecontext == "SEND" Then

            set @SFNewRec = CreateSalesforceObject("Campaign_Email_Sent__c", 2, "Contact__c", @subKey, "Email_Name__c", @emailName)

        ENDIF

endif

if empty(@contactName) then

       Set @rsLead = RetrieveSalesforceObjects("Lead","Name","Id", "=", @subKey)

        Set @LeadRow = ROW(@rsLead,1)

        Set @leadName = FIELD(@LeadRow,"Name")

endif

if not empty(@leadName) then

    IF _messagecontext == "SEND" Then

    set @SFNewRec = CreateSalesforceObject("Campaign_Email_Sent__c", 2, "Lead__c", @subKey, "Email_Name__c", @emailName)

    ENDIF

endif

]%%

 

Greatly appreciate your support!

Best Regards,

Geetha
Hello,
i am new to Apex code and have a question. I have built a new class on the Affilation object and my intention was to call the method from an after insert and after update trigger. Then I saw that the Affiliation object already has a trigger on it called TDTM_Affiliation. I understand that we should use only one trigger per object. How do I make sure that my new class is called from the existing trigger? I am not able to see what is in the TDTM_Affiliation trigger as it is a HEDA installed package. How do I do it? Any help would be greatly appreciated.

Thanks!
Geetha
Hello,
I am trying to create an event in Emma from Salesforce Apex class which will get invoked from the process builder. However, I am geting the following error:
​​​​​​An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.
Here is my sample apex class. There is no DML operation here.
public with Sharing class CreateEmmaEvent{
    @invocableMethod(label='Create Emma Event on RFI submission')
    public static void CreateEmmaEventForRFI(List<Id> RFIIds){
        List<RFI_Transaction__c> rfis = [Select ID, Contact__c, Lead__c, RFI_Email__c from RFI_Transaction__c where ID in :RFIIds];
        for (RFI_Transaction__c rfi : rfis){
           JSONGenerator gen = JSON.createGenerator(true);    
            gen.writeStartObject();      
            gen.writeStringField('event_name', 'rfi_submitted');
            gen.writeStringField('email',rfi.RFI_Email__c);
            gen.writeEndObject();
            String jsonS = gen.getAsString();
            System.debug('jsonMaterials'+jsonS);
            
            HttpRequest req = new HttpRequest();
            req.setMethod('POST');
            req.setbody(jsonS);
            req.setEndpoint('callout:emma_event/v1/1923372/events');
            req.setHeader('APIKEY', '{!$Credential.Password}');
            HttpResponse res = new Http().send(req);
           
        }
    }   
}
By the way, I created a Named Credential in Salesforce with the endpoint https://events.e2ma.net/ and the public api key as the password.
I would greatly appreciate if someone can guide me in the right direction to get this working. 

Thanks,
Geetha
Can someone please help me to get the SOQL (on opportunity object) from the for loop? I understand that we cannot have soql inside a for loop. Thanks a lot!!

      for (CampaignMember cm: cms) {
          oldcm= (CampaignMember)oldlist[i];
             //Update Opportunity Amount  
             o = [Select ID, Contact__c, CloseDate, CampaignID, OE_Program_of_Interest__c, Amount, Cvent_Balance_Due__c, Amount_Paid__c,       Description, StageName from Opportunity where Contact__c = :cm.ContactID and  Campaign.ID = :cm.CampaignId limit 1];       
             if(o.size() == 1){
                if(cm.Cost__c != oldcm.Cost__c){o[0].Amount = cm.Cost__c;}
                if(cm.Amount_Paid__c != oldcm.Amount_Paid__c){o[0].Amount_Paid__c = cm.Amount_Paid__c;}
                if(cm.Balance_Due__c != oldcm.Balance_Due__c){o[0].Cvent_Balance_Due__c = cm.Balance_Due__c;}                
                if(cm.Status != oldcm.Status && cm.Status == 'Registered'){o[0].StageName = 'Closed Won';}
                if(cm.Payment_Status__c != oldcm.Payment_Status__c && cm.Payment_Status__c == 'Paid in Full'){o[0].StageName = 'Closed Paid';} 
                if(cm.Status != oldcm.Status && (cm.Status == 'Cancelled' || cm.Status == 'Unattended' || cm.Status == 'Withdraw')){o[0].StageName = 'Closed Lost';}
                Updopps.add(o[0]);
             }   
      i++;      
      }
hi all,

I urgently need to edit/delete a post made by me on this discussion forum...But its not allowing me to do so and pops up
saying that 'you cant delete this question as others are interested in it'.
There are no likes and no comments on it still i am unable  to delete it
Any help would be highly appreciated

Its very urgent,
Thanks,