function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nag22Nag22 

Error - The remote server returned an error: (401) Unauthorized

Hi,

I am working on integration with docusign.

I have created an Rest Resouce class to update record in salesforce, and  I have give this  class URL like
https://test.salesforce.com/apexrest/services/UpdateRecord

Now docusign is calling this URL when ever the docusign database updated. But while calling this URL docusign getting

https://cs5.salesforce.com/services/apexrest/UpdateRecord:: Error - The remote server returned an error: (401) Unauthorized  ERROR.
public void sendTemplateWithDetails(){
    
        String passString = authHeader();  //THIS METHOD CONTAINS ALL THE CREDENTIALS OF DOCUSIGN
        Http httpProtocol = new Http();
        HttpRequest request = new HttpRequest();
        String endpoint = templateWrap.uri+'/envelopes';
        String xmlStr;
        if(oppty.id != null){
           xmlStr =  '<envelopeDefinition xmlns=\'http://www.docusign.com/restapi\'>' +
                    '<accountId>' + oppty.accountId + '</accountId>' +
                    '<status>sent</status>' +   // 'sent' to send envelope, 'created' to save as draft envelope
                    '<emailSubject>DocuSign API Call - Signature request from template</emailSubject>' +
                    '<templateId>' + templateId + '</templateId>' + 
                    '<templateRoles>' + 
                    '<templateRole>' +  
                    '<name>' + Account.Name+ '</name>' +
                    '<email>' + Account.personEmail+ '</email>' +               
                    '<roleName>' + 'Client' + '</roleName>' + 
                    '</templateRole>' + 
                    '</templateRoles>' + 
                    '<eventNotification>' +
                       '<EnvelopeEvents>' +
                          '<envelopeEvent>' +
                             '<envelopeEventStatusCode>'+'Completed'+'</envelopeEventStatusCode>' +
                          '</envelopeEvent>' +
                          '<envelopeEvent>' +
                             '<envelopeEventStatusCode>'+'Delivered'+'</envelopeEventStatusCode>' +
                          '</envelopeEvent>' +
                       '</EnvelopeEvents>' +
                       
                       '<loggingEnabled>'+'true'+'</loggingEnabled>' +
                       '<recipientEvents>' +
                          '<recipientEvent>' +
                             
                             '<recipientEventStatusCode>'+'Delivered'+'</recipientEventStatusCode>' +
                          '</recipientEvent>' +
                          '<recipientEvent>' +
                             '<recipientEventStatusCode>'+'Completed'+'</recipientEventStatusCode>' +
                          '</recipientEvent>' +
                       '</recipientEvents>' +
                      '<url>'+'https://cs5.salesforce.com/services/apexrest/UpdateOpportunityStatus'+'</url>'+   
                    '</eventNotification>'+
                    '</envelopeDefinition>';

        }

        request.setEndPoint(endpoint);
        request.setBody(xmlStr);
        request.setMethod('GET');
        request.setHeader('Content-Type', 'application/xml');
        request.setHeader('Content-Disposition', 'form-data');
        request.setHeader('X-DocuSign-Authentication', passString);
        
        system.debug('***********request body***'+request.getBody());
        HttpResponse response1 = httpProtocol.send(request);
How to authenticate to salesforce, when external system calling our URL to update records?

anyone help me to figure out this.
AshwaniAshwani
Hi Nag

I think you forgot to set X-SFDC-Session header in request.

request.setHeader('X-SFDC-Session', sessionIdString);


Nag22Nag22
But How to get session Id ?