Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need to know how to code Salesforce Tooling API in C#... The code shows errors...

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Web;
using System.Web.Http;

namespace GenieAPI.Controllers
{
    public class ToolingWebApiController : ApiController
    {

          public ToolingWebApiController()
          {
          Http http = new Http();
           }

        public HttpResponse createLightingWEBComponent(String cmponentName)
        {

            HttpRequest request = createHttpRequest('POST');

            request.setEndpoint('/services/data/v45.0/sobjects/LightningComponentBundle');

            JSONGenerator gen = Json.createGenerator(true);

            gen.writeStartObject();

            gen.writeStringField('FullName', cmponentName);

            gen.writeFieldName('Metadata');

            gen.writeStartObject();

            gen.writeStringField('masterLabel', cmponentName);

            gen.writeEndObject();

            gen.writeEndObject();

            request.setBody(gen.getAsString());

            return sendRequest(request);

        }



        public HttpResponse createLightingWEBComponentFile(String componentId, String componentName,

                                                            String filename, String format, String body)
        {

            //List<String> supportedFormats=new List<String>{'css','html','js','xml'};

            HttpRequest request = createHttpRequest('POST');

            request.setEndpoint('/services/data/v45.0/sobjects/LightningComponentResource ');

            JSONGenerator gen = Json.createGenerator(true);

            gen.writeStartObject();

            gen.writeStringField('LightningComponentBundleId', componentId);

            gen.writeStringField('FilePath', 'lwc/' + componentName + '/' + filename);

            gen.writeStringField('Format', format);

            gen.writeStringField('Source', body);

            gen.writeEndObject();

            request.setBody(gen.getAsString());

            return sendRequest(request);

        }







        private HttpRequest createHttpRequest(String requestType)
        {

            HttpRequest request = new HttpRequest();

            request.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v45.0/tooling');

            request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());

            request.setHeader('Content-Type', 'application/json');

            request.setMethod(requestType);

            return request;

        }

    }
}
   } 
Posted
Updated 12-Nov-19 19:55pm

1 solution

Quote:
The code shows errors...

Getting your code to compile is a fundamental part of "being a developer" - and just throwing your code at other people and saying "it don't work" doesn't help you develop at all.
Visual Studio is very helpful when it finds a problem; it's error messages are normally very good and accurate. So start with the error list and look at what it says. Double click on an error message and it will take you to the actual line, where the problem part will be underlined in red. Read the message, and it should be fairly obvious what the actual problem is - check what you wrote against the actual syntax you should have used, check your brackets match up, and check your code against the documentation for all methods you are calling, and that should cover most problems pretty quickly.

Give it a try - this is the easy part: getting your code to do what you wanted it to once it compiles is the hard bit!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900