0
votes

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

1
Can you show the json expect by LACRM? - vernou
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} - KadronG
Can you show the value of json_encode($Parameters) from the php example? - vernou
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} - KadronG

1 Answers

0
votes

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");
    }
}