0

I have asked this question before in another post, however I didn't received a respond. I am trying to integrate ASP.Net MVC with Less Annoying CRM API. The purpose is to store form submissions into the CRM so as to organised each submissions into categories or tasks. The LACRM api is designed for PHP and the developers mentioned that they lack the skills to help in regards to C#. Here is my code (The LACRM uses default fields "FullName"and "Email" then it have a parameter "CustomFields" so that custom form inputs can be created. The issue I am experiencing is that only FullName is getting registered and the "Email and "CustomFields are showing in the Api logs but not registering in the fields on the crm): My Code:

    public async Task createContact(Booking booking)
        {
           
            //string APITOKEN = "some token";
            //string UserCode = "98992";
            //string Function = "CreateContact";
            //string PipeLineId = "3687195324475747612076041165694";
            //string url = "https://api.lessannoyingcrm.com?APIToken=" + APITOKEN + "&UserCode=" + UserCode + "&Function=" + Function + "&PipelineId=" + PipeLineId + "";
            string url = "https://api.lessannoyingcrm.com?APIToken=sometoken&UserCode=A2575&Function=CreateContact&PipelineId=3727019319364096972431828675722";
            var postData = new List>
            {
                
               };
            postData.Add(new KeyValuePair
            (
                   "Email[1]", (new KeyValuePair("Text", booking.Email),
                               new KeyValuePair("Type", "Work")).ToString()
               ));
    
            postData.Add(new KeyValuePair
               (
                   "Phone[1]", (new KeyValuePair("Text", booking.Phone),
                               new KeyValuePair("Type", "Work")).ToString()
               ));
    
            postData.Add(new KeyValuePair
               (
                   "Website[0]", (new KeyValuePair("Text", "")).ToString()
               ));
    
    
            postData.Add(new KeyValuePair
               (
                   "Location", (
                                new KeyValuePair("", booking.PostCode)
                                
                               ).ToString()
               ));
    
    
            postData.Add(new KeyValuePair
                (
    
                    "CustomFields", (
    
                        new KeyValuePair("Conservatory", booking.Consevatory),
                        new KeyValuePair("Size", booking.Size),
                        new KeyValuePair("Type", booking.RoofType),
                        new KeyValuePair("Source", booking.HearAboutUs),
                        new KeyValuePair("Comment", booking.OtherInfo),
                        new KeyValuePair("Conservatory", booking.SelectedProduct)
    
    
                    ).ToString()
    
    
                ));
        
    
            using (var httpClient = new HttpClient())
            {
                using (var content = new FormUrlEncodedContent(postData))
                {
                    content.Headers.Clear();
                    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    
                    HttpResponseMessage response = await httpClient.PostAsync(url, content);
    
                    var apiresponse = await response.Content.ReadAsAsync();
                }
            }
            return true;
        }

And this is the LACRM PHP code:

    $_REQUEST['ContactName'],
        "Email"=>array(
                    0=>array(
                        "Text"=>"$_REQUEST[Email]",
                        "Type"=>"Work"
                    )
                ),
        "Phone"=>array(
                    0=>array(
                        "Text"=>"$_REQUEST[Phone]",
                        "Type"=>"Work"
                    )
                ),
    );
     
    //...And then use the "CallAPI" function to send the information to LACRM
    $Result = CallAPI($UserCode, $APIToken, $Function, $Parameters);
    //That's it, the contact will now show up in the CRM!
     
    //Now let's enter the "Comment" field from the HTML form as a note on the contact's record
    //Get the new ContactId
    $ContactId = $Result['ContactId'];
     
     
    $Function = "CreateNote";
    $Parameters = array(
        "ContactId"=>$ContactId,
        "Note"=>$_REQUEST['Comment']
    );
     
    //And pass that note to the API
    $Result = CallAPI($UserCode, $APIToken, $Function, $Parameters);
     
    /*
    There are all kinds of other things you might want to do here as well such as:
        -Set a task to follow up with the contact (https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/11/)
        -Add the contact as a lead (https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/87/)
        -Add the contact to a group (https://www.lessannoyingcrm.com/help/topic/API_Example_Code_PHP/13/)
        -Send an email to yourself letting you know a form was submitted (you can use the PHP "mail" function)
    */
     
     
    //Now forward the visitor to an html page confirming that we got their contact info
    header('location:confirm.html');
     
      
    /*
        This function takes all of the settings needed to call the API and puts them into
        one long URL, and then it makes an HTTP request to that URL.
    */
    function CallAPI($UserCode, $APIToken, $Function, $Parameters){
        $APIResult = file_get_contents("https://api.lessannoyingcrm.com?UserCode=$UserCode&APIToken=$APIToken&".
                    "Function=$Function&Parameters=".urlencode(json_encode($Parameters)));
        $APIResult = json_decode($APIResult, true);
          
        if(@$APIResult['Success'] === true){
            //echo "Success!";
        }
        else{
            echo "API call failed. Error:".@$APIResult['Error'];
            exit;
        }
        return $APIResult;
    }

  

I would great appreciate any help in getting the customfield working. thank you

4
  • Can you show the json expect by LACRM? Commented Mar 22, 2021 at 16:52
  • the api log is Parameters: {"PipelineId":"3727019319364096972431828675722","FullName":"John Doe","Email":["([Text, [email protected]], [Type, Work])"],"Phone":["([Text, 5555555555], [Type, Work])"],"Website":["[Text, ]"],"Location":"[, john street]","CustomFields":"([Conservatory, Glass], [Size, 3.33 x 3.33 Metres], [Type, ], [Source, Newspaper], [Comment, testing api], [Conservatory, Lean-To])"} Response: {"ContactId":"3727243229049634192380189957314","Success":true} Commented Mar 22, 2021 at 17:28
  • Can you show the value of json_encode($Parameters) from the php example? Commented Mar 23, 2021 at 7:47
  • this is the result of the PHP: Parameters: {"FullName":"John Doe","Email":[{"Text":"[email protected]","Type":"Personal"}],"Phone":[{"Text":"05555555555","Type":"Mobile"}],"CompanyName":"Example","CustomFields":{"Type":"Glass","Size":"3.33 x 3.33 Metres","Source":"Radio","Comment":"Example","Location":"London, UK"}} Response: {"ContactId":"3727368068151081214811269180358","CompanyId":"3727283960281429054350232441254","Success":true} Commented Mar 23, 2021 at 8:31

1 Answer 1

0

In C#, the associative array is Dictionary. You can prepare parameters like :

var parameters = new Dictionary<string, object> {
    { "FullName", booking.FullName },
    { "Email",
        new object[] {
            new Dictionary<string, string> {
                { "Text", booking.Email},
                { "Type", "Work"},
            }
        }
    },
    { "Phone", new object[] {
            new Dictionary<string, string> {
                { "Text", booking.Phone},
                { "Type", "Work"},
            }
        }
    }
    // Complete the other data
};

In the PHP example, the API call use the HTTP method GET, but you can use POST (and I advocate this). From the LACRAM documentation :

You can use POST, GET, or whatever else you want (it's all the same to us).

string url = "https://api.lessannoyingcrm.com?APIToken=sometoken&UserCode=A2575&Function=CreateContact&PipelineId=3727019319364096972431828675722";
var parametersAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(parameters);
using (var httpClient = new HttpClient())
{
    var response = await httpClient.PostAsync(url, new StringContent(parametersAsJson));
    // Check the response status code to check if operation success
    // Usualy 200 (Ok), maybe LACRAM return other success code, check the doc
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        var resultAsJson = await response.Content.ReadAsStringAsync();
        var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(resultAsJson);
        var contactId = result["ContactId"];
        var companyId = result["CompanyId"];
    }
    else
    {
        throw new InvalidOperationException("Fail to create the contact");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

It is POST. The API CreateContact in the Less Annoying CRM from the submission of the contact form from the website so I assume it is POST. I might be wrong
We are two right. From the documentation : You can use POST, GET, or whatever else you want (it's all the same to us).. In this case, I advocate to do a post.
The both example you have posted.. are those the correct method to use?
Ok thank you very much.. I will try this and let you know :-)
@KadronG did you get it working? I'm about to start LACRM integration via their API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.