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
Mahesh KGMahesh KG 

How to Attach a file to Account Object

Hi Team,

Can you please help me in attaching a file to the Account Object. 
i am able to create the account object with the below code i now need to attacha a file to the account object.

@RestResource(urlMapping='/Account/*')
global with sharing class AccountManager {
    @HttpGet
    global static Account getAccountById() {
        RestRequest request = RestContext.request;
        // grab the caseId from the end of the URL
        String accountId = request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        Account result =  [SELECT Name
                        FROM Account
                        WHERE Id = :accountId];
        return result;
    }
    @HttpPost
    global static ID createAccount(String accountName,String accountOwner, String accountManager,String website,String companyCode,String atsOrHris, String accountSetUpFee,String accountStatus,String type,Decimal annualRevenue, Decimal annualAbRevenue, String revenueStream,String serviceLevelAgrmnt,String billingAddress,
    String shippingAddress,String billingStreet,String billingCity, String billingState,String billingZip,
                                   String billingCountry,
                                   String shippingStreet,String shippingCity, String shippingState,String shippingZip,
                                   String shippingCountry,String dunsNumber,String lineOfBus,String subType,String lastName) {
        Account restAcc = new Account(
                                      Name=accountName,
                                      ParentId=accountOwner,Ac
                                      count_Manager__c=accountManager, 
                                      // need to test website url field Id
                                      Website=website,
                                      Company_Code__c=companyCode,
                                      ATS_or_HRIS__c=atsOrHris,
                                      Account_Set_up_Fee__c=accountSetUpFee,
                                      AccountStatus_c=accountStatus,
                                      Type=type, 
                                      AnnualRevenue=annualRevenue, 
                                      Annual_AB_Revenue__c= annualAbRevenue,
                                      Revenue_Stream__c = revenueStream,
                                      ServiceLevelAgrmnt = serviceLevelAgrmnt,
                                      BillingAddress=billingAddress,
                                      BillingStreet=billingStreet,
                                      BillingCity= billingCity,
                                      BillingState=billingState,
                                      BillingPostalCode=billingZip,
                                      BillingCountry=billingCountry,
                                      ShippingAddress=shippingAddress,
                                      ShippingStreet=shippingStreet,
                                      ShippingCity= shippingCity,
                                      ShippingState=shippingState,
                                      ShippingPostalCode=shippingZip,
                                      ShippingCountry=shippingCountry,
                                      DunsNumber=dunsNumber,
                                      Line_of_Business__c=lineOfBus,
                                      SubType=subType);
        insert restAcc;
                                      // Account a = [select Id, Name from Account where Name = ''Shaik'];
                                       Contact c = new Contact();
                                       c.LastName = lastName;
                                       c.AccountId = restAcc.Id;
                                       insert c;
        return restAcc.Id;
                                       
                                       

    }   
    @HttpDelete
    global static void deleteCase() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
        delete thisCase;
    }     
    @HttpPut
    global static ID upsertCase(String name,String parent, String accountMgr,String setUpFee,String vendorType,
                                   Decimal annualRevenue, Decimal annualAbRevenue, String revenueStream,
                                   String billingStreet,String billingCity, String billingState,String billingPostalCode,
                                   String billingCountry,
                                   String shippingStreet,String shippingCity, String shippingState,String shippingPostalCode,
                                   String shippingCountry) {
        Account restAccUpdate = new Account(
                                      Name=name,
                                      ParentId=parent,
                                      Account_Manager__c=accountMgr, 
                                      Account_Set_up_Fee__c=setUpFee,
                                      Type=vendorType, 
                                      AnnualRevenue=annualRevenue, 
                                      Annual_AB_Revenue__c= annualAbRevenue,
                                      Revenue_Stream__c = revenueStream,
                                      BillingStreet=billingStreet,BillingCity= billingCity,BillingState=billingState,
                                      BillingPostalCode=billingPostalCode,BillingCountry=billingCountry,
                                      ShippingStreet=shippingStreet,ShippingCity= shippingCity,ShippingState=shippingState,
                                      ShippingPostalCode=shippingPostalCode,ShippingCountry=shippingCountry);
        // Match case by Id, if present.
        // Otherwise, create new case.
        upsert restAccUpdate;
        // Return the case ID.
        return restAccUpdate.Id;
    }
    @HttpPatch
    global static ID updateCaseFields() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Account thisCase = [SELECT Id FROM Account WHERE Id = :caseId];
        // Deserialize the JSON string into name-value pairs
        Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
        // Iterate through each parameter field and value
        for(String fieldName : params.keySet()) {
            // Set the field and value on the Case sObject
            thisCase.put(fieldName, params.get(fieldName));
        }
        update thisCase;
        return thisCase.Id;
    }    
}


Thanks and Regards
Mahesh 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Mahesh,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/130867/attaching-document-by-calling-apex-rest-resource

https://xomino.com/2016/09/05/adding-an-attachment-to-an-opportunity-using-salesforce-rest-apis/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Mahesh KGMahesh KG
Hi Khan,

i could not get much from the above links that you shared. Can you please help me more on this?


Thanks and Regards
Mahesh K G