Salesforce希望将值从列表传递到JSON以调用API

时间:2018-02-02 09:42:14

标签: rest salesforce apex

Helloforce开发人员,

从我在下面的代码中创建的SOQL列表中,我想将电子邮件和名称传递给我已经序列化的json。请让我知道为什么我不能把它们放在一起

APEX:

public class SampleMaroPost {
    public static string sendEmailThroughMaro(string myInpEmail) {
        string successContacts = '';
        string failureContacts = '';
        Date d = Date.Today().addDays(15);
        list<Account> conts = new list<Account> ([SELECT name, Email_FLP_com__c  from account where ID IN
(select Distributor__c from Stripe_Subscripton__c where Start_Date__c = TODAY AND Actual_End_Date__c = : d AND Status__c='active' ) AND  Email_FLP_com__c != NULL LIMIT 1]);

        overallEmail myEmail = new overallEmail();
        for(Account c : conts){
            myEmail.email.campaign_id = 172;
            myEmail.email.contact.Email = c.Email_FLP_com__c;
            myEmail.email.contact.first_name = c.name;
            system.debug('#### Input JSON: ' + JSON.serialize(myEmail)); 

使用的JSON:

{
  "email": {
    "campaign_id": 172,
    "contact": {
      "email": "x1testa@gmail.com",
      "first_name": "Test_naresh"
    }
  }
}

以下是与硬编码名称和电子邮件一起使用时的整个代码。这是有效的,我们希望查询我们的记录并插入&#34; Name&#34; &#34;电子邮件&#34;然后分批发送它们。

public class sampleMaroPost1 {
    public list<Account> autorenewal {set;get;}
        public void renewal(){

            Date d = Date.Today().addDays(15);
            autorenewal = [SELECT ID, Email__c, name, Provision_Date__c, Email_FLP_com__c  from account where ID IN
(select Distributor__c from Stripe_Subscripton__c where Actual_End_Date__c = :d  AND Status__c='active' ) AND  Email_FLP_com__c != NULL limit 1];


    }
    public static maroResponse sendEmailThroughMaro(string myInpEmail) {


        //Contact c = [SELECT Id ,LastName, Email FROM Contact WHERE Email = 'vineeth.anirush@gmail.com'];    
        string resultBodyGet = '';
        overallEmail myEmail = new overallEmail();
        myEmail.email.campaign_id = 174;
        myEmail.email.contact.email = 'shrikant.bhise@nextsphere.com'; //c.Email;
        myEmail.email.contact.first_name = 'Swetha'; //c.LastName;
        Map<String, String> tags = new Map<String, String>();
        tags.put('firstName', 'Swetha');
        myEmail.email.tags = tags;
        system.debug('#### Input JSON: ' + JSON.serialize(myEmail));
        String endpoint = 'http://api.maropost.com/accounts/1173/emails/deliver.json?auth_token=j-V4sx8ueUT7eKM8us_Cz5JqXBzoRrNS3p1lEZyPUPGcwWNoVNZpKQ';
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('POST');
        req.setHeader('Accept', 'application/json');
        req.setHeader('Content-type', 'application/json');
        req.setbody(JSON.serialize(myEmail));
        Http http = new Http();
        try{
            system.debug('Sending email');
            HTTPResponse response = http.send(req);
            system.debug('sent email');
             resultBodyGet = response.getBody();  
            system.debug('Output response:' + resultBodyGet);
            maroResponse myMaroResponse = new maroResponse();
        }
        catch (exception e){
            system.debug('#### exception: ' + e.getMessage());
        }

        maroResponse myMaroResponse = new maroResponse();

       if(resultBodyGet != null && resultBodyGet != '') 
       {
         myMaroResponse = (maroResponse) JSON.deserialize(resultBodyGet, maroResponse.class);
       }


        system.debug('#### myMaroResponse: ' + myMaroResponse);      

        return myMaroResponse;        

    }   

    public class maroResponse {
        public string message {get;set;}
    }

    public class overallEmail {
        public emailJson email = new emailJson();
    }

    public class emailJson {
        public Integer campaign_id;
        public contactJson contact = new contactJson();
        public Map<String, String> tags;
    }

    public class contactJson {
        public string email;
        public string first_name;
    }


}

1 个答案:

答案 0 :(得分:0)

第16日没有数据。我用了 unique_ptr<T>(new typename std::remove_extent<T>::type[size]())代替

Date d = Date.Today().addDays(14);

谢谢