0

What are the changes to be performed to send the list of json's, currently its working only for 1 account record. looking for the list of json as a body to pass it to salesforce.

Code:

@RestResource(urlMapping='/abc/*')
global without sharing class abc
{
 @HttpPost
  global static String doUpsert()
  {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        //system.debug('-----------data--------'+req) ;
        system.debug('-----------data--------'+req.requestBody.toString()) ;

    list<master> b = (list<master>)JSON.deserialize(req.requestBody.toString(),list<master>.class);

        system.debug('----------b----------'+b);

    list<Account> accList = new list<Account>();
    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
        Account acc = new Account();

        if(!b.isEmpty())
        {

            for(master mas: b)
            { 

                    acc.Magento_ID__c = string.valueof(mas.MagentoID);               
                    acc.firstname= string.valueof(mas.fname);                
                    acc.lastname= string.valueof(mas.lname);               
                    acc.Phone= string.valueof(mas.Phon);

                if(!string.isBlank(mas.DateCustomerAdded))   
                {
                    acc.Date_Customer_Added__c = date.valueof(mas.DateCustomerAdded);
                }
                    acc.Email__c= string.valueof(mas.Email);                
                    acc.Billing_Street_1__c = string.valueof(mas.BillingStreet);                
                    acc.Billing_City__c = string.valueof(mas.BillingCity);                
                    acc.Billing_State_Province__c = string.valueof(mas.BillingStateProvince);                
                    acc.Billing_Country__c = string.valueof(mas.BillingCountry);                
                    acc.Billing_Zip_Postal_Code__c = string.valueof(mas.BillingZip);               
                    acc.Shipping_Street_1__c = string.valueof(mas.ShippingStreet);                
                    acc.Shipping_City__c = string.valueof(mas.ShippingCity);                
                    acc.Shipping_State_Province__c = string.valueof(mas.ShippingStateProvince);                
                    acc.Shipping_Country__c = string.valueof(mas.ShippingCountry);                
                    acc.Shipping_Zip_Postal_Code__c = string.valueof(mas.ShippingZip);               
                    acc.Hair_Type__c = string.valueof(mas.HairType);                
                    acc.Hair_Color__c = string.valueof(mas.HairColor);

                if(!string.isBlank(mas.PersonDOB))   
                {
                    acc.PersonBirthdate = date.valueof(mas.PersonDOB);
                }
                    acc.Hair_Texture__c = string.valueof(mas.HairTexture);                
                    acc.Hair_Porosity__c = string.valueof(mas.HairPorosity);                
                    acc.Hair_Length__c = string.valueof(mas.HairLength);                
                    acc.Person_Using_Product__c = string.valueof(mas.PersonUsProduct);
                   acc.Hair_Goals__c = string.valueof(mas.HairGoals);                
                    acc.Skin_Issue__c = string.valueof(mas.SkinIssue);

                if(!string.isBlank(mas.OptInEmail))   
                {
                    acc.Opt_In_to_Email__c = Boolean.valueof(mas.OptInEmail);
                }
                if(!string.isBlank(mas.OptInWeeklyDeals))   
                {
                    acc.Opt_In_to_Weekly_Deals__c = Boolean.valueof(mas.OptInWeeklyDeals);
                }
                if(!string.isBlank(mas.OptInPromos))   
                {
                    acc.Opt_In_to_Promos__c = Boolean.valueof(mas.OptInPromos);
                }
                if(!string.isBlank(mas.OptInNews))   
                {
                    acc.Opt_In_to_News__c = Boolean.valueof(mas.OptInNews);
                }
                if(!string.isBlank(mas.OptInCommunity))   
                {
                    acc.Opt_In_to_Community_Commerce__c = Boolean.valueof(mas.OptInCommunity);
                }

                    acc.RecordTypeId = personAccountRecordType.id;

                accList.add(acc);
            }   
                List<Database.UpsertResult> upsertResult = Database.upsert(accList,Account.Fields.Email__c,false);
                //Insert accList;
                system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+upsertResult);
        }

        // return JSON.serialize(acc); 
        return 'Success';
  }

   global class master
   {
       public string MagentoID;
       public string fname;
       public string lname;
       public string Phon;
       public string Email; 
       public string DateCustomerAdded;
       public string BillingStreet;
       public string BillingCity;
       public string BillingStateProvince;
       public string BillingCountry;
       public string BillingZip;
       public string ShippingStreet;
       public string ShippingCity;
       public string ShippingStateProvince;
       public string ShippingCountry;
       public string ShippingZip;
       public string HairType;
       public string HairColor;
       public string PersonDOB;
       public string HairTexture;
       public string HairPorosity;
       public string HairLength;
       public string PersonUsProduct;
       public string HairGoals;
       public string SkinIssue;
       public string OptInEmail;
       public string OptInWeeklyDeals;
       public string OptInPromos;
       public string OptInNews;
       public string OptInCommunity;      
       public master()
       {

       }
   }

}

closed as unclear what you're asking by Adrian Larson, Ratan Paul, Boris Bachovski, Sebastian Kessel, Vigneshwaran G Oct 28 '16 at 20:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    If you select your code and hit CTRL + K, it will indent for you. – Adrian Larson Oct 28 '16 at 6:28
  • i have tried : Transient List<Database.UpsertResult> upsertResult; upsertResult = Database.upsert(accList,Account.Fields.Email__c,false); But still no luck. Looking for some help. – B1 sfdc Oct 28 '16 at 6:36
1

The loop logic change would be:

list<Account> accList = new list<Account>();
for(master mas: b)
{
    Account acc = new Account();
    accList.add(acc);

    acc.Magento_ID__c = ...
    ...
}
return JSON.serialize(accList);

Note that setting the JSON in the RestResponse body rather than returning it avoids unwanted escaping.

Not the answer you're looking for? Browse other questions tagged or ask your own question.