• Jyoti Navale
  • NEWBIE
  • 19 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 7
    Replies
Hi, I am learning REST apis, in which I can post a single record but now I want to handle a bulky calls.  Anyone having an example for the same?...I googled it but I am confused about how to start...so want a runinig code if possible. Thanks in advance
I am learing REST.... During developing code for calling PUT method I stuck at a point. I am loading a pick list at the load time depending on account name selected I am listing below the table for Status of cases available for that Account. Now I want to edit that Status and save it. But I am not able to use inlineEditSupport component as I am using DTO....anyonw know how to solve this issue??

I needed to create DTO as my REST methods do not let me to store response as a case list. 

my code is here:
(I am commenting some part)

VF page :
<apex:page standardController="Account" Extensions="demoExtension">
  
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Picklists" columns="2">
                <apex:selectList value="{!selectedAccounts}" size="1" label="Account" >
                    <!-- <apex:actionSupport event="onchange" action="{!getOpenCases}" status="assign-action-status" reRender="theForm, caseResults"/> -->
                    <apex:selectOptions value="{!items}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>

        <apex:pageBlock mode="inlineEdit">
            <apex:pageBlockTable value="{!resDtolist}" var="val" >          
                <apex:column value="{!val.Status}" headerValue="Status"/>
                <apex:inlineEditSupport event="ondblClick" />
            </apex:pageBlockTable>
        </apex:pageBlock>
        
    </apex:form>
  
</apex:page>

APEX / Controller /class code :

public class demoExtension {
    // stores selected account option
    public String selectedAccounts { get; set; }
    public List<RestDTO> resDtolist{ get; set; }
    // stores accounts (used as drop down list)
    public List<Account> accountList { get; set; }
    
    // constructor
    public demoExtension(ApexPages.StandardController controller) {
        accountList = [SELECT name, id, OwnerId FROM Account WHERE OwnerId=:UserInfo.getUserId()];
    
        resDtolist = new List<RestDTO>();
        for(Case c : [SELECT ID, Subject , Status , Owner.Id , Owner.Name FROM case LIMIT 10]){
            resDtolist.add(new RestDTO(c));
        }
    }
    // creates drop down list of options (account ids and names)
    public List<SelectOption> getItems() 
    {
        List<SelectOption> options = new List<SelectOption>();
        for(Account actName : accountList ) //constructor
        {
            options.add(new SelectOption(actName.name, actName.name));
        }
        return options;
    }
    
    public class RestDTO{
        public String Id{ get; set; }
        public String Subject{ get; set; }
        public String Status{ get; set; }
        public String OwnerId{ get; set; }
        public String OwnerName{ get; set; }
        
        public RestDTO(Case c){
            this.Id = c.Id;          
            this.Subject = c.Subject;            
            this.Status = c.Status;          
            this.OwnerId = c.OwnerId;            
            this.OwnerName = c.Owner.Name;           
        }
    }


}



When to use RestRequest/RestResponse and when to use HttpResuest/HttpResponse?

I am learning REST in Saleforce. I know there are methods like GET, POST, PUT, PATCH, DELETE.
I have written a code for this (actually taken an example from a help page from Salesforce)

@RestResource(urlMapping='/FieldCases/*')
global with sharing class myRESTCaseController {
    // create  new case : POST method to create
    @HttpPost   
    global static String createNewCase(String companyName, String caseType) {
        System.debug('COMPANY: '+companyName);
        System.debug('CASE TYPE: '+caseType);
        
        List<Account> company = [Select ID, Name, Email__c, BillingState from Account where Name = :companyName];
        List<Support_Member__c> support;
        
        if(company.size() > 0) {
            support = [SELECT ID, User__c, Name, User__r.Email from Support_Member__c WHERE Support_State__c = :company[0].BillingState LIMIT 1];
        }
        List<Support_Documentation__c> doc = [SELECT ID from Support_Documentation__c WHERE Type__c = :caseType ORDER BY CreatedDate ASC LIMIT 1];
        
        if(company.size() == 0 || support.size() == 0 || doc.size() == 0) {
            return 'No support data exists for this problem';
        }
        
        Case c = new Case();
        c.OwnerId = support[0].User__c;
        c.AccountId = company[0].Id;
        c.Subject = caseType + ' for '+companyName;
        c.Status = 'Open';
        insert c;
        
        //sendEmail(companyName, company[0].Email__c, doc[0].Id, support[0].Name, support[0].User__r.Email);
        
        return 'Submitted case to '+support[0].Name+' for '+companyName+'.  Confirmation sent to '+company[0].Email__c;
    }
    
    // delete an old case : DELETE method used
    @HttpDelete
    global static String deleteOldCases() {
        String companyName = RestContext.request.params.get('companyName');
        Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName];
        
        List<Case> cases = [SELECT Id, Subject, Status, OwnerId, Owner.Name from Case WHERE AccountId =: company.Id AND Status = 'Closed'];
        delete cases;
        
        return 'Closed Cases Deleted';
    }
    
    
    // update a case : PATCH method used for updation
    @HttpPatch
    global static String updateCase(String caseId, String caseStatus, String caseNote) {
        Case companyCase = [SELECT Id, Subject, Status, Description from Case WHERE Id =: caseId];
        
        companyCase.Status = caseStatus;
        companyCase.Description += '/n/n';
        companyCase.Description += caseNote;
        update companyCase;
        
        return 'Case Updated';
    }
    
    // updates a case : PUT method used to update/insert
    @HttpPut
    global static String updateCase() {
        RestRequest req;
        String companyName = req.params.get('companyName');
        Account company = [ Select ID, Name, Type, BillingState from Account where Name = :companyName];

        Attachment a = new Attachment();
        a.ParentId = company.Id;
        a.Name = 'test.png';
        a.Body = req.requestBody;
        
        insert a;
        
        return 'Attachment added';
    }
    
    // get Open case : GET method to get records
    @HttpGet
    global static List<Case> getOpenCases() {
        String companyName = RestContext.request.params.get('companyName');
        Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName];
        
        List<Case> cases = [SELECT Id, Subject, Status, OwnerId, Owner.Name from Case WHERE AccountId =: company.Id];
        return cases;
        
    }
}

I want to know how to call these methods?
I suppose I can call them from cURL....if so how?
Currently I am trying to call them from Anonymous window of developer Console like this:
String companyName = 'Edge Communications';
String caseType = 'Electrical';


Http http = new Http();
//HttpRequest request = new HttpRequest(); // http format
RestRequest request = RestContext.Request; // rest format***


request.setEndpoint('https://instance.salesforce.com/services/apexrest/FieldCase?');

// calling method
request.setMethod('POST');

// pass parameteres : POST method
request.setBody('companyName='+EncodingUtil.urlEncode(companyName, 'UTF-8')+'&caseType='+EncodingUtil.urlEncode(caseType, 'UTF-8'));

// header params
request.setHeader('Authorization', 'Bearer '+userinfo.getSessionId());
request.setCompressed(true);

// response
//HttpResponse response = http.send(request);
RestResponse response = RestContext.response;***

System.debug('Request : '+request.toString());
System.debug('Response : '+response.getBody());

And I am getting error like this :
"
Response : [{"errorCode":"UNSUPPORTED_MEDIA_TYPE","message":"Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded"}]
"

Now my concern is....when can I use RestRequest/RestResponse and when to use HttpResuest/HttpResponse?...Any help?
Hi, I have a site in my salesforce org. I want to make it as a community. How can I achieve that?
Thanks
Hello, I am new to Salesfotce. I want to design a form which can take inputs from user and want to post them to salesforce. How can I achieve this?
I am learing REST.... During developing code for calling PUT method I stuck at a point. I am loading a pick list at the load time depending on account name selected I am listing below the table for Status of cases available for that Account. Now I want to edit that Status and save it. But I am not able to use inlineEditSupport component as I am using DTO....anyonw know how to solve this issue??

I needed to create DTO as my REST methods do not let me to store response as a case list. 

my code is here:
(I am commenting some part)

VF page :
<apex:page standardController="Account" Extensions="demoExtension">
  
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Picklists" columns="2">
                <apex:selectList value="{!selectedAccounts}" size="1" label="Account" >
                    <!-- <apex:actionSupport event="onchange" action="{!getOpenCases}" status="assign-action-status" reRender="theForm, caseResults"/> -->
                    <apex:selectOptions value="{!items}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>

        <apex:pageBlock mode="inlineEdit">
            <apex:pageBlockTable value="{!resDtolist}" var="val" >          
                <apex:column value="{!val.Status}" headerValue="Status"/>
                <apex:inlineEditSupport event="ondblClick" />
            </apex:pageBlockTable>
        </apex:pageBlock>
        
    </apex:form>
  
</apex:page>

APEX / Controller /class code :

public class demoExtension {
    // stores selected account option
    public String selectedAccounts { get; set; }
    public List<RestDTO> resDtolist{ get; set; }
    // stores accounts (used as drop down list)
    public List<Account> accountList { get; set; }
    
    // constructor
    public demoExtension(ApexPages.StandardController controller) {
        accountList = [SELECT name, id, OwnerId FROM Account WHERE OwnerId=:UserInfo.getUserId()];
    
        resDtolist = new List<RestDTO>();
        for(Case c : [SELECT ID, Subject , Status , Owner.Id , Owner.Name FROM case LIMIT 10]){
            resDtolist.add(new RestDTO(c));
        }
    }
    // creates drop down list of options (account ids and names)
    public List<SelectOption> getItems() 
    {
        List<SelectOption> options = new List<SelectOption>();
        for(Account actName : accountList ) //constructor
        {
            options.add(new SelectOption(actName.name, actName.name));
        }
        return options;
    }
    
    public class RestDTO{
        public String Id{ get; set; }
        public String Subject{ get; set; }
        public String Status{ get; set; }
        public String OwnerId{ get; set; }
        public String OwnerName{ get; set; }
        
        public RestDTO(Case c){
            this.Id = c.Id;          
            this.Subject = c.Subject;            
            this.Status = c.Status;          
            this.OwnerId = c.OwnerId;            
            this.OwnerName = c.Owner.Name;           
        }
    }


}



Hi, I am learning REST apis, in which I can post a single record but now I want to handle a bulky calls.  Anyone having an example for the same?...I googled it but I am confused about how to start...so want a runinig code if possible. Thanks in advance
I am learing REST.... During developing code for calling PUT method I stuck at a point. I am loading a pick list at the load time depending on account name selected I am listing below the table for Status of cases available for that Account. Now I want to edit that Status and save it. But I am not able to use inlineEditSupport component as I am using DTO....anyonw know how to solve this issue??

I needed to create DTO as my REST methods do not let me to store response as a case list. 

my code is here:
(I am commenting some part)

VF page :
<apex:page standardController="Account" Extensions="demoExtension">
  
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Picklists" columns="2">
                <apex:selectList value="{!selectedAccounts}" size="1" label="Account" >
                    <!-- <apex:actionSupport event="onchange" action="{!getOpenCases}" status="assign-action-status" reRender="theForm, caseResults"/> -->
                    <apex:selectOptions value="{!items}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>

        <apex:pageBlock mode="inlineEdit">
            <apex:pageBlockTable value="{!resDtolist}" var="val" >          
                <apex:column value="{!val.Status}" headerValue="Status"/>
                <apex:inlineEditSupport event="ondblClick" />
            </apex:pageBlockTable>
        </apex:pageBlock>
        
    </apex:form>
  
</apex:page>

APEX / Controller /class code :

public class demoExtension {
    // stores selected account option
    public String selectedAccounts { get; set; }
    public List<RestDTO> resDtolist{ get; set; }
    // stores accounts (used as drop down list)
    public List<Account> accountList { get; set; }
    
    // constructor
    public demoExtension(ApexPages.StandardController controller) {
        accountList = [SELECT name, id, OwnerId FROM Account WHERE OwnerId=:UserInfo.getUserId()];
    
        resDtolist = new List<RestDTO>();
        for(Case c : [SELECT ID, Subject , Status , Owner.Id , Owner.Name FROM case LIMIT 10]){
            resDtolist.add(new RestDTO(c));
        }
    }
    // creates drop down list of options (account ids and names)
    public List<SelectOption> getItems() 
    {
        List<SelectOption> options = new List<SelectOption>();
        for(Account actName : accountList ) //constructor
        {
            options.add(new SelectOption(actName.name, actName.name));
        }
        return options;
    }
    
    public class RestDTO{
        public String Id{ get; set; }
        public String Subject{ get; set; }
        public String Status{ get; set; }
        public String OwnerId{ get; set; }
        public String OwnerName{ get; set; }
        
        public RestDTO(Case c){
            this.Id = c.Id;          
            this.Subject = c.Subject;            
            this.Status = c.Status;          
            this.OwnerId = c.OwnerId;            
            this.OwnerName = c.Owner.Name;           
        }
    }


}



Hi, I have a site in my salesforce org. I want to make it as a community. How can I achieve that?
Thanks