• Arawind_Mundankar
  • NEWBIE
  • 25 Points
  • Member since 2018

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hello Community Developers ,
I am working on one rest api service class which  will accept  payload from external system and send it to our salesforce and will created data in Salesforce.

 I am getting this issue while running my test classes.
Apex Class :-


@RestResource (urlMapping = '/createContact/*')
global with sharing class communityUsersWebService {
 @HttpPost
    global static ResponseWrapper createContacts(){
        Boolean success = false;
        String message = '';
        Set<Id> accountIds = new Set<Id>();
        List<Contact> newContactRecords = new List<Contact>();
        RestRequest request = RestContext.request;
        RestResponse response = RestContext.response;
        if(request != null){
            System.debug('request is not equal to null');
            Blob body = request.requestBody;
            String requestBody = body.toString();
            if(requestBody != null){
                try{
                    Map<String, Object> deserializeRequestBody = (Map<String , Object>)JSON.deserializeUntyped(requestBody);
                    System.debug(' deserializeRequestBody Map :-' +deserializeRequestBody);
                    Map<String , Object> storedeserializeRequestBody = new Map< String , Object>();
                    for(String s : deserializeRequestBody.keySet()){
                        storedeserializeRequestBody.put(s , deserializeRequestBody.get(s));
                        System.debug(' storedeserializeRequestBody Map :-' +storedeserializeRequestBody);
                    }
                    if(!storedeserializeRequestBody.isEmpty()){
                        String DDI_Industry_c = String.valueOf(storedeserializeRequestBody.get('DDI_Industry__c'));
                        String RecordTypeId =  String.valueOf(storedeserializeRequestBody.get('RecordTypeId'));
                        String Firstname =  String.valueOf(storedeserializeRequestBody.get('Firstname'));
                        String LastName = String.valueOf(storedeserializeRequestBody.get('Lastname'));
                        String Email = String.valueOf(storedeserializeRequestBody.get('Email'));
                        String DDI_Contact_Role_c =  String.valueOf(storedeserializeRequestBody.get('DDI_Contact_Role__c'));
                        String DDI_Decision_role_c =  String.valueOf(storedeserializeRequestBody.get('DDI_Decision_role__c'));
                        String Contact_Type_c =  String.valueOf(storedeserializeRequestBody.get('Contact_Type__c'));
                        String Account_Name_c  = String.valueOf(storedeserializeRequestBody.get('Account_Name__c'));
                        
                        List<Contact> getContacts = [Select id , FirstName , LastName , Email , RecordTypeId from Contact 
                                                     Where RecordTypeId =: RecordTypeId
                                                     AND FirstName =: Firstname
                                                     AND LastName =: LastName];
                        if(getContacts != null && getContacts.size() > 0){
                            System.debug('You Cannot Create Contacts due to Duplicacy !!!');
                            success = true;
                            message  = 'Contact creation is not Proceesed Succesfully since Duplicates Contacts in Salesforce ';
         
                               
                            
                        }
                        else{
                            List<Account> getAccounts = [Select id ,  Name   from Account Where Name=: Account_Name_c Limit 1];
                            if(getAccounts != null && getAccounts.size() > 0){
                                for(Account account : getAccounts){
                                    accountIds.add(account.Id);
                                    
                                }
                                
                            }
                            if(accountIds != null && accountIds.size() == 1 ){
                                Id mapToAccountId ;
                                for(Id id : accountIds){
                                    mapToAccountId = id;
                                }
                                Contact newContact = new Contact();
                                newContact.DDI_Industry__c = DDI_Industry_c;
                                newContact.RecordTypeId = RecordTypeId;
                                newContact.FirstName = Firstname;
                                newContact.LastName = Lastname;
                                newContact.Email = Email;
                                newContact.DDI_Contact_Role__c = DDI_Contact_Role_c;
                                newContact.DDI_Decision_role__c = DDI_Decision_role_c;
                                newContact.Contact_Type__c = Contact_Type_c;
                                newContact.AccountId = mapToAccountId;
                                //newContact.Account_Name__c = Account_Name_c;
                                newContactRecords.add(newContact);
                                
                            }
                            if(newContactRecords != null && newContactRecords.size() > 0){
                                insert newContactRecords;
                                success = true;
                                message =  'Contact creation request is processed Succcessfully !!!!';
                               
                            }
                            
                            
                        }
                        
                        
                        
                        
                    }
                    
                    
                }
                Catch(Exception ex){
                    System.debug('The Exception is : '+ex.getMessage());
                    success = true;
                    message = ex.getMessage();
                    
                }
            }
        }
        
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.message = message ;
        responseWrapper.success = success;
        return responseWrapper;
        
    }
    
    
    global class ResponseWrapper{
           global String message ; 
           global Boolean success;
    }


}
 
Test Class :-


@isTest
public with sharing class communityUsersWebServiceTest {
    
    @isTest
    public static void testContactCreation(){
        Account account = new Account();
        account.name = 'Test Account';
        Id RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('DDI').getRecordTypeId();
        account.RecordTypeId = RecordTypeId;
        insert account;
        Contact contact = new Contact();
        contact.FirstName = 'Raghav';
        contact.LastName = 'Thakur';
        contact.Email = 'raghav.thakue+33@gmail.com';
        Id contactRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('DDI').getRecordTypeId();
        contact.RecordTypeId = contactRecordTypeId;
        contact.AccountId = account.Id;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        req.requestURI = '/services/apexrest/subscriptionUpdate/';
        String jsonMessage = '{"DDI_Industry": "Other", "RecordTypeId": "0123g000000kDbrAAE", "Firstname": "Raghav", "Lastname": "Thakur", "Email": "raghav.thakue+33@gmail.com","DDI_Contact_Role__c": "Point of Contact","DDI_Decision_role__c": "Other", "Contact_Type__c": "Partner", "Account_Name__c": "'+account.Name+'" }';
        if(jsonMessage != null){
            req.requestBody = Blob.valueOf(jsonMessage);
            req.httpMethod = 'POST';
            RestContext.request = req;
            RestContext.response = res;
           
            
        }
        
         communityUsersWebService.createContacts();
        
        
        
    }

}

In my test class the line which i made as bold showing an issue !!

Any Help really thankfull !!!!
 
I'm trying to generate an access token and it generates fine when using the "form-data" option in post-man. However, we have a client who is only able to use x-www-form-urlencoded in their post request and using this method returns the below error:

{
    "error": "invalid_grant",
    "error_description": "authentication failure"
}

I am including the password and security token (concatanated) in the request and I cant figure out why it would work using form-data but not form-urlencoded

Any help greatly appreciated!

 
Hello Community Developers ,
I am working on one rest api service class which  will accept  payload from external system and send it to our salesforce and will created data in Salesforce.

 I am getting this issue while running my test classes.
Apex Class :-


@RestResource (urlMapping = '/createContact/*')
global with sharing class communityUsersWebService {
 @HttpPost
    global static ResponseWrapper createContacts(){
        Boolean success = false;
        String message = '';
        Set<Id> accountIds = new Set<Id>();
        List<Contact> newContactRecords = new List<Contact>();
        RestRequest request = RestContext.request;
        RestResponse response = RestContext.response;
        if(request != null){
            System.debug('request is not equal to null');
            Blob body = request.requestBody;
            String requestBody = body.toString();
            if(requestBody != null){
                try{
                    Map<String, Object> deserializeRequestBody = (Map<String , Object>)JSON.deserializeUntyped(requestBody);
                    System.debug(' deserializeRequestBody Map :-' +deserializeRequestBody);
                    Map<String , Object> storedeserializeRequestBody = new Map< String , Object>();
                    for(String s : deserializeRequestBody.keySet()){
                        storedeserializeRequestBody.put(s , deserializeRequestBody.get(s));
                        System.debug(' storedeserializeRequestBody Map :-' +storedeserializeRequestBody);
                    }
                    if(!storedeserializeRequestBody.isEmpty()){
                        String DDI_Industry_c = String.valueOf(storedeserializeRequestBody.get('DDI_Industry__c'));
                        String RecordTypeId =  String.valueOf(storedeserializeRequestBody.get('RecordTypeId'));
                        String Firstname =  String.valueOf(storedeserializeRequestBody.get('Firstname'));
                        String LastName = String.valueOf(storedeserializeRequestBody.get('Lastname'));
                        String Email = String.valueOf(storedeserializeRequestBody.get('Email'));
                        String DDI_Contact_Role_c =  String.valueOf(storedeserializeRequestBody.get('DDI_Contact_Role__c'));
                        String DDI_Decision_role_c =  String.valueOf(storedeserializeRequestBody.get('DDI_Decision_role__c'));
                        String Contact_Type_c =  String.valueOf(storedeserializeRequestBody.get('Contact_Type__c'));
                        String Account_Name_c  = String.valueOf(storedeserializeRequestBody.get('Account_Name__c'));
                        
                        List<Contact> getContacts = [Select id , FirstName , LastName , Email , RecordTypeId from Contact 
                                                     Where RecordTypeId =: RecordTypeId
                                                     AND FirstName =: Firstname
                                                     AND LastName =: LastName];
                        if(getContacts != null && getContacts.size() > 0){
                            System.debug('You Cannot Create Contacts due to Duplicacy !!!');
                            success = true;
                            message  = 'Contact creation is not Proceesed Succesfully since Duplicates Contacts in Salesforce ';
         
                               
                            
                        }
                        else{
                            List<Account> getAccounts = [Select id ,  Name   from Account Where Name=: Account_Name_c Limit 1];
                            if(getAccounts != null && getAccounts.size() > 0){
                                for(Account account : getAccounts){
                                    accountIds.add(account.Id);
                                    
                                }
                                
                            }
                            if(accountIds != null && accountIds.size() == 1 ){
                                Id mapToAccountId ;
                                for(Id id : accountIds){
                                    mapToAccountId = id;
                                }
                                Contact newContact = new Contact();
                                newContact.DDI_Industry__c = DDI_Industry_c;
                                newContact.RecordTypeId = RecordTypeId;
                                newContact.FirstName = Firstname;
                                newContact.LastName = Lastname;
                                newContact.Email = Email;
                                newContact.DDI_Contact_Role__c = DDI_Contact_Role_c;
                                newContact.DDI_Decision_role__c = DDI_Decision_role_c;
                                newContact.Contact_Type__c = Contact_Type_c;
                                newContact.AccountId = mapToAccountId;
                                //newContact.Account_Name__c = Account_Name_c;
                                newContactRecords.add(newContact);
                                
                            }
                            if(newContactRecords != null && newContactRecords.size() > 0){
                                insert newContactRecords;
                                success = true;
                                message =  'Contact creation request is processed Succcessfully !!!!';
                               
                            }
                            
                            
                        }
                        
                        
                        
                        
                    }
                    
                    
                }
                Catch(Exception ex){
                    System.debug('The Exception is : '+ex.getMessage());
                    success = true;
                    message = ex.getMessage();
                    
                }
            }
        }
        
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.message = message ;
        responseWrapper.success = success;
        return responseWrapper;
        
    }
    
    
    global class ResponseWrapper{
           global String message ; 
           global Boolean success;
    }


}
 
Test Class :-


@isTest
public with sharing class communityUsersWebServiceTest {
    
    @isTest
    public static void testContactCreation(){
        Account account = new Account();
        account.name = 'Test Account';
        Id RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('DDI').getRecordTypeId();
        account.RecordTypeId = RecordTypeId;
        insert account;
        Contact contact = new Contact();
        contact.FirstName = 'Raghav';
        contact.LastName = 'Thakur';
        contact.Email = 'raghav.thakue+33@gmail.com';
        Id contactRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('DDI').getRecordTypeId();
        contact.RecordTypeId = contactRecordTypeId;
        contact.AccountId = account.Id;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        req.requestURI = '/services/apexrest/subscriptionUpdate/';
        String jsonMessage = '{"DDI_Industry": "Other", "RecordTypeId": "0123g000000kDbrAAE", "Firstname": "Raghav", "Lastname": "Thakur", "Email": "raghav.thakue+33@gmail.com","DDI_Contact_Role__c": "Point of Contact","DDI_Decision_role__c": "Other", "Contact_Type__c": "Partner", "Account_Name__c": "'+account.Name+'" }';
        if(jsonMessage != null){
            req.requestBody = Blob.valueOf(jsonMessage);
            req.httpMethod = 'POST';
            RestContext.request = req;
            RestContext.response = res;
           
            
        }
        
         communityUsersWebService.createContacts();
        
        
        
    }

}

In my test class the line which i made as bold showing an issue !!

Any Help really thankfull !!!!
 
I have one object : TradeHistories which is a reference to another object RepTradeHistories linked by lookup, the TradeHistory Object also has a master Detail Lookup RepCode which is a collection of Reps.

The Kicker is all the RepTradeHisories are tied to a single Rep.

My current Proccess is - Pass list of Objects that Contain the lookup for the TradeHisotry and the Rep Tied to it.  Query for all all TradeHistories that match the Rep and the TradeHistory ID.  Then use I have to also get the Rep from the Rep Object so the RepTradeHistory and Reference it before Update or Adding the RepTradeHisoty.

I would like to Have one Query that can pull the TradeHisotiry that matches the ID and Rep and also have the RepCode and all the Reps connected to that RepCode so I do not have to QUery separately for the Rep, I can just do a comparison in the same Query.
 
for (Wrapper ob : (List<Wrapper>)wResp) 
        {          
            Updated =false;

            //System.debug(ob);
            System.debug('------Rep Name : '+ob.Rep_Name);
            System.debug('------Split Percentage : '+ob.Split_Percentage);
            System.debug('------Cas_ID : '+ob.Trade_History_CAS_ID);
            System.debug('------Estimated Commission : '+ob.Estimated_Commission);

            for(Trade_History__c oh : 
            [Select 

               CAS_ID__c, Id, 
               RepCode__r.Id ,
               RepCode__c,
               LastModifiedDate                
            From Trade_History__c 
            Where RepCode__r.Name =:ob.Rep_Name AND CAS_ID__c =: ob.Trade_History_CAS_ID   //need to match there on UAT and prod misisngh on DEV               
             
             ]){             

                System.debug('------Found Record details '+ oh.Id);                 
                //get rep details 
                Rep__c  Rep = [Select Id,CAS_ID__c from Rep__c where Rep_Code__c=:oh.RepCode__c Limit 1];
                System.debug('------Found Rep details '+ Rep.Id); 

                 //ok trade history is valid (overkill check if we have an existing rep trade history i.e UPDATE vs INSERT, rep trade history has matche on casID)                  
                 for(Rep_Trade_History__c rth : 
                 [Select                     
                    Id ,
                    LastModifiedDate                
                 From Rep_Trade_History__c 
                 Where Trade_History__c =:oh.Id 

                 ]){                  
     
                     System.debug('------Found EXISTING rep trade history we must Update! '+ rth.Id);
                    //DO stuff  
                    //store it for now so we can combine update and save login                         
                    //update record here                     
                    
                    //do the modifciation here and add to the list
                    //just do the update here
                    rth.Trade_History_CAS_ID__c = ob.Trade_History_CAS_ID;

                    if( ob.Split_Percentage != null){
                        rth.Split_Percentage__c = ob.Split_Percentage;
                    }
                    if(ob.Estimated_Commission != null){
                        rth.Split_Revenue__c = ob.Estimated_Commission;  //** TO DO this might be a calculated feild percentage x comminson */
                    }    

                    rth.Rep__c = Rep.id;
                    rth.Trade_History__c = oh.id;  //link up the trade history
                    System.debug('------Updated Record Details for RepTradeHistory ID '+ rth.Id);                                      
                    
                    ExistingRepTradeHistories.add(rth); 
                 
                    Updated =true;
                   }